11

I use some code to report duration of a task using std::chrono::high_resolution_clock ... part of c++0x.

I can successfully compile c++0x features in eclipse cdt using the -gnu++0x flag. Although successfully compiling, the editor seemed unaware of c++0x i.e. it displayed errors for any c++0x features in my code. I solved that by adding the -gnu++0x flag to my project discovery options. Note: doesn't show fixed until you do another compile and rebuild the index ...

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

I still have one last editor error that I can't rid myself of "Symbol 'duration_cast' could not be resolved" (I had a pic but new users can't post pics)

Anyone have any ideas on how to fix this? Here is the code:

#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
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
Rob Lorimer
  • 121
  • 1
  • 5
  • possible duplicate of [Build c++0x features with Eclipse](http://stackoverflow.com/q/8564544/636019) – ildjarn Jan 06 '12 at 20:28
  • In my case, I have added "-std=c++0x" to Project -> Properties -> C++-Build[Settings] -> G++Compiler[miscellanous] -> Other flags. I have checked and there is only one 'chrono' file in the include paths. When open the declaration e.g. press F3 on , the correct file is opened and has a definition for duration_cast. I also note that autocomplete does not see 'duration_cast' but sees everything else I need from the 'chrono' header. – Rob Lorimer Jan 06 '12 at 22:54
  • What version of Eclipse/CDT are you using? – ildjarn Jan 06 '12 at 23:01
  • indigo sr1 with cdt 8.0.0.201109151620. – Rob Lorimer Jan 06 '12 at 23:34
  • note mistake in my comment: -std=c++0x incorrect ... I actually use -std=gnu++0x – Rob Lorimer Jan 06 '12 at 23:36
  • 1
    by adding -std=gnu++0x to discovery options, a few new symbols are added to my project paths and symbols (after a build and index update) e.g. _GXX_EXPERIMENTAL_CXX0X_. This fixes an initial editor error stating that std::chrono could not be resolved (even though the file compiled ok) – Rob Lorimer Jan 06 '12 at 23:39
  • Make sure you've pathed up the appropriate compiler too. eclipse uses the prevailing "g++" and if you're using a higher-versioned gcc either by path or name you'll need to point eclipse at it. Additionally once you've modified this, I find a rebuild all (to "discover" the new settings), followed by a rebuild index usually does the trick. Finally sometimes I've had eclipse not rescan a directory. A horrible workaround is to "find . -type f | xargs touch" to update all the timestamps of the files (although this seems fixed in Juno). – Matt Godbolt Nov 28 '12 at 18:10

2 Answers2

8

For chrono in eclipse you should add those symbols

_GLIBCXX_USE_C99_STDINT_TR1

and

__cplusplus = 201103L

How to add them:

Prject properties -> C/C++ General -> Path and Symbols -> Symbols (Tab) -> GNU C++ -> and there click add.

Remember to add __cplusplus with value 201103L

Gelldur
  • 11,187
  • 7
  • 57
  • 68
  • 1
    this is working, thanks. maybe you can consider to explain what is it doing, for us to have more sense? – meakgoz Jan 20 '16 at 08:28
3

Eclipse has it's own parser. That parser can't deal with the c++11 (C++0x) features. So you have to wait until the c++11 support in the eclipse parser will be ready.

Kocka
  • 1,634
  • 2
  • 13
  • 21
  • I think it will be never ready. If I were a CDT developer I would choose to use clang for this. But this is a native library not Java so this is not really an option. – Kocka Feb 11 '14 at 09:46