0

hi am trying to compile a simple program in boost library but i keep getting linker errors

#include <iostream>
#include <string>
#include <boost\regex.hpp>  // Boost.Regex lib

using namespace std;

int main( ) {

   std::string s, sre;
   boost::regex re;

   while(true)
   {
      cout << "Expression: ";
      cin >> sre;
      if (sre == "quit")
      {
         break;
      }
      cout << "String:     ";
      cin >> s;

      try
      {
         // Set up the regular expression for case-insensitivity
         re.assign(sre, boost::regex_constants::icase);
      }
      catch (boost::regex_error& e)
      {
         cout << sre << " is not a valid regular expression: \""
              << e.what() << "\"" << endl;
         continue;
      }
      if (boost::regex_match(s, re))
      {
         cout << re << " matches " << s << endl;
      }
   }
}

but i keep getting linker errors

  [Linker error] undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)' 

How can i solve this?
Ps: am using devcpp ide and i installed boost from www.devpaks.org

1 Answer 1

4

If you get the Boost libraries from the official source distribution, make sure you build the binaries using bjam (a lot of the boost libraries are header-only and don't require you to do this; the regex library needs to be built). It looks like the libraries are distributed with the devpack release, so this shouldn't be a problem for you.

Make sure that you have the boost lib directory in your linker path. With gcc, you can use the -L compiler flag:

gcc [whatever you normally put here] -L/path/to/boost/lib/directory

or for Visual C++, you can just add the lib directory to the "Additional Library Directories" on the "Linker > General" property page for your project.

With the Visual C++ linker, you don't need to explicitly tell the linker which particular Boost libraries to pull in; the Boost headers contain directives to have the correct ones pulled in automatically. For g++, you need to specify the libraries, so you'll need to go look up the name of the regex library and add the appropriate compiler directive to link against it (-llibname (that's a lowercase L)).

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

1 Comment

With Visual Studio you have auto-linking support, gcc doesn't support it though.

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.