【问题标题】:boost linker errors -regular expressions - c++提升链接器错误 - 正则表达式 - C++
【发布时间】:2009-11-21 06:12:50
【问题描述】:

您好,我正在尝试在 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:我正在使用 devcpp ide 并且我从 www.devpaks.org

安装了 boost

【问题讨论】:

    标签: c++ boost linker


    【解决方案1】:

    如果您从官方源代码分发中获取 Boost 库,请确保您使用 bjam 构建二进制文件(许多 boost 库仅包含标头,不需要您这样做;正则表达式库需要建)。看起来这些库是随 devpack 版本分发的,所以这对您来说应该不是问题。

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

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

    对于 Visual C++,您只需将 lib 目录添加到项目“链接器 > 常规”属性页上的“附加库目录”即可。

    使用 Visual C++ 链接器,您无需明确告诉链接器要拉入哪些特定的 Boost 库; Boost 标头包含自动拉入正确标头的指令。对于 g++,您需要指定库,因此您需要查找正则表达式库的名称并添加适当的编译器指令以链接到它(-llibname(这是一个小写的 L))。

    【讨论】:

    • 使用 Visual Studio 可以支持自动链接,但 gcc 不支持。
    • @gf:感谢您的提醒;我不知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 2011-07-29
    • 2011-06-26
    • 1970-01-01
    相关资源
    最近更新 更多