0

TIME_UTC and timespec_get were not declared with gcc installed by anaconda

I tried to install the latest anaconda with cxx-compiler and compilers and attempted to compile a c file, 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 successfully. I tried the solution from https://root-forum.cern.ch/t/error-timespec-get-has-not-been-declared-with-conda-root-package/45712/6 and made sure that packages from conda-forge have higher priority, but it did not work for me.

I've tried searching 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.c:

#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
gcc test.c

Here is the error reported by gcc

test.c: In function 'get_nanos':
test.c:9:9: warning: implicit declaration of function 'timespec_get' [-Wimplicit-function-declaration]
    9 |         timespec_get(&ts, TIME_UTC);
      |         ^~~~~~~~~~~~
test.c:9:27: error: 'TIME_UTC' undeclared (first use in this function)
    9 |         timespec_get(&ts, TIME_UTC);
      |                           ^~~~~~~~
test.c:9:27: note: each undeclared identifier is reported only once for each function it appears in

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

This is the version of gcc installed by conda

(test_env) ➜  test_dir gcc --version 
gcc (conda-forge gcc 11.3.0-19) 11.3.0
Copyright (C) 2021 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.
Roger Q.
  • 1
  • 1
  • I think I may have narrowed down the problems to differences in time.h from glibc. The system-level `time.h` from `/usr/include/time.h` contains definition for TIME_UTC and timespec_get, but the version provided by anaconda, which is found at `~/anaconda3/envs/test_env/x86_64-conda-linux-gnu/sysroot/usr/include/time.h`, does not provide such definitions. – Roger Q. May 29 '23 at 22:56
  • 1
    `timespec_get` is a relatively new C++ thing. I'd try the function `clock_gettime` and the macro `CLOCK_REALTIME`. – Steve Summit May 29 '23 at 23:02
  • @Steve Summit, But it's part of C11, and they're using a gcc from 2021. My gcc from 2019 supports it. – ikegami May 29 '23 at 23:27
  • @ikegami Okay, my bad, thanks! (But I stand by my suggestion that OP might try `clock_gettime`.) – Steve Summit May 29 '23 at 23:35
  • @SteveSummit Thanks for the pointer, but I am trying to compile a large library. So it'd be best if I can avoid manually patching every single occurence. – Roger Q. May 30 '23 at 01:04

0 Answers0