Problems building MRPT program with cmake

83 Views Asked by At

I hate cmake, as I always seem to spend more time trying to get cmake to compile and build a program than I do writing and debugging the program cmake is supposed to be building.

Anyway, the latest problem is this.

I have some code that starts ...

    #include <mrpt/obs/CObservationBatteryState.h>
    #include <mrpt/obs/CObservationComment.h>
    #include <mrpt/obs/CObservation2DRangeScan.h>
    #include <mrpt/maps/CSimplePointsMap.h>
    #include <mrpt/comms/CSerialPort.h>
    #include <mrpt/poses/CPose3D.h>
    #include <mrpt/system/os.h>

a fairly simple CMakeLists.txt that looks like...

project(some_sort_of_name)

cmake_minimum_required(VERSION 3.8)

find_package(MRPT COMPONENTS obs maps comms )
if(CMAKE_COMPILER_IS_GNUCXX AND NOT CMAKE_BUILD_TYPE MATCHES "Debug")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
endif()

add_executable(lidar lidar.c)
add_executable(lidar_obs lidar_obs.cpp)
   
target_link_libraries(lidar_obs mrpt::obs mrpt::maps)

And the compilation fails with...

[ 50%] Built target lidar
[ 75%] Building CXX object CMakeFiles/lidar_obs.dir/lidar_obs.cpp.o
/home/mike/work/minipupper/lidar/lidar_obs.cpp:5:10: fatal error: mrpt/comms/CSerialPort.h: No such file or directory
    5 | #include <mrpt/comms/CSerialPort.h>

So it is happy with compiling & building "lidar" which is a simple 'C' program with no calls to MRPT, but when it comes to compiling lidar_obs, it is happy to find the include files for obs and maps but not comms.

The include files are there, i.e.

$ ls /usr/include/mrpt/comms/include/mrpt
comms  comms.h
$ ls /usr/include/mrpt/comms/include/mrpt/comms
CClientTCPSocket.h  CInterfaceFTDI.h  CSerialPort.h  CServerTCPSocket.h  net_utils.h  nodelets.h  registerAllClasses.h

just like e.g. obs & maps

$ ls /usr/include/mrpt/obs
include
$ /usr/include/mrpt/obs/include
mrpt
$ ls /usr/include/mrpt/obs/include/mrpt
maps  obs  obs.h
$ ls /usr/include/mrpt/obs/include/mrpt/maps
CMetricMapEvents.h  CMetricMap.h  CSimpleMap.h  metric_map_types.h  TMetricMapInitializer.h  TMetricMapTypesRegistry.h

What is going on with cmake ? How does it find some files and not others.

1

There are 1 best solutions below

0
On

You were missing the link command to also include the mrpt-comms library:

target_link_libraries(lidar_obs mrpt::obs mrpt::maps mrpt::comms)
#                                                    ^^^^^^^^^^^

CMake with exported libraries work like that: "linking" an imported cmake target, also includes the "-I" flags (for #includes) apart of the link -l flags themselves.