12

我有以下问题。在我的 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。如果需要,我可以提供有关此问题的任何其他信息,但现在我遇到了这个链接问题。

有任何想法吗?提前谢谢。

4

2 回答 2

13

add_definitions只为预处理器添加输入,它甚至在编译器开始其业务之前就在起作用,甚至距离链接可执行文件更远,这一步ld应该是做的。

您想要ld解决链接时依赖关系的是 CMake 命令target_link_libraries,对于给定的目标,它会在编译后添加一些要链接的库。

在您的情况下,适当的调用可能如下所示

target_link_libraries(${PROJECT_NAME} [...] -lpthread [...]) #obviously without the '[...]' and the correct target name
于 2013-11-11T10:00:17.800 回答
1

我遇到了类似的问题,但是mpich. 两个都:

target_link_libraries(${PROJECT_NAME} [...] -lmpich [...])

target_link_libraries(${PROJECT_NAME} [...] mpich [...])

工作正常。

于 2015-07-07T20:23:16.833 回答