Undefined reference to clock_gettime, gcc 4.6 & cmake

13,871

You should ensure that -lpthread -lrt appear after your libraries like ../lib/libLibUtil.a

Share:
13,871
hookenz
Author by

hookenz

Updated on June 19, 2022

Comments

  • hookenz
    hookenz almost 2 years

    I'm having a link error under cmake that I don't have when compiling on a system with an older version of gcc.

    The specific error is "undefined reference to clock_gettime" but -lrt is appearing on the command line.

    My gcc version:

    gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
    Copyright (C) 2011 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.
    

    cmake version 2.8.5

    I am including the required "-lrt" through target_link_libraries in my CMakeLists.txt file. I can see it appear in the compiler line with make VERBOSE=1

    I've read that gcc 4.6 is picky about the position of link libraries on the command line.

    How can I fix this for CMake?

    EDIT: Link line

    Linking CXX executable ../bin/obbsd
    cd /home/matt/Desktop/Matt/OBBS/Build/server && /usr/bin/cmake -E cmake_link_script \
         CMakeFiles/obbsd.dir/link.txt --verbose=1   
    /usr/bin/c++   -g    CMakeFiles/obbsd.dir/Block.cpp.o \
         CMakeFiles/obbsd.dir/BlockFileCache.cpp.o \
          CMakeFiles/obbsd.dir/BlockFileStore.cpp.o \
          CMakeFiles/obbsd.dir/BlockMemoryCache.cpp.o \
          CMakeFiles/obbsd.dir/BlockStore.cpp.o CMakeFiles/obbsd.dir/Config.cpp.o \
          CMakeFiles/obbsd.dir/DeleteBlockSession.cpp.o \
          CMakeFiles/obbsd.dir/mConfigFile.cpp.o \
          CMakeFiles/obbsd.dir/mNetworkPacketReader.cpp.o \
          CMakeFiles/obbsd.dir/mNetworkPacketWriter.cpp.o \
          CMakeFiles/obbsd.dir/obbsd.cpp.o \
          CMakeFiles/obbsd.dir/ReadBlockSession.cpp.o CMakeFiles/obbsd.dir/Server.cpp.o \
          CMakeFiles/obbsd.dir/Session.cpp.o CMakeFiles/obbsd.dir/Utility.cpp.o \
          CMakeFiles/obbsd.dir/WriteBlockSession.cpp.o  -o ../bin/obbsd -rdynamic \
          -lpthread -lrt ../lib/libLibUtil.a 
    ../lib/libLibUtil.a(mTimer.cpp.o): In function `mTimer::GetTick()':
    /home/matt/Desktop/Matt/OBBS/LibUtil/src/mTimer.cpp:108: undefined reference to
         `clock_gettime'
    

    Why is ../lib/libLibUtil.a appearing after -lrt when LibUtil (what would be libLibUtil.a) is before rt?

    CMakeLists.txt includes...

    ...
    add_executable(obbsd ${SERVER_SOURCE_FILES})
    find_package(Threads REQUIRED)
    target_link_libraries(obbsd ${CMAKE_THREAD_LIBS_INIT} LibUtil rt)