Skip to content

error: 'CLOCK_MONOTONIC' undeclared #54

Closed

Description

@lgehr

The initial make fails with CLOCK_MONOTONIC undeclared

I llama.cpp build info: 
I UNAME_S:  Linux
I UNAME_P:  unknown
I UNAME_M:  x86_64
I CFLAGS:   -I.              -O3 -DNDEBUG -std=c11   -fPIC -pthread -mavx -mavx2 -mfma -mf16c -msse3
I CXXFLAGS: -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC -pthread
I LDFLAGS:  
I CC:       cc (Alpine 12.2.1_git20220924-r9) 12.2.1 20220924
I CXX:      g++ (Alpine 12.2.1_git20220924-r9) 12.2.1 20220924

cc  -I.              -O3 -DNDEBUG -std=c11   -fPIC -pthread -mavx -mavx2 -mfma -mf16c -msse3   -c ggml.c -o ggml.o
ggml.c: In function 'ggml_time_ms':
ggml.c:309:5: warning: implicit declaration of function 'clock_gettime' [-Wimplicit-function-declaration]
  309 |     clock_gettime(CLOCK_MONOTONIC, &ts);
      |     ^~~~~~~~~~~~~
ggml.c:309:19: error: 'CLOCK_MONOTONIC' undeclared (first use in this function)
  309 |     clock_gettime(CLOCK_MONOTONIC, &ts);
      |                   ^~~~~~~~~~~~~~~
ggml.c:309:19: note: each undeclared identifier is reported only once for each function it appears in
ggml.c: In function 'ggml_time_us':
ggml.c:315:19: error: 'CLOCK_MONOTONIC' undeclared (first use in this function)
  315 |     clock_gettime(CLOCK_MONOTONIC, &ts);
      |                   ^~~~~~~~~~~~~~~
make: *** [Makefile:182: ggml.o] Error 1

This can be solved by adding -D_POSIX_C_SOURCE=199309L to the C{,XX}FLAGS in the Makefile. See this Stackoverflow question: https://stackoverflow.com/questions/29666937/error-clock-monotonic-undeclared-first-use-in-this-function

Activity

  1. changed the title [-]error: 'CLOCK_MONOTONIC' undeclared on Alpine Linux[/-] [+]error: 'CLOCK_MONOTONIC' undeclared[/+] on Mar 12, 2023
  2. liuxiaocs7 commented on Mar 13, 2023

    @liuxiaocs7

    same question

    (base) lizheng@lizheng-System-Product-Name:/home/ytkj/root1/liuxiao/LLM/llama.cpp$ make
    I llama.cpp build info: 
    I UNAME_S:  Linux
    I UNAME_P:  x86_64
    I UNAME_M:  x86_64
    I CFLAGS:   -I.              -O3 -DNDEBUG -std=c11   -fPIC -pthread -mavx -mavx2 -mfma -mf16c -msse3
    I CXXFLAGS: -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC -pthread
    I LDFLAGS:  
    I CC:       cc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
    I CXX:      g++ (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
    
    cc  -I.              -O3 -DNDEBUG -std=c11   -fPIC -pthread -mavx -mavx2 -mfma -mf16c -msse3   -c ggml.c -o ggml.o
    ggml.c: In function ‘ggml_time_ms’:
    ggml.c:309:5: warning: implicit declaration of function ‘clock_gettime’ [-Wimplicit-function-declaration]
         clock_gettime(CLOCK_MONOTONIC, &ts);
         ^
    ggml.c:309:19: error: ‘CLOCK_MONOTONIC’ undeclared (first use in this function)
         clock_gettime(CLOCK_MONOTONIC, &ts);
                       ^
    ggml.c:309:19: note: each undeclared identifier is reported only once for each function it appears in
    ggml.c: In function ‘ggml_time_us’:
    ggml.c:315:19: error: ‘CLOCK_MONOTONIC’ undeclared (first use in this function)
         clock_gettime(CLOCK_MONOTONIC, &ts);
                       ^
    Makefile:182: recipe for target 'ggml.o' failed
    make: *** [ggml.o] Error 1
  3. wshzd commented on Mar 13, 2023

    @wshzd

    在ggml.h头文件中增加以下宏定义即可
    #define _POSIX_C_SOURCE 199309L

  4. added
    help wanted
    on Mar 13, 2023
  5. anishthite commented on Mar 14, 2023

    @anishthite

    在ggml.h头文件中增加以下宏定义即可 #define _POSIX_C_SOURCE 199309L

    This fixed it for me

  6. gquarles commented on Mar 14, 2023

    @gquarles

    Adding -lrt to the CXXFLAGS and changing CFLAGS to use -std=gnu11 worked for me, running Ubuntu 16

  7. tdr1991 commented on Mar 15, 2023

    @tdr1991

    我的机器是centos7, 我在 Makefile文件添加 -D_POSIX_C_SOURCE=199309L 可以解决问题。

    CFLAGS   = -I.              -O3 -DNDEBUG -std=c11   -fPIC -D_POSIX_C_SOURCE=199309L
    CXXFLAGS = -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC -D_POSIX_C_SOURCE=199309L
    
  8. added a commit that references this issue on Mar 21, 2023
  9. Errand24 commented on Apr 5, 2023

    @Errand24

    Same happens when compiling on macOS 10.7:
    ggml.c:310:19: error: use of undeclared identifier 'CLOCK MONOTONIC'
    ggml.c:316:19: error: use of undeclared identifier 'CLOCK MONOTONIC'

  10. added a commit that references this issue on Mar 14, 2026
  11. 2 remaining items

  12. added 2 commits that reference this issue on Jul 5, 2026
  13. added a commit that references this issue on Jul 16, 2026
  14. added a commit that references this issue on Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions