【发布时间】:2016-06-23 23:13:03
【问题描述】:
我使用 CMake 3.5.2。尝试构建我的 C++ 代码时,出现以下错误:
[100%] Linking CXX executable SomeExecutable
CMakeFiles/SomeExecutable.dir/Common/src/FunctionOne.cpp.o: In function `FunctionOne::FunctionOne(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, double, double, unsigned int, bool)':
FunctionOne.cpp:(.text+0x490): undefined reference to `Helper::initLevel(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
程序在 myLib.so 中找不到 Helper::initLevel()。我该如何解决?
CMakeLists.txt:(为简洁而缩短)
cmake_minimum_required(VERSION 3.0)
project(ProjectName)
add_definitions(-std=c++11)
add_definitions(-Wall)
add_definitions(-O2)
link_directories(/usr/local/lib)
add_executable(SomeExecutable SomeExecutable.cpp ${FunctionOne} ${FunctionTwo})
target_link_libraries(SomeExecutable -pthread -lboost_thread ${Boost_LIBRARIES} myLib armadillo)
文件结构(为简洁起见):
├── bin
├── CMakeCache.txt
├── cmake_install.cmake
├── CMakeLists.txt
├── Common
│ ├── include
│ │ ├── dataContainer.h
│ │ ├── FunctionOne.h
│ │ ├── FunctionTwo.h
│ └── src
│ ├── FunctionOne.cpp
│ └── FunctionTwo.cpp
├── SomeExecutable.cpp
├── myLib
│ ├── example.cpp
│ ├── include
│ │ ├── Config.h
│ │ ├── Helper.h
│ ├── libmyLib.so
更新:
myLib/include/Config.h:
Namespace Helper {
[...]
Level* initLevel(const std::string& jsonLvlPath, bool twiceSpatPts = false);
[..]
}
更新 2:
kepj@laptop:~/ProjectName$ make VERBOSE=1
/usr/bin/cmake -E cmake_link_script CMakeFiles/SomeExecutable.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/SomeExecutable.dir/SomeExecutable.cpp.o CMakeFiles/SomeExecutable.dir/Common/src/FunctionOne.cpp.o CMakeFiles/SomeExecutable.dir/Common/src/FunctionTwo.cpp.o SomeExecutable -L/usr/local/lib -rdynamic -pthread -lboost_thread -lboost_system -lboost_filesystem -lmyLib -lsymbolicc++ -lmatio -larmadillo -Wl,-rpath,/usr/local/lib
CMakeFiles/SomeExecutable.dir/Common/src/FunctionTwo.cpp.o: In function `FunctionTwo::FunctionTwo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, double, double, unsigned int, bool)':
FunctionTwo.cpp:(.text+0x490): undefined reference to `Helper::initLevel(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
collect2: error: ld returned 1 exit status
CMakeFiles/SomeExecutable.dir/build.make:174: recipe for target 'SomeExecutable' failed
make[2]: *** [SomeExecutable] Error 1
make[2]: Leaving directory '/home/kepj/ProjectName'
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/SomeExecutable.dir/all' failed
make[1]: *** [CMakeFiles/SomeExecutable.dir/all] Error 2
make[1]: Leaving directory '/home/kepj/ProjectName'
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
【问题讨论】:
-
那么
Helper::initLevel位于哪个文件中?它是什么样子的? -
我的猜测是有一个
Helper.cpp你忘记包含在 CMake 中,但如果没有更多数据就无法得出结论 -
initLevel在 Config.h 中声明,但它在哪里定义?您编译的源代码中似乎缺少它,或者定义具有不同的签名。 -
@KarstenKoop 在 libmyLib.so 中,随源码一起分发。
-
请分享你运行时看到的
make VERBOSE=1