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 ?

1

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

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: 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.
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
OK I find the solution: export LD_LIBRARY_PATH="/usr/lib/boost/lib"

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.