4

I would like to learn something about regex in boost lib and i try compile this simple example code:

// regex_search example
#include <iostream>
#include <string>
#include <boost/regex.hpp>

int main ()
{
  std::string s ("this subject has a submarine as a subsequence");
  boost::smatch m;
  boost::regex e ("\\b(sub)([^ ]*)");   // matches words beginning by "sub"

  std::cout << "Target sequence: " << s << std::endl;
  std::cout << "Regular expression: /\\b(sub)([^ ]*)/" << std::endl;
  std::cout << "The following matches and submatches were found:" << std::endl;

  while (boost::regex_search (s,m,e)) {
    for (auto x:m) std::cout << x << " ";
    std::cout << std::endl;
    s = m.suffix().str();
  }

  return 0;
}

I use: g++ -std=c++0x -I /usr/lib/boost/include -L /usr/lib/boost/lib -lboost_regex test_regex.cpp

but g++ show me:

/tmp/ccjni2je.o: In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
test_regex.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)]+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/ccjni2je.o: In function `bool boost::regex_search<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, char, boost::regex_traits<char, boost::cpp_regex_

and much more ...

Can anyone help me ?

user1518451
  • 1,767
  • 6
  • 23
  • 26
  • 3
    Put `-lboost_regex` at the end. See http://stackoverflow.com/questions/9966959/noobish-linker-errors-when-compiling-against-glib/9966989#9966989 for more info. – hmjd Sep 16 '12 at 11:36

1 Answers1

2

Three things:

  1. The Boost.Regex library is likely called libboost_regex-mt.
  2. Unless you know that a Boost lib was compiled with C++11 support, you should remove the
    -std=c++0x option.
  3. You should always place LIBS at the end because GNU ld resolves symbols in the order that object files and LIBS appear in the command line.

Try:

g++ -I /usr/lib/boost/include -L /usr/lib/boost/lib test_regex.cpp -lboost_regex-mt
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
  • 1
    I know that C++11 support regex but i used -std=c++0x because i used in code auto type. I tried your version but g++ show me: `/usr/bin/ld: cannot find -lboost_regex-mt collect2: ld returned 1 exit status ` Any other suggestions ? – user1518451 Sep 16 '12 at 12:51
  • @user1518451: Look in `/usr/lib/boost/lib` for a file that starts with `libboost_regex` and ends with `.a`. In my copy of Boost, for example, it is `libboost_regex-mt.a`. Boost.Build encodes other compilation options in the name of the library, so `-lboost_regex` might have been correct, or maybe it is something else. – Daniel Trebbien Sep 16 '12 at 13:03
  • I tried: `g++ -I /usr/lib/boost/include -L /usr/lib/boost/lib test_regex.cpp -lboost_regex` and everything is ok but when i start program i see: `./a.out: error while loading shared libraries: libboost_regex.so.1.50.0: cannot open shared object file: No such file or directory ` – user1518451 Sep 16 '12 at 13:08
  • 3
    OK I find the solution: `export LD_LIBRARY_PATH="/usr/lib/boost/lib"` – user1518451 Sep 16 '12 at 13:46