3

I am trying to use the boost regex library but I can't manage to compile my program. All related questions which involve similar error messages seem to relate to situations where regex library wasn't properly linked. I think that I am linking it, but maybe I am doing something stupid... Perhaps someone sees my mistake?
Here is my test program:

// ~/workspace/test/test.cpp
#include <string>
#include <iostream>
#include <boost/regex.hpp>
int main(){
   std::string s("abaab");
   boost::regex r("[ab]*");
   std::cout << boost::regex_match(s,r) << std::endl;   
}

Here are the boost binaries (I installed them via synaptic on Xubuntu):

  /usr/lib/i386-linux-gnu$ ls libboost_regex* -al
  -rw-r--r-- 1 root root 2169582 Okt 13 05:14 libboost_regex.a
  lrwxrwxrwx 1 root root      24 Okt 13 05:13 libboost_regex.so -> libboost_regex.so.1.53.0
  -rw-r--r-- 1 root root 1002568 Okt 13 05:14 libboost_regex.so.1.53.0  

Nevertheless I'm getting quite an ugly error message when running

~/workspace/test$ g++ -L/usr/lib/i386-linux-gnu/ -lboost_regex test.cpp  

Here is the complete message:

  ~/workspace/test$ g++ -L/usr/lib/i386-linux-gnu/ -lboost_regex test.cpp 
  /tmp/cc1yDf0v.o: In function `bool boost::regex_match<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(__gnu_cxx::__normal_iterator<char const*, std::string>, __gnu_cxx::__normal_iterator<char const*, std::string>, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)':
  test.cpp:(.text._ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE[_ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE]+0x4c): undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match()'
  /tmp/cc1yDf0v.o: In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
  test.cpp:(.text._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j]+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)'
  /tmp/cc1yDf0v.o: In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::perl_matcher(__gnu_cxx::__normal_iterator<char const*, std::string>, __gnu_cxx::__normal_iterator<char const*, std::string>, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, __gnu_cxx::__normal_iterator<char const*, std::string>)':
  test.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC2ES6_S6_RNS_13match_resultsIS6_S9_EERKNS_11basic_regexIcSD_EENS_15regex_constants12_match_flagsES6_[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC5ES6_S6_RNS_13match_resultsIS6_S9_EERKNS_11basic_regexIcSD_EENS_15regex_constants12_match_flagsES6_]+0xcd): undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, 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)'
  collect2: error: ld returned 1 exit status

The header seems to be found, right? How else could gcc know about boost::re_detail::perl_matcher or boost::basic_regex?
I have no clue what the problem might be. Maybe it is the linking after all? Thanks in advance!

5
  • If you have installed the libboost-all-dev by using sudo apt-get install libboost-all-dev, try to compile it without -L option. Commented Mar 29, 2014 at 15:46
  • Thanks for your hint, but compiling with g++ -lboost_regex test.cpp doesn't change anything -- same errors. However, this shows that my -L option doesn't succeed I guess. Commented Mar 29, 2014 at 16:39
  • Drop the trailing slash on your -L path. Commented Mar 29, 2014 at 16:59
  • I think, I should mention that it does compile with g++ test.cpp /usr/lib/i386-linux-gnu/libboost_regex.a. But why doesn't the original command work? Commented Mar 29, 2014 at 17:21
  • Can there be some problem with gcc automatically selecting the .so instead of the .a? Or is this nonsense? Commented Mar 29, 2014 at 17:27

1 Answer 1

2

You just needed to make sure the library comes after your own sources.

g++ -L/usr/lib/i386-linux-gnu/ test.cpp -lboost_regex 

This is a FAQ:

And probably quite a few more. I guess it's (sadly) one of those rites-of-passage that goes with the territory :(

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.