简体   繁体   English

如何使用cmake链接到boost.system

[英]How to link against boost.system with cmake

I use a cmake generated makefile to compile a c++ file that depends on the boost filesystem library. 我使用cmake生成的makefile来编译依赖于boost文件系统库的c ++文件。

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: 来自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

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. 进行一些谷歌搜索后,我发现该错误在boost文件系统库的Mac上似乎很常见,因为我必须链接到boost.system库,或者使我的项目依赖于boost.system库。

How do i force cmake to link against the library without hardcoding the library path? 我如何在不对库路径进行硬编码的情况下强制cmake链接库?

Here the result from otool: 这里是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)

On linux CMake figures itself that boost_filesystem is linked against boost_system. 在Linux上,CMake认为boost_filesystem与boost_system链接在一起。 Obviously you have to tell it explicitly on Mac: 显然,您必须在Mac上明确地告诉它:

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

This is not an "answer" to the posted question, but an observation on my Ubuntu box. 这不是发布问题的“答案”,而是对我的Ubuntu盒子的观察。

To use the Boost libs, you have to explicitly write something like this: 要使用Boost库,必须显式编写如下代码:

find_package(Boost COMPONENTS regex system filesystem REQUIRED)

In addition, you need to link it this way: 另外,您需要通过以下方式链接:

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

and the following way didn't work, at least for me: 并且以下方式无效,至少对我而言:

target_link_libraries(binary regex system filesystem)

This follows what Maik says, but not only on Mac. 这符合Maik所说的,但不仅限于Mac。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM