Using Catkin with CMake Interface Library

asked 2018-11-13 03:17:58 -0500

friggler21 gravatar image

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

edit retag flag offensive close merge delete

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?).

mgruhler gravatar image mgruhler  ( 2018-11-13 06:01:15 -0500 )edit

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.

friggler21 gravatar image friggler21  ( 2018-11-13 06:54:37 -0500 )edit

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

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

mgruhler gravatar image mgruhler  ( 2018-11-13 07:59:23 -0500 )edit

@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 ..

gvdhoorn gravatar image gvdhoorn  ( 2018-11-13 08:02:18 -0500 )edit

.. 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.

gvdhoorn gravatar image gvdhoorn  ( 2018-11-13 08:04:13 -0500 )edit

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

mgruhler gravatar image mgruhler  ( 2018-11-13 08:05:58 -0500 )edit

@Dirk Thomas Do we get a response? :)

friggler21 gravatar image friggler21  ( 2018-11-21 10:07:27 -0500 )edit