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

C++ 如何包含/链接boost::regex库?,c++,boost,linker,boost-regex,C++,Boost,Linker,Boost Regex,我正在尝试使用boost正则表达式库,但无法编译我的程序。所有涉及类似错误消息的相关问题似乎都与regex库未正确链接的情况有关。我想我是在连接它,但也许我在做一些愚蠢的事情。。。也许有人看到了我的错误? 这是我的测试程序: // ~/workspace/test/test.cpp #include <string> #include <iostream> #include <boost/regex.hpp> int main(){ std::strin

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

// ~/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;   
}
然而,我在运行时收到了一条非常难看的错误消息

~/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
~/workspace/test$g++-L/usr/lib/i386 linux gnu/-lboost\u regex test.cpp
/tmp/cc1yDf0v.o:在函数'bool boost::regex_match('uuuu gnu_cxx::'uuu normal_迭代器,'uuu gnu_cxx:::'uu normal_迭代器,boost::match_results&,boost::basic_regex const&,boost::regex_常量::'u match_flags'):
测试。cpp:(.text.)[[ZN5Boost11 regex_Matchin 9___gnu cxx17____正常_迭代器PkCSSeesains_9 Sub_匹配5_Eecns_12 regex_Traitsics_16cpp_regex_Traitsiceeebt_SD_RNS_13匹配_结果Isd_Eeerkns_11基本_regex__12__12_12_匹配标志]+0x4;未定义的参考:未定义的匹配::perl::匹配
/tmp/cc1yDf0v.o:在函数“boost::basic_regex::assign(char const*,char const*,unsigned int)”中:
test.cpp:(.text."zn5boost11基本规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则规则
/tmp/cc1yDf0v.o:在函数'boost::re_detail::perl_matcher::perl_matcher('uuu gnu_cxx::'uuu normal_迭代器,'uuu gnu_cxx:::'uu normal_迭代器,boost::match_结果&,boost::基本正则表达式常量&,boost::regex常量::'u match_标志,'uu gnu cxx:'uu normal迭代器):
test.cpp:(.text.)_[[ZN5boost9re\u detail12perl\u matcherIN9\u gnu\u cxx17\u normal\u iteratorIPKCSeesains\u sub\u EENS\u regex\u traitsIcNS\u 16cpp\u regex\u traitsiceees6\u RNS\u 13 match\u结果为6\u EERKNS\u基本\u regexics\u constants12\u match\u flag6\u se6]+0xcd::未定义的匹配]:未定义的细节(boost::basic_regex const&,boost::regex_constants::_match_flags)'
collect2:错误:ld返回了1个退出状态
标题似乎找到了,对吗?gcc怎么知道
boost::re_detail::perl_matcher
boost::basic_regex

我不知道问题出在哪里,也许是链接的问题?
提前感谢!

您只需确保该库位于您自己的资源之后

g++ -L/usr/lib/i386-linux-gnu/ test.cpp -lboost_regex 
这是常见问题解答:


可能还有很多。我想这是(可悲的)与领土相关的一种成年礼:(

如果您使用
sudo apt get install libboost all dev
安装了libboost all dev,请尝试在不使用
-L
选项的情况下编译它。谢谢您的提示,但是使用
g++-lboost\u regex test.cpp
编译不会改变任何东西——同样的错误。但是,这表明我的
-L
选项没有成功uess.在你的-L路径上去掉尾随斜杠。我想,我应该提到它确实是用
g++test.cpp/usr/lib/i386 linux gnu/libboost_regex.a进行编译的。但是为什么原始命令不起作用呢?gcc自动选择
。所以
而不是
。a
?或者这是胡说八道吗?你是ri嗯!谢谢!谁会想到这就是原因?!:)
g++ -L/usr/lib/i386-linux-gnu/ test.cpp -lboost_regex