cmake undefined typename loc_t (gpslib)

2019-08-28 02:13发布

问题:

Here is my actual code:

I'm trying to create a cmake build system for gpslib.

cmake_minimum_required(VERSION 2.6)
set(PROJECT_NAME LOGGER)
project(${PROJECT_NAME})


set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "res/cmake/Modules/")


add_library(gps_lib STATIC "")

target_link_libraries(gps_lib m)
target_include_directories(gps_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src ${LIBM_INCLUDE_DIRS})

target_sources(gps_lib 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_lib PRIVATE 
           ${CMAKE_CURRENT_SOURCE_DIR}/src/gps.c
           ${CMAKE_CURRENT_SOURCE_DIR}/src/nmea.c
           ${CMAKE_CURRENT_SOURCE_DIR}/src/serial.c)
target_link_libraries(gps_lib PUBLIC ${LIBM_LIBRARIES})

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

Does anybody know how to build this project?

This is the error message:

/usr/bin/ld: libgps_lib.a(gps.c.o): in function `gps_deg_dec': gps.c:(.text+0x2d5): undefined reference to `round'
/usr/bin/ld: gps.c:(.text+0x312): undefined reference to `round'

In the res/cmake/Modules is a FindLibM.cmake Module from FindLibM.cmake


Edit due to compor answear:

  • added link libraries for gps_lib
  • removed LIBM_LIBRARIES from PROJECT_NAME link library

Does not change anything

Thanks to @KamilCuk I solved the Problem.

link_libraries(m) or target_link_libraries(gps_lib m) (only for the target gps_lib)

must be added to the cmake File

Thanks to everbody who helped me to find my errors!

回答1:

The first target_sources should be

target_include_directories(gps_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)

It seems it cannot find a definition of loc_t as is defined in gps.h, so the above line will inform the target with an include directory to search for it.

Moreover, the last line should be

target_link_libraries(${PROJECT_NAME} ${LIBM_LIBRARIES} gps_lib)

There's no ${gps_lib} variable, gps_lib is the name of a target.


Update due to OP edit

I'm not sure if you've changed the examples, but the initial example code that you refer to does not include math.h hence the math library should be used as a dependency for the gps_lib target only

target_link_libraries(gps_lib PUBLIC ${LIBM_LIBRARIES})


回答2:

I had to link libm (math.h) into the target gps_lib.

link_libraries(m) 

or

target_link_libraries(gps_lib m) (only for the target gps_lib)


标签: cmake