13

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?

MysteryMoose
  • 2,211
  • 4
  • 23
  • 47
  • Your snippet fails to include the header that declares `std::put_time`. – eerorika Jul 13 '17 at 02:47
  • Whoops, good point. I have `#include ` 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`. – MysteryMoose Jul 13 '17 at 02:48
  • 2
    Well, that would seem to be the problem. `std::put_time` is declared in ``. – eerorika Jul 13 '17 at 02:52
  • Yeah, this is embarrassing. That was totally it. Will accept your answer in 4 minutes. Well, hopefully this will help future searches. – MysteryMoose Jul 13 '17 at 02:54

1 Answers1

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

eerorika
  • 232,697
  • 12
  • 197
  • 326