我正在尝试学习CMake,但未获得对...链接器错误的引用我有一个带有子目录的目录。他们每个人都有自己的CMakeLists.txt
test
|----main.cpp
|----CMakeLists.txt
|----test2
|----foo.hpp
|----foo.cpp
|----CMakeLists.txt
用于测试的CMakeLists.txt是:
cmake_minimum_required(VERSION 3.5)
project(tests)
add_subdirectory(test2)
set(SOURCE_FILES main.cpp)
add_executable(tests ${SOURCE_FILES})
test2的CMakeLists.txt是:
set(test2_files
foo.cpp
foo.hpp
)
add_library(test2 ${test2_files})
foo.cpp
实现了foo.hpp
为此功能定义的功能,我收到未定义的参考错误。我究竟做错了什么?我如何摆脱此链接器错误
编辑:我的CMakeLists.txt现在看起来像这样,但我仍然收到链接器错误:
project(tests)
cmake_minimum_required(VERSION 2.8)
set(SOURCE_FILES main.cpp)
include_directories(test2)
link_directories(test2)
add_subdirectory(test)
add_executable( ${PROJECT_NAME} ${SOURCE_FILES} )
target_link_libraries(${PROJECT_NAME} test2)
我也尝试使用绝对路径而不是 test2
编辑:解决它只是test2的CMakeLists.txt中的一个错字。
确保您的测试CMakeLists.txt
链接到创建的库。
project(test)
cmake_minimum_required(VERSION 2.8)
set(SOURCE_FILES main.cpp)
include_directories( test2 )
#here
link_directories(test2)
add_subdirectory(test2)
add_executable( ${PROJECT_NAME} ${SOURCE_FILES} )
#and here
target_link_libraries( ${PROJECT_NAME} test2 )
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句