gpt4 book ai didi

c++ - boost 正则表达式中的链接器错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:54:10 25 4
gpt4 key购买 nike

我想在 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_

还有更多...

谁能帮帮我?

最佳答案

三件事:

  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++ - boost 正则表达式中的链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12446471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com