【问题标题】:Compile and regex in c++ using eclipse [duplicate]使用eclipse在c ++中编译和正则表达式[重复]
【发布时间】:2013-05-18 16:26:19
【问题描述】:

所以我的教授给了我一个在 c++ 中使用正则表达式的工作。

所以我尝试在 eclipse 中编写我的代码(我使用的是 linux (ubuntu 12.04))。

所以我拿了代码:

   // regex_search example
#include <iostream>
#include <string>
#include <regex>

int main ()
{
  std::string s ("this subject has a submarine as a subsequence");
  std::smatch m;
  std::regex e ("\\b(sub)([^ ]*)");   // matches words beginning by "sub"

  std::cout << "Target sequence: " << s << std::endl;
  std::cout << "Regular expression: /\\b(sub)([^ ]*)/" << std::endl;
  std::cout << "The following matches and submatches were found:" << std::endl;

  while (std::regex_search (s,m,e)) {
    for (auto x:m) std::cout << x << " ";
    std::cout << std::endl;
    s = m.suffix().str();
  }

  return 0;
}

如您所见,这是一个使用正则表达式的简单代码。

所以我尝试构建它并且 eclipse 给了我一个错误:

Type 'smatch' could not be resolved

还有:

Type 'std::regex' could not be resolved

有什么问题?

我尝试添加标志 -std=c++0x 在合适的位置(properties->c/c++ build ->Miscellaneous),什么也没有发生。

也许我做错了?

也许我必须像在 pthread 中一样添加指向库的链接?

【问题讨论】:

  • 你运行的是什么版本的 GCC?
  • 你可能在那个版本的 Ubuntu 上使用 gcc 4.7.2(或者之前的那个)?该版本未实现正则表达式。 (不确定他们现在已经走了多远。我看到的任何活动的最后证据是 1 月份邮件列表中的一些模糊的 cmets。
  • 考虑Boost.Regex。问问你的教授,他们应该知道他们打算让你使用什么。
  • 要么将库更改为支持更好的库,要么使用Boost regex(应该非常兼容)。
  • 我已经开始使用clang 它是libc++ 库,只是因为缺乏对 GCC 的 C++11 支持。

标签: c++ regex eclipse compiler-errors eclipse-cdt


【解决方案1】:

我的设置:

操作系统:Ubuntu 14.04 编译器:GCC 4.8.2 IDE:Eclipse 3.8.1

问题:

我已将正则表达式 (#include &lt;regex&gt;) 包含到我的头文件中,但我的 IDE 一直抱怨无法解决“某事”类型。我已经尝试使用诸如 std:: 和 std::tr1 之类的命名空间,就像谷歌的人所建议的那样,但没有成功。

解决方案:

解决方案与问题本身一样简单。如果您查看 usr/include/c++/4.8,您会注意到根文件夹中的正则表达式文件有点像存根。但是 /tr1 文件夹中还有另一个正则表达式文件。

诀窍是,不要写#include &lt;regex&gt;,而应该写#include &lt;tr1/regex&gt;

之后,您可以通过 std::tr1:: 命名空间使用正则表达式。

例如像这样:std::tr1::smatch

希望有帮助!

©http://antaumus.blogspot.com/2014/08/how-to-use-regex-with-gcc-482.html

【讨论】:

    【解决方案2】:

    正则表达式添加在std::tr1 命名空间下。 请将您的正则表达式的对象声明为std::tr1::regex,它应该可以工作

    【讨论】:

    • 感谢 DevZer0,但我真的不明白除了字体更改之外您所做的实际更改是什么?
    【解决方案3】:

    flag 还不够,你还要在“C/C++ General -> Paths and Symbols -> Symbols -> GNU C++”中添加预处理器符号“____GXX_EXPERIMENTAL_CXX0X____”。 详情请参考以下问题: Eclipse CDT C++11/C++0x support。 它也在 Eclipse wiki 中: http://wiki.eclipse.org/CDT/User/FAQ#CDT_does_not_recognize_C.2B.2B11_features。 第二个链接还包含一个附加链接,指向描述“隐藏”选项的论坛页面 (http://www.eclipse.org/forums/index.php/mv/msg/373462/909018/#msg_909018)。 如果您按照链接中的说明使用“隐藏”选项配置编译器,则在再次删除“____GXX_EXPERIMENTAL_CXX0X____”标志后它仍然可以工作。至少对我来说它是这样工作的(Eclipse Juno 4.2.2 和 GCC 4.7.2)。

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-30
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多