Ruggero Turra
Ruggero Turra

Reputation: 17730

How to build Boost::program_options (on linux)

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

Upvotes: 3

Views: 4449

Answers (2)

apcel
apcel

Reputation: 11

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)

Upvotes: 1

tiagovrtr
tiagovrtr

Reputation: 162

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

Upvotes: 2

Related Questions