【发布时间】:2016-07-22 15:40:30
【问题描述】:
我正在尝试学习 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中的一个错字。
【问题讨论】: