我正在尝试构建一个具有以下结构的项目:
└── main/
├── CMakeLists.txt/
│ └── src/
│ └── main.cpp
└── models/
└── modelA/
├── src/
│ └── h
├── lib
└── CMakeLists.txt
main/CMakeLists.txt
看起来像这样:
cmake_minimum_required(VERSION 3.17.0)
project(fiducial_marker_test)
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Release)
ENDIF()
MESSAGE("Build type: " ${CMAKE_BUILD_TYPE})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -w ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -w ")
# Check C++11 or C++0x support
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_subdirectory(${CMAKE_SOURCE_DIR}/models/modelA)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/app)
add_executable(main ${CMAKE_SOURCE_DIR}/src/main.cpp)
target_include_directories(main PRIVATE ${CMAKE_SOURCE_DIR}/modules/modelA/src/)
target_link_libraries(main PRIVATE modelA)
main/models/modelA/CMakeLists.txt
是这样的:
cmake_minimum_required(VERSION 3.17.0)
project(libmodelA)
find_package(OpenCV REQUIRED)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
file(GLOB SRC_FILE1 "./src*.c*")
file(GLOB SRC_FILE2 "./src/ED/*.c*")
include_directories(
${OpenCV_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}/src/
${CMAKE_CURRENT_SOURCE_DIR}/src/ED/
)
add_library(
modelA SHARED
${SRC_FILE1}
${SRC_FILE2}
)
target_include_directories(modelA INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/src/ED)
target_link_libraries(
modelA
${OpenCV_LIBS}
)
当我尝试运行cmake --build时。在main/build中,它给出以下错误:
[ 92%] Building CXX object CMakeFiles/main.dir/src/main.cpp.o
[100%] Linking CXX executable ../app/main
/usr/bin/ld: CMakeFiles/main.dir/src/main.cpp.o: in function `main':
main.cpp:(.text.startup+0xb9): undefined reference to `ModelA::ModelA(int, int, bool)'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/main.dir/build.make:113: ../app/main] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/main.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2
我尝试了不同的方法来解决这个问题,但到目前为止没有运气。我认为是连接问题?我对cmake还很陌生,所以任何提示或见解都会非常感激。
1条答案
按热度按时间disho6za1#
首先,我会用
CMAKE_VERBOSE_MAKEFILE
设置CMake verbose来检查编译命令然后,当你运行 cmake --build 时,你应该能够在linkage中看到类似
-lmodels/ModelA/libModelA.so
的东西,这意味着它做得很好。其次,在不检查代码的情况下检查它,似乎ModelA. h中的构造函数签名可能与ModelA. cc中的构造函数签名不同。难道是这样?
最后,你可以让你的CMake配置更干净、更简洁。当您使用
find_package
时,大多数情况下,您可以链接到这些包的目标。这些目标包含库和头文件,因此您不需要手动包含和链接必要的库。如果您将target_include_directories
与INTERFACE或PUBLIC一起使用,这同样适用于您自己的库。您可以在Using OpenCV's CMake module targets instead of including all libraries directly?中检查OpenCV目标或在Conan center中检查其他libs目标。这样做,main/models/modelA/CMakeLists.txt
将是这样的东西: