0

I created a cmake file for libgps (cmake File). This worked fine, and the libgps.a/libgps.so(tryed both) will be placed in /usr/local/lib. When I try to use this library I get the cmake message, that the library was not found. What can I check to find out why it does not work?

Here the cmake code for libgps:

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 contains the paths to the cross compiler

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

This is the cmake for my test program:

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

The problem is, that libgps will not be found. I get every time the message, that libgps was not found:

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

I checked that the program and the library are compiled with the same compiler. I added also

#ifdef __cplusplus
extern "C"{
#endif

to the gps.h file

Murmi
  • 91
  • 10
  • `set(CMAKE_TOOLCHAIN_FILE ...)` - This is not how toolchain works in CMake. You need to pass `CMAKE_TOOLCHAIN_FILE` to the `cmake` invocation via option `-DCMAKE_TOOLCHAIN_FILE=...`. On Linux, `lib` is automatic prefix for the library, it is not included to the library name. Use `NAMES gps` or `NAMES libgps.so` for find the library. – Tsyvarev Sep 08 '18 at 19:51
  • But set(CMAKE_TOOLCHAIN_FILE ...) works fine. so I dont have to type everytime -DCMAKE_TOOLCHAIN_FILE=... I tries now NAMES gps and NAMES libgps.so but it did not change something. – Murmi Sep 08 '18 at 20:15
  • Hm, I had always thought that toolchain is executed before `CMakeLists.txt`. But you are right - a toolchain is actually executed at the `project()` call, even if it is passed with `-D` option. – Tsyvarev Sep 08 '18 at 20:45

1 Answers1

0

After changing libgps to gps and the change from below cmake worked properly.

I thought I have to take the value from GPS_LIBRARY. But this was the problem.

wrong:

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

correct:

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