2
votes

I'm trying to use boost::program_options following the official instruction: http://www.boost.org/doc/libs/1_36_0/more/getting_started/unix-variants.html#link-your-program-to-a-boost-library

But it doesn't work:

~/download/boost_1_36_0/libs/program_options/example> g++ -o first first.cpp /usr/lib64/libboost_program_options-mt-1_36.a

/tmp/ccNh69JH.o: In function `main':
first.cpp:(.text+0xc8): undefined reference to `boost::program_options::options_description::options_description(std::basic_string, std::allocator > const&, unsigned int, unsigned int)'
/tmp/ccNh69JH.o: In function `std::basic_string, std::allocator > const& boost::program_options::validators::get_single_string(std::vector, std::allocator >, std::allocator, std::allocator > > > const&, bool)':                                                                     
first.cpp:(.text._ZN5boost15program_options10validators17get_single_stringIcEERKSbIT_St11char_traitsIS3_ESaIS3_EERKSt6vectorIS7_SaIS7_EEb[std::basic_string, std::allocator > const& boost::program_options::validators::get_single_string(std::vector, std::allocator >, std::allocator, std::allocator > > > const&, bool)]+0x142): undefined reference to `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&)'
first.cpp:(.text._ZN5boost15program_options10validators17get_single_stringIcEERKSbIT_St11char_traitsIS3_ESaIS3_EERKSt6vectorIS7_SaIS7_EEb[std::basic_string, std::allocator > const& boost::program_options::validators::get_single_string(std::vector, std::allocator >, std::allocator, std::allocator > > > const&, bool)]+0x2e9): undefined reference to `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&)'
collect2: ld returned 1 exit status

This works but it is weird:

g++ -o first first.cpp /usr/lib64/libboost_program_options.so.1.42.0
2
Your link is for boost 1.45 but your code appears to be using the rather ancient boost 1.36. Why the difference?Sam Miller
@Sam: changed, no differencesRuggero Turra
what is 'weirdie' about your second example?Sam Miller
@Sam: because with 1.36 lib.a and 1.36 include (.hpp) (the one installed on the system) it doesn't work, but it works with 1.42 lib.0 and 1.36 include (.hpp). I've update boost to 1.42 and everythigs work.Ruggero Turra
in your first example you are linking a static archive, in the second example you are linking a dynamic library.Sam Miller

2 Answers

2
votes

Assuming you've included the header, compiled the libraries and added a reference to them correctly, I can suggest you a fast (and lazy) way to solve the problem. You can add the source files to the project and compile them together while you work the solution out

1
votes

I've created a boost.pc file with following contents:

prefix=/usr/local/boost
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: boost
Description: my boost pc file
Version: 1.57.0
Cflags: -I${includedir}
Libs: -L${libdir} -lboost_system -lboost_program_options

Now I can run something like

g++ -g -O2 `pkg-config ./boost.pc --cflags --libs` -w -c main-new.cpp -o obj/main.o
g++ -g -O2 -w obj/main.o -o bin/main `pkg-config ./boost.pc --cflags --libs`

(via makefile)