Compiler error on using Boost --- config problem ?

I'm trying to use boost (debian lenny, boost installed via debian repos)
I wrote a toy program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <boost/regex.hpp>
#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main(){
  // string s;
  //vector<string> store;

  // while(getline(cin s));

  boost::regex reg("A.*)");
  bool b1 = boost::regex_match("This expression A is not.", reg);
  bool b2 = boost::regex_match("A  expression is.", reg);

  cout << "First" << b1 <<endl;
  cout << "Second" << b2 <<endl;
  
  return 0;
}


On doing
g++ main.cpp

I get a massive compiler error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/tmp/cckK8XKs.o: In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
split.cpp:(.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)]+0x27): undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)'
/tmp/cckK8XKs.o: In function `boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::perl_matcher(char const*, char const*, boost::match_results<char const*, std::allocator<boost::sub_match<char const*> > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, char const*)':
split.cpp:(.text._ZN5boost9re_detail12perl_matcherIPKcSaINS_9sub_matchIS3_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC1ES3_S3_RNS_13match_resultsIS3_S6_EERKNS_11basic_regexIcSA_EENS_15regex_constants12_match_flagsES3_[boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::perl_matcher(char const*, char const*, boost::match_results<char const*, std::allocator<boost::sub_match<char const*> > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, char const*)]+0xd5): undefined reference to `boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)'
/tmp/cckK8XKs.o: In function `bool boost::regex_match<char const*, std::allocator<boost::sub_match<char const*> >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(char const*, char const*, boost::match_results<char const*, std::allocator<boost::sub_match<char const*> > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)':
split.cpp:(.text._ZN5boost11regex_matchIPKcSaINS_9sub_matchIS2_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SA_RNS_13match_resultsISA_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE[bool boost::regex_match<char const*, std::allocator<boost::sub_match<char const*> >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(char const*, char const*, boost::match_results<char const*, std::allocator<boost::sub_match<char const*> > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)]+0x84): undefined reference to `boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match()'
/tmp/cckK8XKs.o: In function `boost::cpp_regex_traits<char>::transform(char const*, char const*) const':
split.cpp:(.text._ZNK5boost16cpp_regex_traitsIcE9transformEPKcS3_[boost::cpp_regex_traits<char>::transform(char const*, char const*) const]+0x35): undefined reference to `boost::re_detail::cpp_regex_traits_implementation<char>::transform(char const*, char const*) const'
/tmp/cckK8XKs.o: In function `boost::cpp_regex_traits<char>::transform_primary(char const*, char const*) const':
split.cpp:(.text._ZNK5boost16cpp_regex_traitsIcE17transform_primaryEPKcS3_[boost::cpp_regex_traits<char>::transform_primary(char const*, char const*) const]+0x35): undefined reference to `boost::re_detail::cpp_regex_traits_implementation<char>::transform_primary(char const*, char const*) const'
/tmp/cckK8XKs.o: In function `boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match_match()':
split.cpp:(.text._ZN5boost9re_detail12perl_matcherIPKcSaINS_9sub_matchIS3_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE11match_matchEv[boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match_match()]+0x13c): undefined reference to `boost::match_results<char const*, std::allocator<boost::sub_match<char const*> > >::maybe_assign(boost::match_results<char const*, std::allocator<boost::sub_match<char const*> > > const&)'
/tmp/cckK8XKs.o: In function `boost::re_detail::cpp_regex_traits_implementation<char>::error_string(boost::regex_constants::error_type) const':
split.cpp:(.text._ZNK5boost9re_detail31cpp_regex_traits_implementationIcE12error_stringENS_15regex_constants10error_typeE[boost::re_detail::cpp_regex_traits_implementation<char>::error_string(boost::regex_constants::error_type) const]+0x91): undefined reference to `boost::re_detail::get_default_error_string(boost::regex_constants::error_type)'
split.cpp:(.text._ZNK5boost9re_detail31cpp_regex_traits_implementationIcE12error_stringENS_15regex_constants10error_typeE[boost::re_detail::cpp_regex_traits_implementation<char>::error_string(boost::regex_constants::error_type) const]+0x122): undefined reference to `boost::re_detail::get_default_error_string(boost::regex_constants::error_type)'
/tmp/cckK8XKs.o: In function `void boost::re_detail::raise_error<boost::regex_traits_wrapper<boost::regex_traits<char, boost::cpp_regex_traits<char> > > >(boost::regex_traits_wrapper<boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::error_type)':
split.cpp:(.text._ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE[void boost::re_detail::raise_error<boost::regex_traits_wrapper<boost::regex_traits<char, boost::cpp_regex_traits<char> > > >(boost::regex_traits_wrapper<boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::error_type)]+0x68): undefined reference to `boost::re_detail::raise_runtime_error(std::runtime_error const&)'
/tmp/cckK8XKs.o: In function `boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::extend_stack()':
split.cpp:(.text._ZN5boost9re_detail12perl_matcherIPKcSaINS_9sub_matchIS3_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE12extend_stackEv[boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::extend_stack()]+0x36): undefined reference to `boost::re_detail::get_mem_block()'
/tmp/cckK8XKs.o: In function `boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::unwind_extra_block(bool)':
split.cpp:(.text._ZN5boost9re_detail12perl_matcherIPKcSaINS_9sub_matchIS3_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE18unwind_extra_blockEb[boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::unwind_extra_block(bool)]+0x62): undefined reference to `boost::re_detail::put_mem_block(void*)'
collect2: ld returned 1 exit status 


Is it some config error ? g++ IS finding the boost header files.
I just saw a thread the other day with similar issues, although not related to boost. Here it is:
http://cplusplus.com/forum/unices/20120/

Basically, I'm guessing boost::regex is a library that isn't JUST a header file. In such a case, you'll need to link with the library. Try something like:
g++ -lboost_regex main.cpp

If g++ does not find the library, you'll need to tell it where to look. For more information:
man gcc

Pay particular attention to the -L and -l options.

OK, I got it to compile using
g++ main.cpp -lboost_regex

Though I dont know why it is compiling --- can anyone explain what -lboost_regex is doing, and how to compile with boost in general ?
Moorecm, I saw your reply after posting !
I'm confused on how g++ is finding a library called "boost_regex".
Should it not require something like "/usr/lib/libboost_regex.so.1.35.0" ?
There is probably a symbolic link named libboost_regex.so somewhere in the default library path that points to the specific version. I think that is done to try and decouple the version number from the library. Additionally, since it is in the default path you do not have to explicitly tell g++ where to look.
Ah, thanks!
How do I find out what these symbolic names are ? Eg., if I want to use some other library from Boost or elsewhere ?
Your best bet is to check boost.org for documentation. Alternatively, I suppose you could try to use something like find /usr/lib -name libboost\*.so -exec nm {} | grep -H SYMBOL \; to locate the shared objects that contain SYMBOL. (I think I just let some of my nerd show with that last sentence...)
Thanks! I shall check out Boost.org and also decipher what you wrote :)

A very basic question, does g++ interpret "boost_regex" to mean "libboost_regex.so" ?
Yes. The manual pages for gcc will go into further detail. ;)
Topic archived. No new replies allowed.