【问题标题】:linker error in boost regex升压正则表达式中的链接器错误
【发布时间】:2012-09-16 11:27:54
【问题描述】:

我想在 boost lib 中学习一些关于正则表达式的知识,我尝试编译这个简单的示例代码:

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

我使用:g++ -std=c++0x -I /usr/lib/boost/include -L /usr/lib/boost/lib -lboost_regex test_regex.cpp

但是 g++ 给我看:

/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_

还有更多...

谁能帮帮我?

【问题讨论】:

标签: c++ regex boost linker


【解决方案1】:

三件事:

  1. Boost.Regex 库很可能称为libboost_regex<b>-mt</b>
  2. 除非您知道 Boost 库是使用 C++11 支持编译的,否则您应该删除
    -std=c++0x 选项。
  3. 您应该始终将 LIBS 放在末尾,因为 GNU ld 按照目标文件和 LIBS 在命令行中出现的顺序来解析符号。

试试:

g++ -I /usr/lib/boost/include -L /usr/lib/boost/lib test_regex.cpp -lboost_regex-mt

【讨论】:

  • 我知道 C++11 支持正则表达式,但我使用了 -std=c++0x,因为我在代码自动类型中使用了。我试过你的版本,但 g++ 告诉我:/usr/bin/ld: cannot find -lboost_regex-mt collect2: ld returned 1 exit status 还有其他建议吗?
  • @user1518451:在/usr/lib/boost/lib 中查找以libboost_regex 开头并以.a 结尾的文件。例如,在我的 Boost 副本中,它是 libboost_regex-mt.a。 Boost.Build 以库的名称对其他编译选项进行编码,因此 -lboost_regex 可能是正确的,或者可能是其他的东西。
  • 我试过了:g++ -I /usr/lib/boost/include -L /usr/lib/boost/lib test_regex.cpp -lboost_regex,一切正常,但是当我启动程序时,我看到:./a.out: error while loading shared libraries: libboost_regex.so.1.50.0: cannot open shared object file: No such file or directory
  • 好的,我找到了解决方案:export LD_LIBRARY_PATH="/usr/lib/boost/lib"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-26
  • 2013-04-06
  • 1970-01-01
  • 2011-07-29
  • 2017-01-26
  • 2014-07-01
相关资源
最近更新 更多