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

时间:2018-09-14 13:14:14

标签: c build cmake raspberry-pi

我有一个在Raspberry Pi 3上运行的C应用程序,目前,我必须使用Cmake在PI上构建它。我正在尝试在Ubuntu计算机上构建它。我已经按照here的说明添加了CMAKE_TOOLCHAIN_FILE。 我可以毫无问题地运行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_library”时正在打印库mirsdrapi-rsp的正确路径,正如我提到的那样,我只是在运行“ make”命令而不是“ cmake”命令时收到错误消息。 我的CMAKE_TOOLCHAIN_FILE内容如下:

# 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)

任何人都知道如何从mirsdrapi-rsp库添加头文件以包含路径吗?

1 个答案:

答案 0 :(得分:0)

我猜您正在将include_directories设置为错误的路径(它被设置为$ {CMAKE_SOURCE_DIR} / lib的2倍,它必须是库的文件夹而不是头文件)。再次检查丢失的头文件的正确位置。

更精确的是:您需要找到mirsdrapi-rsp.h的路径,并像find_library一样让CMake知道它:

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_INCLUDE_DIRECTORIES属性设置为库,如下所示:

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

这样,target_include_directories可能是不必要的,而target_link_directories应该就足够了。