Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++: location of localtime_s in GCC

Following the documentation and compiling with gcc-11.2.0 like so

g++ -std=c++17 -pthread -O3 -flto -fPIC ...

I am not able to use localtime_s in my program:

#define __STDC_WANT_LIB_EXT1__ 1
#include <time.h>

using SystemClock = std::chrono::system_clock;
const auto in_time_t = SystemClock::to_time_t(SystemClock::now());

struct tm buf; localtime_s(&in_time_t, &buf);
// error: there are no arguments to ‘localtime_s’ that depend on a template parameter, 
//        so a declaration of ‘localtime_s’ must be available [-fpermissive]

struct tm buf; std::localtime_s(&in_time_t, &buf);
// error: ‘localtime_s’ is not a member of ‘std’; did you mean ‘localtime’?

Am I doing something wrong or localtime_s is not available in GCC? Or it is only available for pure C programs (for example, compiled with gcc -std=c11)?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you very much for your help!

>Solution :

Many of the functions specified in the standard ending with _s were originally developed by Microsoft as alternates to functions ending in _r.

On Linux systems, localtime_s is not defined but localtime_r is, and has the same parameters/return type, so use that instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading