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

标签 c++ boost linker boost-regex

我正在尝试使用 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/

相关文章:

c++ - 为什么我的 Gdiplus::Bitmap 不能保存文件

c++ - 如何在 C++ 中使用单个 APDU 发送和获取数据?

c++ - 转换为递归函数?

c++ - 如何禁用 TBB 的自动链接

函数 slqastrt、sqlacall、sqlastop 上的 Ubuntu DB2 gcc prep 错误

c++ - C++ 的 Hello World 错误

c++ - 如何为涉及对象成员、间接和强制转换的排序算法实现 lambda 函数?

c++ - 如何使用构造函数外部分配的 Boost 序列化大型 malloc'ed float 数组?

c++ - 基于boost的单元测试套件编译缓慢

cmake - 将 sfml 与 cmake 链接 (Windows MinGW)