Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

c++ - 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.

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:

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

On linux CMake figures itself that boost_filesystem is linked against boost_system. Obviously you have to tell it explicitly on Mac:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...