CMake 项目构建的问题

Issue with CMake project building

本文关键字:问题 构建 项目 CMake      更新时间:2023-10-16

我有以下问题。在我的 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,提升版本是 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

我遇到了类似的问题,但mpich.双:

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

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

工作正常。