CMake找不到创建的库(libgps)

时间:2018-09-08 19:05:57

标签: c++ cmake

我为libgps(cmake File)创建了一个cmake文件。这很好用,并且libgps.a / libgps.so(都尝试过)将放在/ usr / local / lib中。当我尝试使用时在该库中,我收到了cmake消息,即未找到该库。如何检查该库为什么不起作用?

以下是libgps的cmake代码:

cmake_minimum_required(VERSION 2.6)
set(PROJECT_NAME position_logger)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../RPiToolchainCmake/toolchain-rpi.cmake) # must be before project()
project(${PROJECT_NAME})



add_library(gps STATIC "")
#add_library(gps SHARED "")
target_link_libraries(gps m)
target_include_directories(gps PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)

target_sources(gps PUBLIC 
           ${CMAKE_CURRENT_SOURCE_DIR}/src/gps.h
           ${CMAKE_CURRENT_SOURCE_DIR}/src/nmea.h
           ${CMAKE_CURRENT_SOURCE_DIR}/src/serial.h)

target_sources(gps PRIVATE 
           ${CMAKE_CURRENT_SOURCE_DIR}/src/gps.c
           ${CMAKE_CURRENT_SOURCE_DIR}/src/nmea.c
           ${CMAKE_CURRENT_SOURCE_DIR}/src/serial.c)

add_executable(${PROJECT_NAME} examples/position_logger.c)
target_link_libraries(${PROJECT_NAME} gps)

install(TARGETS gps DESTINATION /usr/local/lib)
#install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)

toolchain-rpi.cmake包含交叉编译器的路径

# Define our host system
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)

# Define the cross compiler locations
SET(CMAKE_C_COMPILER    ${CMAKE_CURRENT_LIST_DIR}/../RaspCompiler/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER  ${CMAKE_CURRENT_LIST_DIR}/../RaspCompiler/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++)

# Define the sysroot path for the RaspberryPi distribution in our tools folder 
#SET(CMAKE_FIND_ROOT_PATH    ${CMAKE_CURRENT_LIST_DIR}/../../RaspCompiler/arm-    bcm2708/arm-rpi-4.9.3-linux-gnueabihf/sysroot/)

# 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 ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

这是我的测试程序的秘诀:

cmake_minimum_required(VERSION 2.6)
set(PROJECT_NAME GPS_Program)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../../RPiToolchainCmake/toolchain-rpi.cmake) # must be before project()
project(${PROJECT_NAME})


# libgps -- Begin
set(GPS_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../libgps/src)
set(LIBGPS_PATH /usr/local/lib)
find_library(GPS_LIBRARY 
            NAMES libgps 
            PATHS ${LIBGPS_PATH}
            NO_DEFAULT_PATH)
if (NOT ${GPS_LIBRARY})
    message(FATAL_ERROR "LIBGPS not found in: ${LIBGPS_PATH} . Install     libgps(Software/libgps)")
else()
    message("LIBGPS_LIBRARY FOUND: " ${LIBGPS_PATH})
endif()
# libgps -- End

include_directories (include ${GPS_INCLUDE_DIR})

FILE(GLOB SRC_FILES src/main.cpp)
add_executable(${PROJECT_NAME} ${SRC_FILES})

#install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)
target_link_libraries(${PROJECT_NAME} ${GPS_LIBRARY})

问题是,找不到libgps。每当收到该消息时,我都会得到未找到libgps的信息:

CMake Error at CMakeLists.txt:17 (message):
LIBGPS not found in: /usr/local/lib .  Install libgps(Software/libgps)

我检查了程序和库是使用同一编译器编译的。 我还添加了

#ifdef __cplusplus
extern "C"{
#endif

到gps.h文件

1 个答案:

答案 0 :(得分:0)

将libgps更改为gps,并且从cmake进行的更改正常工作后。

我认为我必须从GPS_LIBRARY中获取该值。但这就是问题所在。

错误

if (NOT ${GPS_LIBRARY})
    message("LIBGPS not found in: ${GPS_LIBRARY} . Install libgps(Software/libgps)")
else()
    message("LIBGPS_LIBRARY FOUND: " ${LIBGPS_PATH})
endif()

正确

if (NOT GPS_LIBRARY)
    message("LIBGPS not found in: ${GPS_LIBRARY} . Install libgps(Software/libgps)")
else()
    message("LIBGPS_LIBRARY FOUND: " ${LIBGPS_PATH})
endif()