0

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!

Murmi
  • 91
  • 10
  • Shouldn't the first `target_sources` really be `target_include_directories(gps_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)`? It seems it cannot find a definition because of missing headers; `loc_` is defined in `gps.h`. – compor Sep 07 '18 at 11:25
  • Also 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. – compor Sep 07 '18 at 11:29
  • I added now the line `target_include_directories(gps_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src ${LIBM_INCLUDE_DIRS})` now there is a undefined reference to round. In res/cmake/Modules there is a module which defines LIBM_INCLUDE_DIRS and LIBM_LIBRARIES – Murmi Sep 07 '18 at 12:05
  • It's hard to do this as each error comes about, especially when we have no idea what's `res/cmake/Modules`. Looking at original `Makefile` it seems to just link with the math library by using `-lm`. Please, either amend your question or ask a new one, as I feel that the initially posted error was solved. If you're good with that I'll just convert my comment to an answer. – compor Sep 07 '18 at 12:15
  • Please, update ([edit]) the question with **actual code** and **exact error message** you couldn't solve. – Tsyvarev Sep 07 '18 at 13:01
  • link math `link_libraries(m)` for round – KamilCuk Sep 07 '18 at 18:44
  • @compor in the this folder there is just FindLibM.cmake from the link above. – Murmi Sep 07 '18 at 19:04
  • @KamilCuk what is m? – Murmi Sep 07 '18 at 19:05
  • https://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c It's libm.so, the C math library – KamilCuk Sep 07 '18 at 19:33

2 Answers2

0

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})
compor
  • 2,239
  • 1
  • 19
  • 29
  • "It seems it cannot find a definition because of missing headers" - The error about missed include file would come before other errors and its content would definitely differ. Probably, the "unknown typename" error originated from the compiling of the header files as a source ones (because the header files was listed as source ones), but the only header file which refers to the missed type is `gps.h`, and this header contains definition of this type. – Tsyvarev Sep 07 '18 at 13:06
  • @Tsyvarev I'll amend the phrasing, although I was trying to make it simple, based on the little info we have. As for finding the header from the `target_sources`, I doubt that it's working like that. Last time I tried it, the header does not even appear on the compile line. – compor Sep 07 '18 at 13:31
  • @compor What do you need? Do I forgott something? – Murmi Sep 07 '18 at 13:47
  • @compor yes there was always the math.h in the libgps. It is not my repository – Murmi Sep 07 '18 at 17:59
0

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)
Murmi
  • 91
  • 10