Compiling boost :: program_options on Mac OS X with g ++ 4.8 (Mac ports)

I am trying to compile one example of boost :: program_options, http://svn.boost.org/svn/boost/trunk/libs/program_options/example/first.cpp using gcc 4.8 (installed via MacPorts). However, I keep getting errors:

  Undefined symbols for architecture x86_64:
   "boost :: program_options :: to_internal (std :: basic_string, std :: allocator> const &)", referenced from:
       std :: vector, std :: allocator>, std :: allocator, std :: allocator>>> boost :: program_options :: to_internal, std :: allocator>> (std :: vector, std :: allocator>, std :: allocator, std :: allocator>>> const &) in ccEWnIGV.o
   "boost :: program_options :: options_description :: options_description (std :: basic_string, std :: allocator> const &, unsigned int, unsigned int)", referenced from:
       _main in ccEWnIGV.o
   "boost :: program_options :: invalid_option_value :: invalid_option_value (std :: basic_string, std :: allocator> const &)", referenced from:
       void boost :: program_options :: validate (boost :: any &, std :: vector, std :: allocator>, std :: allocator, std :: allocator>>> const &, double *, long) in ccEWnIGV.o
   "boost :: program_options :: error_with_option_name :: error_with_option_name (std :: basic_string, std :: allocator> const &, std :: basic_string, std :: allocator> const &, std :: basic_string, std :: allocator> const &, int) ", referenced from:
       boost :: program_options :: validation_error :: validation_error (boost :: program_options :: validation_error :: kind_t, std :: basic_string, std :: allocator> const &, std :: basic_string, std :: allocator> const &, int) in ccEWnIGV .o
   "boost :: program_options :: detail :: cmdline :: set_additional_parser (boost :: function1, std :: allocator>, std :: basic_string, std :: allocator>>, std :: basic_string, std :: allocator> const &> ) ", referenced from:
       boost :: program_options :: basic_command_line_parser :: extra_parser (boost :: function1, std :: allocator>, std :: basic_string, std :: allocator>>, std :: basic_string, std :: allocator> const &>) in ccEWnIGV. o
   "boost :: program_options :: detail :: cmdline :: cmdline (std :: vector, std :: allocator>, std :: allocator, std :: allocator>>> const &)", referenced from:
       boost :: program_options :: basic_command_line_parser :: basic_command_line_parser (int, char const * const *) in ccEWnIGV.o
   "boost :: program_options :: operator> &, boost :: program_options :: options_description const &)", referenced from:
       _main in ccEWnIGV.o
   "boost :: program_options :: abstract_variables_map :: operator [] (std :: basic_string, std :: allocator> const &) const", referenced from:
       boost :: program_options :: variables_map :: operator [] (std :: basic_string, std :: allocator> const &) const in ccEWnIGV.o
   "boost :: program_options :: error_with_option_name :: substitute_placeholders (std :: basic_string, std :: allocator> const &) const", referenced from:
       vtable for boost :: program_options :: invalid_option_value in ccEWnIGV.o
       vtable for boost :: program_options :: validation_error in ccEWnIGV.o
 etc ... 

Acceleration libraries were installed through MacPorts, placing header files in /opt/local/include and library files in /opt/local/lib . Used compilation command:

$ g++ -I/opt/local/include first.cpp -L/opt/local/lib -lboost_program_options-mt

Compiling boost-only boost functions works fine using g ++ 4.8 ie factorial, germite, etc.

It seems strange to me that this example compiles if I use the llvm-g ++ compiler:

$ llvm-g++ -I/opt/local/include first.cpp -L/opt/local/lib -lboost_program_options-mt

  $ g ++ -v
 Using built-in specs.
 COLLECT_GCC = g ++
 COLLECT_LTO_WRAPPER = / opt / local / libexec / gcc / x86_64-apple-darwin12 / 4.8.1 / lto-wrapper
 Target: x86_64-apple-darwin12
 Thread model: posix
 gcc version 4.8.1 (MacPorts gcc48 4.8.1_3)
\
  $ llvm-g ++ -v
 Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
 Target: x86_64-apple-darwin13.0.0
 Thread model: posix
\

Why is this example not compiling with gcc 4.8? Any ideas? Is there any way to fix this?

Update: the example also compiles using clang ++

+6
source share
2 answers

As Shigerello mentioned earlier, the problem is that boost compiles with clang and boost compiled with gcc is incompatible.

A simpler variation to get boost compiled with GCC-4.8 is to simply use the gcc48 boost option:

 sudo port install boost +gcc48 
+5
source

I suspect that you are using Boost compiled with Clang, which is incompatible with GCC.

This is because the C ++ implementation is different between the two, and the Clang STL classes are placed in a namespace other than GCC, using the built-in namespace to differentiate Clang from the more popular GCC; for example std::basic_string is std::__1::basic_string in Clang by the built-in namespace).

Try the command below: if you see "lib ++" , yes, your Boost was created using Clang (or "libstd ++" for GCC):

 $ otool -L /opt/local/lib/libboost_program_options-mt.dylib /opt/local/lib/libboost_program_options-mt.dylib: /opt/local/lib/libboost_program_options-mt.dylib (compatibility version 0.0.0, current version 0.0.0) /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0) 


If Boost was built with Clang, I recommend you rebuild Boost with the GCC-4.8 provided by MacPorts:

 sudo port upgrade --force boost configure.compiler=macports-gcc-4.8 

You can find other compilers available at https://trac.macports.org/wiki/UsingTheRightCompiler#configure-compiler .


Another useful link: http://wiki.inkscape.org/wiki/index.php/CompilingMacOsX#Compiling_with_clang_3.3_and_libc.2B.2B_.28c.2B.2B11_enabled.29_.282013.29

+9
source

Source: https://habr.com/ru/post/957854/


All Articles