Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking troubles with boost::program_options on OSX using LLVM

I'm having trouble getting through the linking phase in my C++ program due to problems with Boost 1.49. I have switched to C++ (-std=c++11 -libc=libc++) which works fine for another piece of code (which also uses boost). Boost was installed using homebrew with:

brew install boost --universal --with-mpi --with-icu

The trouble starts with boost::program_options. I get the link errors like this:

  "boost::program_options::validate(boost::any&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*, int)", referenced from:

... etc. ...

ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This is a little strange, because doing an nm on the library used reveals, that the symbol appears to be there:

nm -U /usr/local/lib/libboost_program_options-mt.dylib  | grep validate
0000000000019880 - 01 0000   FUN __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISbIwSt11char_traitsIwESaIwEESaIS7_EEPSsi
0000000000019880 T __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISbIwSt11char_traitsIwESaIwEESaIS7_EEPSsi
00000000000199e0 - 01 0000   FUN __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISbIwSt11char_traitsIwESaIwEESaIS7_EEPbi
00000000000199e0 T __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISbIwSt11char_traitsIwESaIwEESaIS7_EEPbi
0000000000019930 T __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISsSaISsEEPSsi
0000000000019930 - 01 0000   FUN __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISsSaISsEEPSsi
0000000000019c70 - 01 0000   FUN __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISsSaISsEEPbi
0000000000019c70 T __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISsSaISsEEPbi

I have already tried coaxing homebrew to compile boost with clang instead of gcc by setting CXX and CXX_FLAGS accordingly prior to installation. Not sure I succeeded though.

Pointers greatly appreciated.

like image 225
Jürgen Simon Avatar asked Jun 18 '12 11:06

Jürgen Simon


People also ask

Why is LLVM so bad on macOS?

MacOS is really frustrating with how it handles its libraries and compilers. It is also frustrating because it ships an unspecified version of LLVM, which generally isn’t the latest stable release. You can, however, with a little tweaking, use the latest version of LLVM or GCC on your Mac, and reliably use it for your C and C++ tooling.

How do I use Boost command line options?

To use Boost.ProgramOptions, include the header file boost/program_options.hpp. You can access all classes and functions from this library in the namespace boost::program_options. Use the class boost::program_options::options_description to describe command-line options.

Why does my LLVM + Clang build fail to link?

Where $ {PACKAGE} is a full path to the directory in which I expanded the downloaded LLVM + Clang. The build then fails to link: Apparently this is related to a move inside of clang, where FS has moved from libc++experimental to libc++fs [2].

What is Boost program options in Visual Studio?

Boost.ProgramOptions Boost.ProgramOptions is a library that makes it easy to parse command-line options, for example, for console applications. If you develop applications with a graphical user interface, command-line options are usually not important. To parse command-line options with Boost.ProgramOptions, the following three steps are required:


1 Answers

You will need to recompile boost with clang and std11 flags, the libc++ library is not binary compatible with the installed libstdc++ in OSX (very early version of gcc prior to changing to gpl3). If your version of clang is 3.1 or over then you can use (otherwise change c++11 to c++0x for earlier versions).

./bootstrap.sh
mkdir build
sudo ./bjam toolset=clang cxxflags="-std=c++0x -stdlib=libc++" variant=release link=static threading=multi runtime-link=shared --build-dir=Build --layout=system --without-mpi --without-python install --prefix=/usr/local 

You can of course alter any of these as you wish except

toolset=clang cxxflags="-std=c++0x -stdlib=libc++"

This should work for you.

like image 191
dirvine Avatar answered Sep 28 '22 05:09

dirvine