gpt4 book ai didi

c++ - 如何包含/链接 boost::regex 库?

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

我正在尝试使用 boost 正则表达式库,但无法编译我的程序。所有涉及类似错误消息的相关问题似乎都与正则表达式库未正确链接的情况有关。我认为我正在链接它,但也许我在做一些愚蠢的事情......也许有人看到我的错误?
这是我的测试程序:

// ~/workspace/test/test.cpp
#include <string>
#include <iostream>
#include <boost/regex.hpp>
int main(){
std::string s("abaab");
boost::regex r("[ab]*");
std::cout << boost::regex_match(s,r) << std::endl;
}

这是 boost 二进制文件(我通过 Xubuntu 上的 synaptic 安装它们):

  /usr/lib/i386-linux-gnu$ ls libboost_regex* -al
-rw-r--r-- 1 root root 2169582 Okt 13 05:14 libboost_regex.a
lrwxrwxrwx 1 root root 24 Okt 13 05:13 libboost_regex.so -> libboost_regex.so.1.53.0
-rw-r--r-- 1 root root 1002568 Okt 13 05:14 libboost_regex.so.1.53.0

尽管如此,我在运行时收到了一条非常丑陋的错误消息

~/workspace/test$ g++ -L/usr/lib/i386-linux-gnu/ -lboost_regex test.cpp  

完整消息如下:

  ~/workspace/test$ g++ -L/usr/lib/i386-linux-gnu/ -lboost_regex test.cpp 
/tmp/cc1yDf0v.o: In function `bool boost::regex_match<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(__gnu_cxx::__normal_iterator<char const*, std::string>, __gnu_cxx::__normal_iterator<char const*, std::string>, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)':
test.cpp:(.text._ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE[_ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE]+0x4c): undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match()'
/tmp/cc1yDf0v.o: In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
test.cpp:(.text._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j]+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/cc1yDf0v.o: In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::perl_matcher(__gnu_cxx::__normal_iterator<char const*, std::string>, __gnu_cxx::__normal_iterator<char const*, std::string>, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, __gnu_cxx::__normal_iterator<char const*, std::string>)':
test.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC2ES6_S6_RNS_13match_resultsIS6_S9_EERKNS_11basic_regexIcSD_EENS_15regex_constants12_match_flagsES6_[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC5ES6_S6_RNS_13match_resultsIS6_S9_EERKNS_11basic_regexIcSD_EENS_15regex_constants12_match_flagsES6_]+0xcd): undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)'
collect2: error: ld returned 1 exit status

header 好像找到了吧? gcc 还怎么知道 boost::re_detail::perl_matcherboost::basic_regex
我不知道可能是什么问题。也许这毕竟是链接?提前致谢!

最佳答案

您只需要确保库位于您自己的源之后。

g++ -L/usr/lib/i386-linux-gnu/ test.cpp -lboost_regex 

这是一个常见问题:

可能还有很多。我想(遗憾的是)这是与领土相关的那些仪式之一:(

关于c++ - 如何包含/链接 boost::regex 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22732848/

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