1

I can't figure out a linking error to use boost::program_options. Here's a sample c++ code:

# sample_code.cpp
#include <boost/program_options.hpp>
int main()
{
    boost::program_options::options_description description("Test");
}

I've also installed boost 1.57.0 using MacPorts on Yosemite 10.10.2. Here's the related library:

/opt/local/lib/libboost_program_options-mt.a

Let's compile and link this code:

alias g++='/opt/local/bin/g++-mp-5 -std=gnu++14 -I/opt/local/include -L/opt/local/lib'

g++ sample_code.cpp -lboost_program_options-mt

And it fails during linking:

Undefined symbols for architecture x86_64:
"boost::program_options::options_description::options_description(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, unsigned int)"
ld: symbol(s) not found for architecture x86_64

Any thoughts?

Reza Toghraee
  • 1,603
  • 1
  • 14
  • 21
  • Does it work if you reverse the order of the compiler arguments in the aliased use? If so, http://stackoverflow.com/a/24675715/962089 – chris Mar 30 '15 at 19:06
  • not really. I also tried clang++ and CMake with all its boost related arguments and it was no help! – Reza Toghraee Mar 30 '15 at 19:20
  • What do you mean by "not really"? The library _does_ go after the `.cpp`. – Lightness Races in Orbit Mar 30 '15 at 19:26
  • I fixed the code above and tested it again, but the error is the same: $ g++ sample_code.cpp -lboost_program_options-mt Undefined symbols for architecture x86_64: "boost::program_options::options_description::options_description(std::__cxx11::basic_string, std::allocator > const&, unsigned int, unsigned int)", referenced from: _main in ccIZf7tk.o ld: symbol(s) not found for architecture x86_64 collect2: error: ld returned 1 exit status – Reza Toghraee Mar 30 '15 at 19:57
  • 1
    Did macports compile boost with g++ or with clang++? Both need to be compiled with the *same* c++ standard library, otherwise you get link errors like that. clang++ uses libc++. g++ (proper gcc, not the clang wrapper) uses libstdc++. – Anya Shenanigans Mar 30 '15 at 21:06
  • Petesh, you are pointing to the source of problem here! The issue is that macports does not offer any gcc build variants other than clang :-/ – Reza Toghraee May 03 '15 at 20:55

2 Answers2

1

The solution is simple: You have to compile boost and your program with the same version of compilers (or at least they should be compatible). When my boost was compiled with GCC 4.8.3 and my code is being compiled with GCC 5.3.0 I got linking errors. After building boost with GCC 5.3.0, the linking problem went away.

Kemin Zhou
  • 6,264
  • 2
  • 48
  • 56
0

As pointed by Petesh in comments above, the issue turns out to be how MacPorts build boost using clang++ and not g++.

Unfortunately, MacPorts does not offer any gcc built variants for boost!

Reza Toghraee
  • 1,603
  • 1
  • 14
  • 21