6

I'm following the Boost Getting Started article. I've installed it with Bjam and I could see include files and library files (.a, .so).

#include <boost/regex.hpp> #include <iostream> #include <string> int main() { std::string line; boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" ); } 

If I build above code using this command

g++ -I./boost/include -L./boost/lib -lboost_regex-gcc43-mt -static -o test_boost2 test_boost2.cc 

I get this error:

/tmp/ccJFVVid.o: In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)': test_boost2.cc:(.text._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)]+0x22): undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)' collect2: ld returned 1 exit status 

It was working okay under my Windows machine.

What's wrong?

gcc version 4.3.2 (Debian 4.3.2-1.1)

Linux xxxxxxxx 2.6.26-1-686 #1 SMP Fri Mar 13 18:08:45 UTC 2009 i686 GNU/Linux

0

3 Answers 3

18
sudo apt-get install libboost-regex-dev 

Don't bother installing from source where a much more intelligent system can do better.

To compile, just use

g++ -lboost_regex boost.cpp -o boost 

To get list of available package names (more parts of boost):

apt-cache search libboost | grep -- -dev 

Dislaimer: I assume you use Debian since that's what your GCC version reports.

Sign up to request clarification or add additional context in comments.

3 Comments

It works! But how should I do if I want to install the latest version of Boost(1.39)? Because what I really want to use is Boost.Asio library. :)
If you're using lenny you can use 'libboost1.35-dev', if you're using squeeze, there's 'libboost1.38-dev'. There's also the standalone version of asio, 'libasio-dev'. The versions in lenny are pretty old though.
linked libraries should be at the end of the compile should they not? Therefore it would be: g++ boost.cpp -o boost -lboost_regex
8

I found the problem! g++ command problem! :( sorry. If I move back -l option to the end of the line, it does not complain more :)

Comments

2

Your

-L./boost/lib 

seems suspect. When you build Boost, the libraries are put into stage/lib directory. Also, the default build process does not create static libraries, so you probably better take -static off the command line. If that does not fix the problem, I suggest you follow the steps at my blog to troubleshoot this.

3 Comments

After I compiled regex separately and use its result, just same error. g++ -I./boost_1_39_0 ./boost_1_39_0/libs/regex/build/gcc/libboost_regex-gcc-1_38.a -o test_boost2 test_boost2.cc
g++ -I./boost_1_39_0 -o test_boost2 test_boost2.cc ./boost_1_39_0/libs/regex/build/gcc/libboost_regex-gcc-1_38.a ^ this works!!!!!!
This is because for static libraries, the position you specify them on the linker's command line matters. Again, see the blog post.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.