Search code examples
c++cmakeroscatkin

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="[email protected]">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="[email protected]">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>

Solution

  • I've just come across this problem. The fix was to remove the line LIBRARIES ${PROJECT_NAME} from catkin_package in the file interface_library/CMakeLists.txt.

    This answer mentions that the LIBRARIES line requires that you're actually building a target (and the documentation says it currently breaks if the target name doesn't correspond to an installed name), so I guess it just isn't currently written to deal with interface libraries.