I am having an old problem on a new compiler. While trying to print the current time using std::chrono
I receive the following error:
src/main.cpp:51:30: error: ‘put_time’ is not a member of ‘std’
std::cout << std::put_time(std::localtime(&time), "%c") << "\n\n";
^~~
The offending snippet is nothing special:
#include <chrono>
...
auto time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::cout << std::put_time(std::localtime(&time), "%c") << "\n\n";
This looks an awful lot like the errors being returned in the GCC 4.x series, as referenced all over the place:
The only problem with this theory is that my GCC is modern:
$ g++ --version
g++ (GCC) 6.3.1 20161221 (Red Hat 6.3.1-1)
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I also checked that my application is linking against the appropriate libraries:
$ ldd build/myapp
...
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fde97e7b000)
libc.so.6 => /lib64/libc.so.6 (0x00007fde97595000)
Finally, there is nothing exotic going on in my compile flags:
g++ -g -Wall -Werror -std=c++11 -Wno-sign-compare src/main.cpp -c -o obj/main.o
Everything I can think to check indicates that this should be working. So, in short, what gives?
The problem is that you included the wrong header. std::put_time
is declared in <iomanip>
, not <chrono>
.
Also, it is not complaining about any of the other time operations such as
std::localtime
andto_time_t
.
std::localtime
is declared in <ctime>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With