如何使用cmake链接boost.system

时间:2009-06-30 19:54:30

标签: c++ macos boost linker cmake

我使用cmake生成的makefile来编译依赖于boost文件系统库的c ++文件。

在链接过程中,我收到以下错误:

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

生成此错误的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 /opt/local/lib/libboost_filesystem-mt.dylib 

一些谷歌搜索告诉我,这个错误似乎在使用boost文件系统库的mac上很常见,因为我必须链接到boost.system库或根据boost.system库创建我的项目。

如何强制cmake链接到库而不对库路径进行硬编码?

这是otool的结果:

otool -L /opt/local/lib/libboost_filesystem-mt.dylib
 /opt/local/lib/libboost_filesystem-mt.dylib:
 /opt/local/lib/libboost_filesystem-mt.dylib (compatibility version 0.0.0, current version 0.0.0)  
 /opt/local/lib/libboost_system-mt.dylib (compatibility version 0.0.0, current version 0.0.0)   
 /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.4.0)
 /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
 /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.0.0)

2 个答案:

答案 0 :(得分:68)

在Linux上,CMake认为boost_filesystem与boost_system相关联。显然你必须在Mac上明确告诉它:

find_package(Boost COMPONENTS system filesystem REQUIRED)
#...
target_link_libraries(mytarget 
  ${Boost_FILESYSTEM_LIBRARY}
  ${Boost_SYSTEM_LIBRARY}
)

答案 1 :(得分:8)

这不是发布问题的“答案”,而是对我的Ubuntu框的观察。

要使用Boost库,您必须明确地编写如下内容:

find_package(Boost COMPONENTS regex system filesystem REQUIRED)

此外,您需要以这种方式链接:

target_link_libraries(binary
  ${Boost_FILESYSTEM_LIBRARY}
  ${Boost_SYSTEM_LIBRARY}
  ${Boost_REGEX_LIBRARY}
)

并且以下方式不起作用,至少对我而言:

target_link_libraries(binary regex system filesystem)

这遵循Maik所说的,但不仅仅是在Mac上。