如何使用 cmake 链接 boost.system
问题描述
我使用 cmake 生成的 makefile 来编译依赖于 boost 文件系统库的 c++ 文件.
在链接过程中,我收到以下错误:
<前>未定义符号:boost::system::get_generic_category()",引用自:__static_initialization_and_destruction_0(int, int)in FaceRecognizer.cpp.o__static_initialization_and_destruction_0(int, int)in FaceRecognizer.cpp.o__static_initialization_and_destruction_0(int, int)in FaceRecognizer.cpp.oboost::system::get_system_category()",引用自:__static_initialization_and_destruction_0(int, int)in FaceRecognizer.cpp.o__static_initialization_and_destruction_0(int, int)in FaceRecognizer.cpp.old:未找到符号collect2: ld 返回 1 个退出状态make[2]: *** [src/ImageMarker] 错误 1生成此错误的 makefile 中的操作是这一行:
<前>cd/Users/janusz/Documents/workspace/ImageMarker/Debug/src &&/opt/local/bin/cmake -E cmake_link_script CMakeFiles/ImageMarker.dir/link.txt --verbose=1/usr/bin/c++ -O3 -Wall -Wno-deprecated -g -verbose -Wl,-search_paths_first -headerpad_max_install_names -fPIC CMakeFiles/ImageMarker.dir/ImageMarker.cpp.o CMakeFiles/ImageMarker.dir/Image.cpp.o CMakeFiles/ImageMarker.dir/utils.cpp.o CMakeFiles/ImageMarker.dir/XMLWriter.cpp.o CMakeFiles/ImageMarker.dir/FaceRecognizer.cpp.o -o ImageMarker -L/opt/local/lib ../libTinyXml.a/选择/本地/lib/libboost_filesystem-mt.dylib一些谷歌搜索显示,这个错误在带有 boost 文件系统库的 Mac 上似乎很常见,因为我必须链接到 boost.system 库或根据 boost.system 库创建我的项目.
如何在不硬编码库路径的情况下强制 cmake 链接库?
这里是 otool 的结果:
在 linux 上,CMake 认为 boost_filesystem 与 boost_system 相关联.显然你必须在 Mac 上明确告诉它:
I use a cmake generated makefile to compile a c++ file that depends on the boost filesystem library.
During the linking process I get the following error:
Undefined symbols: "boost::system::get_generic_category()", referenced from: __static_initialization_and_destruction_0(int, int)in FaceRecognizer.cpp.o __static_initialization_and_destruction_0(int, int)in FaceRecognizer.cpp.o __static_initialization_and_destruction_0(int, int)in FaceRecognizer.cpp.o "boost::system::get_system_category()", referenced from: __static_initialization_and_destruction_0(int, int)in FaceRecognizer.cpp.o __static_initialization_and_destruction_0(int, int)in FaceRecognizer.cpp.o ld: symbol(s) not found collect2: ld returned 1 exit status make[2]: *** [src/ImageMarker] Error 1
The action from the makefile that generates this error is this line:
cd /Users/janusz/Documents/workspace/ImageMarker/Debug/src && /opt/local/bin/cmake -E cmake_link_script CMakeFiles/ImageMarker.dir/link.txt --verbose=1 /usr/bin/c++ -O3 -Wall -Wno-deprecated -g -verbose -Wl,-search_paths_first -headerpad_max_install_names -fPIC CMakeFiles/ImageMarker.dir/ImageMarker.cpp.o CMakeFiles/ImageMarker.dir/Image.cpp.o CMakeFiles/ImageMarker.dir/utils.cpp.o CMakeFiles/ImageMarker.dir/XMLWriter.cpp.o CMakeFiles/ImageMarker.dir/FaceRecognizer.cpp.o -o ImageMarker -L/opt/local/lib ../libTinyXml.a /opt/local/lib/libboost_filesystem-mt.dylib
Some googling showed me that this error seems to be common on macs with the boost file system library because I have to link against a boost.system library or make my project depending on the boost.system library.
How do i force cmake to link against the library without hardcoding the library path?
Here the result from otool:
On linux CMake figures itself that boost_filesystem is linked against boost_system. Obviously you have to tell it explicitly on Mac:
这篇关于如何使用 cmake 链接 boost.system的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!