Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11: "put_time is not a member of std" on Modern g++

Tags:

c++

gcc

c++11

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:

  • std::put_time implementation status in GCC?
  • Why does std::put_time not compile using multiple compilers?
  • https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54354

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?

like image 370
MysteryMoose Avatar asked Jul 13 '17 02:07

MysteryMoose


1 Answers

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

like image 70
eerorika Avatar answered Sep 19 '22 19:09

eerorika