tags:

views:

162

answers:

1

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

+5  A: 

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)).

James McNellis
With Visual Studio you have auto-linking support, gcc doesn't support it though.
Georg Fritzsche
@gf: Thanks for the heads-up; I had no idea.
James McNellis