将 Boost Regex 库与 GCC 5.2.0 链接时出现未定义的引用错误

undefined reference errors when linking the boost regex library with gcc 5.2.0

提问人:Emlyn Jose 提问时间:4/8/2016 最后编辑:einpoklumEmlyn Jose 更新时间:1/27/2017 访问量:802

问:

以下代码使用 GCC 4.9.2 成功编译,没有任何警告:

#include <memory>
#include <string>
#include <boost/regex.hpp>

int main()
{
boost::wregex wregex;
std::wstring text = L"foobar";
return boost::regex_match(text, wregex);
}

命令运行是:

g++ -I /opt/boost/x86_64/1.60.0/include -L /opt/boost/x86_64/1.60.0/lib test.cc  -lboost_regex

但在与 GCC 5.2.0 一起使用时显示以下错误:

/tmp/cc3L5WWc.o: In function `bool boost::regex_match<__gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > > > >, wchar_t, boost::regex_traits<wchar_t, boost::cpp_regex_traits<wchar_t> > >(__gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, __gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, boost::match_results<__gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > > > > >&, boost::basic_regex<wchar_t, boost::regex_traits<wchar_t, boost::cpp_regex_traits<wchar_t> > > const&, boost::regex_constants::_match_flags)': test.cc:(.text._ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKwNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEEEEESaINS_9sub_matchISB_EEEwNS_12regex_traitsIwNS_16cpp_regex_traitsIwEEEEEEbT_SJ_RNS_13match_resultsISJ_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE[_ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKwNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEEEEESaINS_9sub_matchISB_EEEwNS_12regex_traitsIwNS_16cpp_regex_traitsIwEEEEEEbT_SJ_RNS_13match_resultsISJ_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE]+0x80): undefined reference to `boost::re_detail_106000::perl_matcher<__gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > > > >, boost::regex_traits<wchar_t, boost::cpp_regex_traits<wchar_t> > >::match()'/tmp/cc3L5WWc.o: In function `boost::re_detail_106000::perl_matcher<__gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > > > >, boost::regex_traits<wchar_t, boost::cpp_regex_traits<wchar_t> > >::perl_matcher(__gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, __gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, boost::match_results<__gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > > > > >&, boost::basic_regex<wchar_t, boost::regex_traits<wchar_t, boost::cpp_regex_traits<wchar_t> > > const&, boost::regex_constants::_match_flags, __gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >)': 
 test.cc:(.text._ZN5boost16re_detail_10600012perl_matcherIN9__gnu_cxx17__normal_iteratorIPKwNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEEEEESaINS_9sub_matchISC_EEENS_12regex_traitsIwNS_16cpp_regex_traitsIwEEEEEC2ESC_SC_RNS_13match_resultsISC_SF_EERKNS_11basic_regexIwSJ_EENS_15regex_constants12_match_flagsESC_[_ZN5boost16re_detail_10600012perl_matcherIN9__gnu_cxx17__normal_iteratorIPKwNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEEEEESaINS_9sub_matchISC_EEENS_12regex_traitsIwNS_16cpp_regex_traitsIwEEEEEC5ESC_SC_RNS_13match_resultsISC_SF_EERKNS_11basic_regexIwSJ_EENS_15regex_constants12_match_flagsESC_]+0x116): undefined reference to `boost::re_detail_106000::perl_matcher<__gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > > > >, boost::regex_traits<wchar_t, boost::cpp_regex_traits<wchar_t> > >::construct_init(boost::basic_regex<wchar_t, boost::regex_traits<wchar_t, boost::cpp_regex_traits<wchar_t> > > const&, boost::regex_constants::_match_flags)'
 collect2: error: ld returned 1 exit status
g++ 链接器错误 undefined-reference boost-regex

评论

0赞 KimKulling 4/8/2016
您是否尝试过添加选项 -std=c++14(我在这里找到了它:solarianprogrammer.com/2016/03/06/... )
0赞 Emlyn Jose 4/8/2016
是的,我检查了这个页面。但这没有用。谢谢!
0赞 KimKulling 4/8/2016
好的,您的 boost 版本 (1.60) ABI 是否与 gcc 5.2.0 兼容,或者您是否必须安装新的 boost 版本?
0赞 Emlyn Jose 4/11/2016
对不起,我不熟悉 ABI。我应该如何检查我的 boost 版本是否与 gcc 5.2.0 兼容?
0赞 Emlyn Jose 4/11/2016
我阅读了这个 ABI 并通过添加如下宏来构建 boost 1.60.0: ./bootstrap.sh ./bjam -j4 define=_GLIBCXX_USE_CXX11_ABI=0 stage 但即便如此,也没有变化。这是正确的做法吗?顺便说一句,我通过运行g++手动尝试了这个,如下所示,它没有失败: g++ -I /opt/boost/x86_64/1.60.0/include -L /opt/boost/x86_64/1.60.0/lib test.cc -D_GLIBCXX_USE_CXX11_ABI=0 -lboost_regex在构建提升时要进行任何其他更改?

答: 暂无答案