Compiling boost::program_options on Mac OS X with g++ 4.8 (Mac Ports)
Asked Answered
S

2

7

I'm trying to compile one of the boost::program_options examples, 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...

The boost libraries were installed via MacPorts, placing the header files in /opt/local/include and library files in /opt/local/lib. The compile command used was:

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

Compiling header-only boost functions work fine using g++ 4.8 i.e factorial, hermite etc.

What I find strange is 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 doesn't this example compile with gcc 4.8? Any ideas? Is there a way to fix this?

Update: The example also compiles using clang++

Sepalous answered 11/11, 2013 at 18:3 Comment(0)
T
5

As Shigerello already mentioned, the problem lies in boost compiled with clang and boost compiled with gcc being incompatible.

A simpler variation to get boost compiled with GCC-4.8 is to just use the gcc48 variant of boost:

sudo port install boost +gcc48
Topmast answered 16/5, 2014 at 12:53 Comment(1)
Hi agi, this worked flawlessly without problems! Though Shigerello gave an excellent explanation, the simplicity of your solution cannot go unnoticed. This method did not give problems with conflicting gmp files.Sepalous
T
9

I suspect you're using Boost compiled with Clang, which is incompatible with GCC.

This is because C++ implementation differs between them, and Clang's STL classes are put in a namespace different from GCC's using inline namespace for the sake differentiating Clang from more popular GCC; e.g. std::basic_string is std::__1::basic_string in Clang by inline namespace).

Try a command below, If you see "libc++" in output, yes, your Boost was built with Clang (or "libstdc++" 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 to rebuild Boost with GCC-4.8 provided by MacPorts:

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

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


Other 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

Th answered 16/11, 2013 at 5:25 Comment(1)
Thank you so much for that excellent answer! As you suspected, I did see "libc++" when I ran the otool command, however using your command to rebuild boost using gcc4.8 failed on my machine - more an issue of conflicting gmp files, which I will have to sort out. I skipped the rebuilding of dependences using sudo port -ns upgrade --force boost configure.compiler=macports-gcc-4.8 which worked. The boost example compiled and ran perfectly. Cheers.Sepalous
T
5

As Shigerello already mentioned, the problem lies in boost compiled with clang and boost compiled with gcc being incompatible.

A simpler variation to get boost compiled with GCC-4.8 is to just use the gcc48 variant of boost:

sudo port install boost +gcc48
Topmast answered 16/5, 2014 at 12:53 Comment(1)
Hi agi, this worked flawlessly without problems! Though Shigerello gave an excellent explanation, the simplicity of your solution cannot go unnoticed. This method did not give problems with conflicting gmp files.Sepalous

© 2022 - 2024 — McMap. All rights reserved.