首页 > 解决方案 > CMake:未将-lrt连接到项目

问题描述

我有一个项目,依赖于 rt 库,在运行“cmake .”之后使用 make 时显示 LIBRT 中定义的函数的未声明错误。我已经查看了与此类似的问题的大量建议,但没有一个对我有用。使用函数 check_include_files() 和 check_library_exists() 返回 TRUE,看来我使用 find_library() 成功找到了库。我什至在 link.txt 文件的末尾附加了“-lrt”。

无论如何,我有点难过并寻求一些指导。下面是我的 CMakeLists.txt 文件:

cmake_minimum_required (VERSION 3.5)
project (proj_name C)   
enable_language(C)

set (SOURCES
   /source/file/path
)

add_definitions (-std=c99)
add_executable (proj_name ${SOURCES})

find_library(LIBRT rt)
if(LIBRT)
    target_include_directories (proj_name PUBLIC ${PROJECT_SOURCE_DIR} 
        /proj/includes/path #not including rt header path 
        ${RT_INCLUDE_DIRS}
    )
    target_link_libraries(proj_name PUBLIC ${LIBRT})
endif(LIBRT)

set(CMAKE_C_COMPILER /usr/bin/gcc)

include(CheckIncludeFiles)
check_include_files(mqueue.h HAVE_MQUEUE_H)
if(NOT HAVE_MQUEUE_H)
    message(FATAL_ERROR "mqueue.h not found")
endif(NOT HAVE_MQUEUE_H)

include(CheckLibraryExists)
check_library_exists(rt timer_gettime "" HAVE_RT)
if(NOT HAVE_RT)
    message(FATAL_ERROR "timer_gettime not found")
endif(NOT HAVE_RT)

在 WSL Ubuntu 上运行。

感谢您提供任何可能的见解!

标签: cmake

解决方案


推荐阅读