代码之家  ›  专栏  ›  技术社区  ›  Dr Deo

增强链接器错误-正则表达式-C++

  •  0
  • Dr Deo  · 技术社区  · 15 年前

    嗨,我正在试着在Boost库中编译一个简单的程序,但我一直在出错。

    #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;
          }
       }
    }
    

    但我一直都有链接错误

      [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)' 
    

    我怎么解决这个问题?
    PS: AM使用 DEVCPIDE 我安装了Boost www. DePaks.Org

    1 回复  |  直到 15 年前
        1
  •  4
  •   James McNellis    15 年前

    如果您从官方的源发行版获得boost库,请确保使用bjam构建二进制文件(很多boost库只是头文件,不需要这样做;需要构建regex库)。看起来库是随devpack发行版发行的,所以这对您来说应该不是问题。

    确保在链接器路径中有boost lib目录。使用gcc,可以使用-l编译器标志:

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

    或者对于VisualC++,您可以将LIB目录添加到项目的“链接器GT;通用”属性页中的“附加库目录”中。

    使用VisualC++链接器,您不需要显式地告诉链接器哪些特定的升压库可以拉入;升压头包含指令,以便正确地将正确的指针拉入。对于g++,您需要指定库,因此您需要查找regex库的名称,并添加相应的编译器指令来链接它。( -llibname (小写L))。