14

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?

4
  • Your snippet fails to include the header that declares std::put_time.
    – eerorika
    Commented Jul 13, 2017 at 2:47
  • Whoops, good point. I have #include <chrono> up top. Question has been updated. Also, it is not complaining about any of the other time operations such as std::localtime and to_time_t. Commented Jul 13, 2017 at 2:48
  • 2
    Well, that would seem to be the problem. std::put_time is declared in <iomanip>.
    – eerorika
    Commented Jul 13, 2017 at 2:52
  • Yeah, this is embarrassing. That was totally it. Will accept your answer in 4 minutes. Well, hopefully this will help future searches. Commented Jul 13, 2017 at 2:54

1 Answer 1

18

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 and to_time_t.

std::localtime is declared in <ctime>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.