【问题标题】:c++0x compiles but eclipse editor errors even with -gnu++0x discoveryc++0x 编译但即使使用 -gnu++0x 发现也会出现 Eclipse 编辑器错误
【发布时间】:2012-02-04 13:01:27
【问题描述】:

我使用一些代码来报告使用 std::chrono::high_resolution_clock ... c++0x 的一部分的任务的持续时间。

我可以使用 -gnu++0x 标志在 eclipse cdt 中成功编译 c++0x 功能。尽管编译成功,但编辑器似乎不知道 c++0x,即它显示我的代码中任何 c++0x 功能的错误。我通过在我的项目发现选项中添加 -gnu++0x 标志解决了这个问题。注意:在您进行另一次编译并重建索引之前不会显示固定...

-E -P -v -dD "${plugin_state_location}/specs.cpp" -std=gnu++0x

我仍然有最后一个编辑器错误,我无法摆脱“符号 'duration_cast' 无法解决”(我有一张图片,但新用户无法发布图片)

有人对如何解决这个问题有任何想法吗?代码如下:

#ifndef _scoped_timer_h_
#define _scoped_timer_h_

#include <iostream>
#include <chrono>
#include "boost/noncopyable.hpp"
#include "boost/format.hpp"

using namespace std::chrono;

  // Utility class for timing and logging rates
// (ie "things-per-second").
// NB _any_ destructor invokation (including early return
// from a function or exception throw) will trigger an
// output which will assume that whatever is being measured
// has completed successfully and fully.
class scoped_timer : boost::noncopyable
{
public:


  scoped_timer(
    const std::string& what,
    const std::string& units,
    double n
  )
    :_what(what)
    ,_units(units)
    ,_how_many(n)
    ,_start(high_resolution_clock::now())
  {}

  ~scoped_timer() {
    high_resolution_clock::time_point stop = high_resolution_clock::now();
    const double t = 1e-9 * duration_cast<nanoseconds>(stop-_start).count();
    std::cout << (
      boost::format(
        "%1%: %|2$-5.3g| %|3$|/s (%|4$-5.3g|s)"
      ) % _what % (_how_many/t) % _units % t
    ) << std::endl;
  }

private:

  const std::string _what;
  const std::string _units;
  const double _how_many;
  const high_resolution_clock::time_point _start;
};

#endif

【问题讨论】:

  • 就我而言,我已将“-std=c++0x”添加到 Project -> Properties -> C++-Build[Settings] -> G++Compiler[miscellanous] -> Other flags .我已经检查过,包含路径中只有一个“chrono”文件。打开声明时,例如在 上按 F3,打开正确的文件并定义 duration_cast。我还注意到自动完成没有看到“duration_cast”,但可以从“chrono”标题中看到我需要的所有其他内容。
  • 您使用的是什么版本的 Eclipse/CDT?
  • 靛蓝 sr1 与 cdt 8.0.0.201109151620.
  • 通过将 -std=gnu++0x 添加到发现选项中,一些新符号被添加到我的项目路径和符号中(在构建和索引更新之后),例如GXX_EXPERIMENTAL_CXX0X。这修复了一个初始编辑器错误,指出 std::chrono 无法解析(即使文件编译正常)

标签: c++ eclipse boost c++11


【解决方案1】:

对于 eclipse 中的 chrono,您应该添加这些符号

_GLIBCXX_USE_C99_STDINT_TR1

__cplusplus = 201103L

如何添加它们:

项目属性 -> C/C++ General -> Path and Symbols -> Symbols (Tab) -> GNU C++ -> 然后点击添加。

记得添加__cplusplus201103L

【讨论】:

  • 这行得通,谢谢。也许你可以考虑解释一下它在做什么,让我们更有意义?
【解决方案2】:

Eclipse 有它自己的解析器。该解析器无法处理 c++11 (C++0x) 功能。所以你必须等到eclipse解析器中的c++11支持准备好。

【讨论】:

  • 我认为它永远不会准备好。如果我是 CDT 开发人员,我会选择使用 clang。但这是一个原生库而不是 Java,所以这不是一个真正的选择。
猜你喜欢
  • 2013-10-27
  • 1970-01-01
  • 1970-01-01
  • 2013-02-13
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
  • 2010-11-02
  • 1970-01-01
相关资源
最近更新 更多