Robotics StackExchange | Archived questions

Using Catkin with CMake Interface Library

I am currently trying to use a c++ header-only library declared as a cmake interface library within catkin. This library is supposed to be used by other packages in catkin_ws/src. I was able to compile all packages with catkin_make but not with catkin build.

catkin build fails within the cmake command find_package(... interface_lib) in dependent packages.

Error-Message for the example below would be:

Project 'testnode' tried to find library 'interface_library'. The library is neither a target nor built/installed properly. Did you compile project 'interface_library'? Did you find_package() it before the subdirectory containing its code is included?

How do i need to setup CMakeLists.txt and package.xml files to make catkin build work with interface libraries?

Minimal example:

Interface Library

File: catkin_ws/src/interface_library/include/interface_library.hpp

 #pragma once
 #define RATE 10

File: catkin_ws/src/interface_library/CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0)
project(interface_library)

find_package(catkin REQUIRED)

catkin_package(INCLUDE_DIRS include
  LIBRARIES ${PROJECT_NAME})

add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(${PROJECT_NAME} INTERFACE include)

File: catkin_ws/src/interface_library/package.xml

<package format="2">
  <name>interface_library</name>
  <description>Test interface library</description>
  <version>0.0.1</version>
  <maintainer email="master@disaster.com">Master of Disaster</maintainer>
  <license>MIT</license>
  <buildtool_depend>catkin</buildtool_depend>
</package>

Testnode

File: catkin_ws/src/testnode/src/testnode.cpp

#include <iostream>
#include "interface_library.hpp"

int main(void)
{
  std::cout << RATE << std::endl;
}

File: catkin_ws/src/testnode/CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0)
project(testnode)

find_package(catkin REQUIRED COMPONENTS interface_library)

catkin_package()

include_directories(${catkin_INCLUDE_DIRS})

add_executable(${PROJECT_NAME}_node src/testnode.cpp)

File: catkin_ws/src/testnode/package.xml

<?xml version="1.0"?>
<package format="2">
  <name>testnode</name>
  <version>0.0.0</version>
  <description>The testnode package</description>
  <maintainer email="master@disaster.com">Master of Disaster</maintainer>
  <license>MIT</license>

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>interface_library</build_depend>
  <build_export_depend>interface_library</build_export_depend>
  <exec_depend>interface_library</exec_depend>
</package>

For reference, it has also been posted to SO

Asked by friggler21 on 2018-11-13 04:16:06 UTC

Comments

I guess this is from catkin build essentially doing a catkin_make_isolated. This one should fail too.

Probably it is sufficient to add the respective install tags for the header files (and maybe the library as well?).

Asked by mgruhler on 2018-11-13 07:01:15 UTC

That's correct, catkin_make_isolated is failing as well. I am not sure, what 'install tags' are?!

Assuming you mean the install CMake macros, I have added install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}) to the interface library. Not working again.

Asked by friggler21 on 2018-11-13 07:54:37 UTC

Right, I was talking about the CMake macros. Sorry for being imprecise.

You have to install the headers as well, not just the library.

Asked by mgruhler on 2018-11-13 08:59:23 UTC

@mgruhler: the issue is that an INTERFACE library is not a regular library. But as it is being exported in the catkin_package(..) call, Catkin will pass it on as one.

When downstream pkgs (testnode) then use find_package(catkin ..) and try to use catkin_LIBRARIES, it contains a ..

Asked by gvdhoorn on 2018-11-13 09:02:18 UTC

.. interface_library target which will get passed to target_link_libraries(..).

It's likely that the various catkin_* macros/functions are not compatible with some of the newer CMake functionality such as INTERFACE.

But I guess @Dirk Thomas will respond later.

Asked by gvdhoorn on 2018-11-13 09:04:13 UTC

@gvdhoorn thanks for clarifying. I indeed missed that point (or rather, considered it irrelevant).

Asked by mgruhler on 2018-11-13 09:05:58 UTC

@Dirk Thomas Do we get a response? :)

Asked by friggler21 on 2018-11-21 11:07:27 UTC

Answers