cpp
cpp

Reputation: 3801

Simple way for linking boost libraries

I'd like to use (in Linux Debian Squeeze g++4.4) separately-compiled Boost (1.54.0) libraries:

In order to do this, according to Easy Build and Install, I typed in terminal

$ cd path/to/boost_1_54_0
$ ./bootstrap.sh --prefix=~/boost
$ ./b2 install

As a result two folders include and lib were created in ~/boost. In ~/boost/lib there are files:

libboost_name.a
libboost_name.so
libboost_name.so.1.54.0

for each boost library.

Then I include some library (for example regex) in my test.cpp file:

#include<boost/regex.hpp>   //may be also chrono, filesystem or whatever

I tell the compiler to search for regex library in ~/boost/lib

$ g++ -I path/to/boost_1_54_0 test.cpp -o test -L~/boost/lib -lboost_regex

But this results in compilation error:

test.cpp:(.text+0x49): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x53): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x5d): undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status

What is wrong? There are no stage folder in my ~/boost, nor antything like libboost_regex-gcc34-mt-d-1_36.a mentioned in Easy Build and Install. Is this the reson?

Here is the content of test.cpp

//I need a general solution/idea that works for any of these libraries
#include <boost/regex.hpp>
//#include <boost/chrono.hpp>
//#include <boost/filesystem.hpp>
//#include<boost/some-other-separately-compiled-library>

int main()
{
}

Is there a simple way for linking boost libraries that works for all of the Boost libraries that must be built separately?

Upvotes: 2

Views: 3477

Answers (2)

Dirk is no longer here
Dirk is no longer here

Reputation: 368181

Something is not right with your Boost setup. Any particular library within Boost should correctly resolve as needed.

On a Debian / Ubuntu system, with headers and libraries in standard places and using Boost as shipped, I can just invoke g++ with a single -lfoo:

edd@max:/tmp$ g++ -o boost_re_ex boost_regex_credit_card_ex.cpp -lboost_regex
edd@max:/tmp$ ./boost_re_ex 
validate_card_format("0000111122223333") returned 0
validate_card_format("0000 1111 2222 3333") returned 1
validate_card_format("0000-1111-2222-3333") returned 1
validate_card_format("000-1111-2222-3333") returned 0
machine_readable_card_number("0000111122223333") returned 0000111122223333
machine_readable_card_number("0000 1111 2222 3333") returned 0000111122223333
machine_readable_card_number("0000-1111-2222-3333") returned 0000111122223333
machine_readable_card_number("000-1111-2222-3333") returned 000111122223333
human_readable_card_number("0000111122223333") returned 0000-1111-2222-3333
human_readable_card_number("0000 1111 2222 3333") returned 0000-1111-2222-3333
human_readable_card_number("0000-1111-2222-3333") returned 0000-1111-2222-3333
human_readable_card_number("000-1111-2222-3333") returned 000-1111-2222-3333
edd@max:/tmp$ 

This use the Boost 'credit card' example file straight from their website.

Upvotes: 1

Violet Giraffe
Violet Giraffe

Reputation: 33579

Can't give an exact answer because I don't know which boost lib depends on which, but here's the thing: with the default Linux linker (don't know what's it called, ld?) you have to be very careful with the order in which you feed libs to a linker (order of your -l flags). You have to specify a depending library first and then a dependent library, in that order. So if your application uses library a and a depends on b, the order is:

-la -lb,

not the other way around. Otherwise when linker encounters a new input library that's isn't yet required by any previously linked code, it will optimize symbols from this new "unneccessary" library away.

Bottom line - find out the order of dependence between your boost libraries.

Upvotes: 2

Related Questions