我有以下问题。在我的 Ubuntu 上,我尝试构建一个项目并收到以下链接器错误:
/usr/bin/ld:
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/libboost_thread.a(once.o): undefined reference to symbol 'pthread_once@@GLIBC_2.2.5'
/lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO
missing from command line collect2: error: ld returned 1 exit status
make[2]: *** [sunprint] Error 1 make[1]: ***
[CMakeFiles/sunprint.dir/all] Error 2 make: *** [all] Error 2
*** Failure: Exit code 2 ***
我在 ubuntu 13 桌面、GCC 4.8、boost 版本下运行。是 1.54。作为我正在使用的 IDE 是KDevelop。 如果需要,我可以提供有关此问题的任何其他信息,但现在我坚持 这个链接问题。
有什么想法吗? 提前谢谢。
最佳答案
add_definitions
只为预处理器添加输入,它甚至在 编译器 开始其业务之前就已经在起作用,甚至距离链接可执行文件更远,步骤 ld
应该是在做的。
您希望 ld
解决链接时依赖关系的是 CMake 命令 target_link_libraries
,对于给定的目标,该命令会在之后添加一些要链接的库编译。
在您的情况下,适当的调用可能如下所示
target_link_libraries(${PROJECT_NAME} [...] -lpthread [...]) #obviously without the '[...]' and the correct target name
关于c++ - CMake项目 build 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19902595/