如何针对与cmake的boost.system链接(How to link against boos

2019-06-24 13:29发布

我用的makefile生成cmake的编译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

从生成该错误生成文件的动作是这样的线:

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 

一些谷歌搜索给我看,这个错误似乎是与升压文件系统库的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)

Answer 1:

在Linux上的CMake数字本身boost_filesystem是针对boost_system链接。 很明显,你必须明确地告诉它在Mac上:

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


Answer 2:

这不是一个“答案”张贴的问题,但在我的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上。



文章来源: How to link against boost.system with cmake