未定义对功能CMake的引用 [英] Undefined reference to function CMake
问题描述
我正在尝试学习CMake,但是未获得对...链接器错误的引用 我有一个带有子目录的目录. 他们每个人都有自己的CMakeLists.txt
I am trying to learn CMake, but I get a undefined reference to ... linker error I have a directory with a subdirectory. each of them has its own CMakeLists.txt
test
|----main.cpp
|----CMakeLists.txt
|----test2
|----foo.hpp
|----foo.cpp
|----CMakeLists.txt
用于测试的CMakeLists.txt是:
the CMakeLists.txt for test is:
cmake_minimum_required(VERSION 3.5)
project(tests)
add_subdirectory(test2)
set(SOURCE_FILES main.cpp)
add_executable(tests ${SOURCE_FILES})
test2的CMakeLists.txt是:
the CMakeLists.txt for test2 is:
set(test2_files
foo.cpp
foo.hpp
)
add_library(test2 ${test2_files})
foo.cpp
实现在foo.hpp
中定义的功能
对于此功能,我收到未定义的参考错误.
我究竟做错了什么?如何摆脱链接器错误
foo.cpp
implements a function which is defined in foo.hpp
for this function I am getting the undefined reference error.
What am I doing wrong? How can I get rid of this linker error
我的CMakeLists.txt现在看起来像这样,但是我仍然收到链接器错误:
My CMakeLists.txt now looks like this, but I still get the linker error:
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
I also tried it with the absolute path instead of test2
解决了它只是test2的CMakeLists.txt中的一个错字.
solved it it was only a typo in the CMakeLists.txt of test2.
推荐答案
请确保您的测试CMakeLists.txt
链接到已创建的库.
Make sure that your test CMakeLists.txt
links to the created library.
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 )
这篇关于未定义对功能CMake的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!