使用boost regex库有什么问题?

时间:2014-02-18 19:06:26

标签: c++ regex boost

这是代码;

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

using boost::regex;
using boost::regex_match;

int main(int argc, char **argv) {
    std::string input;
    regex gdreg(R"(g[a-z]+d)");
    while(true)
    {
        std::cout << "Give me a word: ";
        std::cin >> input;
        if(input == "q") {
            break;
        }
        if(regex_match(input, gdreg))
            std::cout << "It's a GD word!" << std::endl;
        else
            std::cout << "Nooooooo!" << std::endl;
    }
}

这是错误消息:

g++ x.cpp -std=c++11 -o x
/tmp/ccJ7zdHo.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)':
x.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]+0x77): 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/ccJ7zdHo.o: In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
x.cpp:(.text._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j]+0x2a): 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/ccJ7zdHo.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>)':
x.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_]+0x10b): 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

不使用原始字符串无法解决问题。

3 个答案:

答案 0 :(得分:12)

Dunno ....也许undefined reference to 'boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)'

阅读此错误消息肯定不是很有趣,但它包含您需要的所有信息 - 只要您了解reference told等字词的含义,能够将消息结构化为部分:

  • ld returned 1 exit status:链接器不开心
  • /tmp/.... .o:我正在创建一个目标文件。
  • 但在函数boost::basic_regex<...>::assign(上下文)
  • 未明确引用boost::cpp_regex_traits<...>::do_assign(...)(问题)

链接器没有看到从哪里获取do_assign功能代码。这通常是由于忘记添加包含代码的库而引起的,例如-lboost_regex

也是CFR。 https://stackoverflow.com/a/559191/6610

答案 1 :(得分:5)

只需将-lboost_regex添加到编译行即可解决:

这有效: $ g ++ main.cc -o main -lboost_regex

来源:http://ubuntuforums.org/archive/index.php/t-1674703.html

对于我的情况,这个解决方案有效:)

答案 2 :(得分:0)

就我而言,我必须将//index.html //Assuming you are using jQuery <div id="locationData"></div> <script> // WHEN HTML IS FULLY LOADED SENDS A GET REQUEST TO YOUR SERVER TO FETCH // THE DATA $( document ).ready(function() { $.ajax({url: "http://localhost:3000/", success: function(result){ // PUTS THE DATA IN THE DIV WITH ID : locationData $("#locationData").html(result); }}); }); <script> 添加到该目录中的Jamfile中。

很抱歉您这么久以前问过这个问题,但没有找到答案,但我希望这会有所帮助!