6

Referring to the N1570 Committee Draft of the ISO/IEC 9899:201x Programming languages - C which dates back to April 12, 2011, there should be a function timespec_get() (see 7.27.2.5).

But MinGW gcc version 8.2.0 returns warning: implicit declaration of function 'timespec_get', and error: 'TIME_UTC' undeclared, when compiling the following code snippet with gcc -std=c11 snippet.c:

#include <time.h>

int main() {
   struct timespec tstart;
   timespec_get(&tstart, TIME_UTC);
   return 0;
}

Why is that?

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
Max Herrmann
  • 305
  • 2
  • 15

1 Answers1

0

In addition to -std=c11, you should use the -D_UCRT option for the compiler and -lucrt for the linker. There is working CMakeLists.txt and cmake invocation

$ cat CMakeLists.txt 
cmake_minimum_required (VERSION 3.10)
project(timespec_get)

set(EXE_NAME test)
set(MAIN_SOURCES 
    ${PROJECT_SOURCE_DIR}/src/test.c
)

add_executable(${EXE_NAME} ${MAIN_SOURCES})
if (${MINGW})
    set(CMAKE_C_FLAGS "-W -Wall -Wextra -std=c11 -D_UCRT")
    target_link_libraries(${EXE_NAME} -lucrt)
else ()
    set(CMAKE_C_FLAGS "-W -Wall -Wextra -std=c11")
endif ()

cmake invocation:

cmake -DCMAKE_TOOLCHAIN_FILE=/usr/share/mingw/toolchain-mingw64.cmake

and result

$ make
[ 50%] Building C object CMakeFiles/test.dir/src/test.c.obj
[100%] Linking C executable test.exe
[100%] Built target test
  • 1
    This depends on MinGW flavor. E.g. if you're using MSYS2, you should probably install their version of GCC that does this by default, instead of trying to convince a different version to target ucrt. – HolyBlackCat Aug 31 '23 at 09:12