【问题标题】:how to debug in c++ [duplicate]如何在c ++中调试[重复]
【发布时间】:2015-02-15 20:02:59
【问题描述】:

我是 C++ 新手;我有一个简单的代码来检查一个字符串一个字符串是否是回文:

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

using namespace std;
using namespace boost;

string PalindromeTwo(string str);

int main(){

  cout << PalindromeTwo("A war at Tarawa!");

}

string PalindromeTwo(string str) {

  int head = 0;
  int tail = str.size() - 1;
  boost::regex e("^[a-zA-Z]*$");
  stringstream stream;
  while(head <= tail){
    stream << str.at(head);
    string strchar = stream.str();
    stream.str(string());
    if(!boost::regex_match(strchar,e))
      head++;
    stream << str.at(tail);
    string strchar1 = stream.str();
    stream.str(string());
    if(!boost::regex_match(strchar1,e))
      tail--;
    if(strchar.compare(strchar1) != 0)
      return "false";
  }
  return "true";

}

我编译了这段代码:g++ -o palin palin.cpp

然后我得到了这么长的错误,我认为它只是一个小错误,但是c++给了我这么长的错误,我无法从中找到任何线索,任何有经验的c++开发人员可以告诉我如何调试c++ 有这么长的错误?

/tmp/ccEwKTX0.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)':
palin.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/ccEwKTX0.o: In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
palin.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/ccEwKTX0.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>)':
palin.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_]+0x113): 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

【问题讨论】:

  • 问题内容与标题有什么关系?
  • @vsoftco 你能告诉我你从错误的哪一行知道这是一个链接器错误吗?我正在学习一个方法,谢谢。
  • @msn error: ld returned 1 exit statusld 是链接器的名称。每次你在一长串错误列表的末尾看到它,这意味着你有一个链接器错误。如果你有一个编译时错误,那么链接器甚至不会被调用,所以在你的情况下,程序肯定通过了编译阶段。

标签: c++ debugging


【解决方案1】:

顺便说一句,这不是编译错误,而是链接器错误,您没有与boostregex 库链接(与大多数Boost 不同,regex 是一个编译库,因此您需要链接它)。更好的是,放弃 boost::regex 并使用 std::regex (#include &lt;regex&gt;),它是 C++11 中标准库的一部分(使用 -std=c++11 编译),所有痛苦都会消失。

【讨论】:

  • 这是我之前做的,但是从this的帖子,说不支持&lt;regex&gt;,所以我尝试使用boost。
  • @msn,确实,regex used 不在标准库中实现,但现在(我认为从 g++4.9 开始)它是。 clang++ 也为它提供了全面的支持。只需测试它,看看它是否有效。如果你真的想使用Boost(或者有一个旧的编译器),那么确保你链接到库,比如-l/path/to/libregex。更好的是,尝试将您的编译器更新为兼容 C++11 的编译器。
  • 我在运行时得到terminate called after throwing an instance of 'std::regex_error',我的正则表达式模式错了吗?
  • 你用的是什么编译器? g++ --version 可能它比较旧,所以使用 Boost 但与 regex 库链接,g++ -lboost_regex -o palin palin.cpp
  • 我在使用 boost 时遇到了另一个错误 /usr/bin/ld: cannot find -lboost_regex,我的 g++ 版本是 4.8.2
猜你喜欢
  • 2011-04-14
  • 2018-01-14
  • 2017-05-07
  • 2016-07-31
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-13
相关资源
最近更新 更多