TIME_UTC and timespec_get not declared errors with g++/gcc installed by anaconda

TIME_UTC and timespec_get not declared with g++ installed by anaconda

I tried to install the latest anaconda with cxx-compiler and compilers and attempted to compile a c++ library, but it failed due to undeclared TIME_UTC marco and timespec_get function, which should have been included in the time.h file. However, my system compiler compiled the file sucessfully. I tried the solution from "error: '::timespec_get' has not been declared" with conda root package - #6 by kkrizka - ROOT - ROOT Forum and made sure that packages from conda-forge have higher priority, but it did not work for me.

I’ve tried search some other keywords but can’t seem to find similar cases on Google. So any hints/suggestions are appreciated.

Minimal Example

We start with a simple test example test.cc (It’s in C style, though it is expected to compile with C++ compiler):

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

// from https://stackoverflow.com/a/36095407
// Get the current time in nanoseconds
long get_nanos() {
	struct timespec ts;
	timespec_get(&ts, TIME_UTC);
	return (long)ts.tv_sec * 1000000000L + ts.tv_nsec;
}

int main() {
    // print time
    long test_time = get_nanos();
    printf("Time: %ld\n", test_time);
}

Create a fresh environment

conda create -n test_env
conda activate test_env
conda install -c conda-forge compilers cxx-compiler
g++ test.cc

Here is the error reported by g++

test.cc: In function 'long int get_nanos()':
test.cc:9:27: error: 'TIME_UTC' was not declared in this scope
    9 |         timespec_get(&ts, TIME_UTC);
      |                           ^~~~~~~~
test.cc:9:9: error: 'timespec_get' was not declared in this scope; did you mean 'timespec'?
    9 |         timespec_get(&ts, TIME_UTC);
      |         ^~~~~~~~~~~~
      |         timespec

For reference, this is the output of conda list after the above commands to make sure packages are coming from conda-forge

# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                 conda_forge    conda-forge
_openmp_mutex             4.5                       2_gnu    conda-forge
binutils                  2.39                 hdd6e379_1    conda-forge
binutils_impl_linux-64    2.39                 he00db2b_1    conda-forge
binutils_linux-64         2.39                h5fc0e48_13    conda-forge
c-compiler                1.5.2                h0b41bf4_0    conda-forge
compilers                 1.5.2                ha770c72_0    conda-forge
cxx-compiler              1.5.2                hf52228f_0    conda-forge
fortran-compiler          1.5.2                hdb1a99f_0    conda-forge
gcc                       11.3.0              h02d0930_13    conda-forge
gcc_impl_linux-64         11.3.0              hab1b70f_19    conda-forge
gcc_linux-64              11.3.0              he6f903b_13    conda-forge
gfortran                  11.3.0              ha859ce3_13    conda-forge
gfortran_impl_linux-64    11.3.0              he34c6f7_19    conda-forge
gfortran_linux-64         11.3.0              h3c55166_13    conda-forge
gxx                       11.3.0              h02d0930_13    conda-forge
gxx_impl_linux-64         11.3.0              hab1b70f_19    conda-forge
gxx_linux-64              11.3.0              hc203a17_13    conda-forge
kernel-headers_linux-64   2.6.32              he073ed8_15    conda-forge
ld_impl_linux-64          2.39                 hcc3a1bd_1    conda-forge
libgcc-devel_linux-64     11.3.0              h210ce93_19    conda-forge
libgcc-ng                 12.2.0              h65d4601_19    conda-forge
libgfortran5              12.2.0              h337968e_19    conda-forge
libgomp                   12.2.0              h65d4601_19    conda-forge
libsanitizer              11.3.0              h239ccf8_19    conda-forge
libstdcxx-devel_linux-64  11.3.0              h210ce93_19    conda-forge
libstdcxx-ng              12.2.0              h46fd767_19    conda-forge
sysroot_linux-64          2.12                he073ed8_15    conda-forge