代码之家  ›  专栏  ›  技术社区  ›  Govan

如何在CMake的外部库中包含头文件

  •  -2
  • Govan  · 技术社区  · 6 年前

    here . 我可以跑了 cmake. -DCMAKE_TOOLCHAIN_FILE 没有任何问题,但“make”命令不成功,并且在一个外部库中找不到头文件:“mirsdrapi rsp”。错误消息是:

    fatal error: mirsdrapi-rsp.h: No such file or directory
     #include "mirsdrapi-rsp.h"
                               ^
    compilation terminated.
    

    我已经创建了一个名为“lib”的文件夹,并将“libmirsdrapi-rsp.so公司“文件在里面。 我的CMakeLists.txt 具体如下:

    cmake_minimum_required(VERSION 3.4.1)
    set(CMAKE_C_STANDARD 11)
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_C_STANDARD_REQUIRED ON)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -pedantic -Wextra -v -g -D_XOPEN_SOURCE")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra -v ")
    set(CMAKE_EXE_LINKER_FLAGS  ${CMAKE_EXE_LINKER_FLAGS} "-v")
    set (SDR_API_PATH ${CMAKE_SOURCE_DIR}/lib)
    include_directories (include ${SDR_API_PATH})
    
    include_directories("${CMAKE_SOURCE_DIR}/lib")
    
    find_library(mirslocation NAMES mirsdrapi-rsp HINTS ${SDR_API_PATH} NO_CMAKE_FIND_ROOT_PATH)
    message(STATUS ${mirslocation})
    
    add_library(mirs STATIC IMPORTED)
    set_target_properties(mirs PROPERTIES IMPORTED_LOCATION ${mirslocation})
    
    target_link_libraries (raspberryPiDaemon mirs)
    target_link_libraries(raspberryPiDaemon m)
    

    Cmake在运行“find\u library”时正在打印库mirsdrapi rsp的正确路径,正如我所提到的,我只是在运行“make”命令而不是“Cmake”命令时收到错误消息。 我的CMAKE\u TOOLCHAIN\u文件的内容如下:

    # Define our host system
    SET(CMAKE_SYSTEM_NAME Linux)
    SET(CMAKE_SYSTEM_VERSION 1)
    
    # Define the cross compiler locations
    SET(CMAKE_C_COMPILER   ${CMAKE_SOURCE_DIR}/../tools-master/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc)
    SET(CMAKE_CXX_COMPILER ${CMAKE_SOURCE_DIR}/../tools-master/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc)
    
    # Define the sysroot path for the RaspberryPi distribution in our tools folder 
    SET(CMAKE_FIND_ROOT_PATH ${CMAKE_SOURCE_DIR}/../tools-master/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/arm-linux-gnueabihf/sysroot/SET)
    
    
    # Use our definitions for compiler tools
    SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    # Search for libraries and headers in the target directories only
    SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
    SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
    
    add_definitions(-Wall -std=c11)
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   Gergely Nyiri    6 年前

    更准确地说:您需要找到mirsdrapi rsp.h的路径,并让CMake知道它,就像find\u库一样:

    find_path(MIRSDRAPI_INCLUDE_DIRS NAMES mirsdrapi-rsp.h PATHS {proper-location})
    
    if (MIRSDRAPI_INCLUDE_DIRS)
      target_include_directories(raspberryPiDaemon PRIVATE ${MIRSDRAPI_INCLUDE_DIRS})
    endif()
    

    此外,您可以将INTERFACE\u INCLUDE\u DIRECTORIES属性设置为库,如下所示:

    set_property(TARGET mirsdrapi-rsp APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${MIRSDRAPI_INCLUDE_DIRS})