我正在编译一个C++程序,但一直收到以下错误信息:
undefined reference to 'pthread_mutexattr_init'
undefined reference to 'dlopen'
undefined reference to 'dlerror'
undefined reference to 'dlsym'
undefined reference to 'dlclose'
为了解决
pthread
的错误,我在我的 CMakeLists.txt
中添加了以下链接器标志。if (UNIX)
set(CMAKE_CXX_FLAGS "-pthread")
endif (UNIX)
这解决了pthread
错误。为了解决libdl
错误,我继续修改它如下。
if (UNIX)
set(CMAKE_CXX_FLAGS "-pthread -dl")
endif (UNIX)
这给了我一个警告
unrecognized gcc debugging option: l
我进行了如下修改。
if (UNIX)
set(CMAKE_CXX_FLAGS "-pthread")
set(CMAKE_CXX_FLAGS "-dl")
endif (UNIX)
并且一起收到了所有错误消息
unrecognized gcc debugging option: l.
我是否错过了在CMake中设置链接器标志的方法?我到底做错了什么?我使用的是Ubuntu 17.04 x64。
target_link_libraries(yourtarget pthread dl)
吗? - Davide Spataro