Time functions in mingw

2019-09-06 17:16发布

I am trying to implement the code given in this page

http://msdn.microsoft.com/en-us/library/windows/desktop/aa366062%28v=vs.85%29.aspx

I am using mingw (gcc) to compile this. But the following lines cannot be compiled. I have included 'time.h'. I searched but cannot locate this '_localtime31_s' or its equivalent in gcc.

error = _localtime32_s(&newtime, (__time32_t*) &pAdapter->LeaseObtained);
error = asctime_s(buffer, 32, &newtime);

Where is the time functions here? Thanks

2条回答
再贱就再见
2楼-- · 2019-09-06 17:48

The functions localtime_s and asctime_s are Microsoft-specific extensions, provided by (certain versions of) the MS runtime library. This is provided by the MS header files. Since those are copyright MS and not allowed for free distribution, mingw provides its own versions of headers - and these probably don't contain these extensions (they certainly didn't a while back when I was using mingw on my local machine - my main machine these days runs Linux...).

Note that casting the time value to time32_t * is probably a bad idea - it is almost certainly going to bite you if you ever compile your code with a time_t that isn't a 32-bit value.

The localtime_r function is a semi-standard version that could be used instead of localtime_s (you will need to pay attention to 32 vs 64-bit time values). You can certainly also use localtime (aside from having to turn off MS's annoying "this function is not safe, please use ..._s instead" - I don't REALLY want to convert my 100 uses of strcpy to strcpy_s that work perfectly fine because it has already been checked elsewhere).

Similarly there is asctime_r which provides a re-entrant version.

You could, perhaps, also add the prototypes for these functions to your file somewhere, I believe that would, as long as you are compiling for Windows, solve the problem:

Link to MS function documentation: localtime_s and asctime_s.

查看更多
祖国的老花朵
3楼-- · 2019-09-06 17:50

MinGW-w64 provides an option to enable the secure CRT functions. Note there are compatibility issues with Windows XP, where msvcrt.dll does not contain these functions and your application will not work in that environment.

These are standardized in C11 Annex K, which is optional and may be missing on C11 conformant systems.

查看更多
登录 后发表回答