我正在尝试将一些代码从 PC 移植到 Ubuntu,但由于在 Linux 下开发的经验有限,我遇到了一些问题。
我们使用 CMake 生成所有构建内容。在windows下我做的是VS2010项目,在Linux下我做的是Eclipse项目。我已经设法成功移植了我的 OpenCV 内容,但在尝试移植我的线程增强应用程序时遇到了很大的麻烦。
为了让我们清楚,到目前为止我在干净的 Ubuntu 12 安装上遵循的步骤。 (我已经完成了 2 次干净的重新安装以尝试修复潜在的库错误,现在我只是放弃并询问):
- 使用我的包管理器安装 Eclipse 和 Eclipse CDT
- 使用我的包管理器安装 CMake 和 CMake Gui
- 使用我的包管理器安装 libboost-all-dev
到目前为止,这就是我所做的一切。我可以使用 CMake 创建 eclipse 项目而没有错误,因此 CMake 成功找到了我的 boost 安装。当我尝试通过 Eclipse 进行构建时,就会遇到问题;我正在尝试构建的应用程序使用 boost::asio 进行某些 UDP I/O,并使用 boost::thread 为 asio I/O 服务创建工作线程。我可以成功编译每个模块,但是当我进行链接时,我会收到大量错误信息,例如:
/usr/bin/c++ CMakeFiles/RE05DevelopmentDemo.dir/main.cpp.o CMakeFiles/RE05DevelopmentDemo.dir/RE05FusionListener/RE05FusionListener.cpp.o CMakeFiles/RE05DevelopmentDemo.dir/NewEye/NewEye.cpp.o -o RE05DevelopmentDemo -rdynamic -Wl,-Bstatic -lboost_system-mt -lboost_date_time-mt -lboost_regex-mt -lboost_thread-mt -Wl,-Bdynamic
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libboost_thread-mt.a(thread.o): In function `void boost::call_once<void (*)()>(boost::once_flag&, void (*)()) [clone .constprop.98]':
make[2]: Leaving directory `/home/david/Code/Build/Support/RE05DevDemo'
(.text+0xc8): undefined reference to `pthread_key_create'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libboost_thread-mt.a(thread.o): In function `boost::this_thread::interruption_enabled()':
(.text+0x540): undefined reference to `pthread_getspecific'
make[1]: Leaving directory `/home/david/Code/Build/Support/RE05DevDemo'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libboost_thread-mt.a(thread.o): In function `boost::this_thread::disable_interruption::disable_interruption()':
(.text+0x570): undefined reference to `pthread_getspecific'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libboost_thread-mt.a(thread.o): In function `boost::this_thread::disable_interruption::disable_interruption()':
(.text+0x59f): undefined reference to `pthread_getspecific'
我从其他 StackOverflow 帖子中收集并已经检查过的一些问题:
- boost 库都在/usr/lib 中
- 我没有收到任何无法找到 boost header 的编译错误,因此必须找到它们。
- 我正在尝试静态链接,但我相信 eclipse 应该传递正确的参数来实现这一点,因为我的 CMakeLists.txt 包含
SET(Boost_USE_STATIC_LIBS ON)
我在这里正式失去了想法,我已经尝试过本地构建 boost 和一堆其他东西但没有成功。我什至重新安装了 Ubuntu,以确保我没有完全破坏 libs 目录和与多个奇怪版本或其他任何东西的链接。任何帮助将不胜感激。
最佳答案
正确的机制是使用 Threads 包:
find_package(Threads)
#...
target_link_libraries(my_app ${CMAKE_THREAD_LIBS_INIT} ...)
关于linux - linux 下使用 Eclipse 和 CMake 的 boost::thread 的链接器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12594704/