12

I am trying out a simple program to print the timestamp value of steady_clock as shown below:

#include <iostream>
#include <chrono>
using namespace std;
int main ()
{
  cout << "Hello World! ";
  uint64_t now = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
  cout<<"Value: " << now << endl;

  return 0;
}

But whenever I am compiling like this g++ -o abc abc.cpp, I am always getting an error:

In file included from /usr/include/c++/4.6/chrono:35:0,
                 from abc.cpp:2:
/usr/include/c++/4.6/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
abc.cpp: In function âint main()â:
abc.cpp:7:3: error: âuint64_tâ was not declared in this scope
abc.cpp:7:12: error: expected â;â before ânowâ
abc.cpp:8:22: error: ânowâ was not declared in this scope

Is there anything wrong I am doing?

Arun
  • 2,087
  • 2
  • 20
  • 33
user1950349
  • 4,738
  • 19
  • 67
  • 119

3 Answers3

13

Obviously, I'm not following certain best practices, but just trying to get things working for you

#include <iostream>
#include <chrono>
#include <cstdint> // include this header for uint64_t

using namespace std;
int main ()
{
  {
    using namespace std::chrono; // make symbols under std::chrono visible inside this code block
    cout << "Hello World! ";
    uint64_t now = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
    cout<<"Value: " << now << endl;
  }

  return 0;
}

and then compile using C++11 enabled (c++0x in your case)

g++ -std=c++0x -o abc abc.cpp
Arun
  • 2,087
  • 2
  • 20
  • 33
  • When I run this, I see this error `error: steady_clock has not been declared`. Any thoughts why this is coming when we already have declared it? – user1950349 Aug 19 '15 at 02:07
  • 1
    steady_clock is under std::chrono. Either use fully qualified name (std::chrono::steady_clock) or make symbols visible by making use of using namespace std::chrono as shown in my example above – Arun Aug 19 '15 at 02:10
  • I tried with your example only and then I got above error. I just used your example as it is. – user1950349 Aug 19 '15 at 02:11
  • I am using `g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3`. Do you think this will matter here? – user1950349 Aug 19 '15 at 02:15
  • If I run this on Ubuntu 14 which has 4.8.2 compiler, then it works fine but if I run this on Ubuntu 12 which has 4.6.3, then it doesn't work. Strange? – user1950349 Aug 19 '15 at 02:17
  • looks like known issue. https://lists.debian.org/debian-gcc/2013/07/msg00040.html – Arun Aug 19 '15 at 02:22
  • Current GCC is [GCC 7](https://gcc.gnu.org/gcc-7/) in last quarter of 2017 – Basile Starynkevitch Dec 02 '17 at 14:39
7

You should include stdint.h file.

JerryYoung
  • 267
  • 1
  • 3
0

If you really want to include, add "#define __STDC_LIMIT_MACROS"

Ref: https://stackoverflow.com/a/3233069/6728794

liu bluse
  • 169
  • 1
  • 4