ros2 foxy including header file from other package
I have two packages, temp_lib
and temp_pkg
. In temp_lib
I have a file temp.hpp
that I would like to include in other packages, but when I attempt to do so by writing in #include "temp_lib/temp.hpp"
in a cpp file in temp_pkg
then it says this header cannot be found.
The problem is i'm not really sure how to set up the CMakeLists file for the library.
How can I set these packages up for this to be possible?
LIBRARY CMAKE
cmake_minimum_required(VERSION 3.5)
project(temp_lib)
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
install(TARGETS
DESTINATION lib/${PROJECT_NAME}
PUBLIC_HEADER DESTINATION include/${PROJECT_NAME}/
)
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION include/${PROJECT_NAME}
FILES_MATCHING PATTERN "*.h"
PATTERN ".git" EXCLUDE)
ament_package()
temp.hpp (from temp_lib
)
#ifndef TEMP_LIB__TEMP_HPP_
#define TEMP_LIB__TEMP_HPP_
#include <iostream>
#endif // TEMP_LIB__TEMP_HPP_
PKG CMAKE
cmake_minimum_required(VERSION 3.5)
project(temp_pkg)
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(temp_lib REQUIRED)
include_directories(
${temp_lib_INCLUDE_DIRS}
)
add_executable(test src/temp.cpp)
ament_target_dependencies(test rclcpp temp_lib)
install(TARGETS
test
DESTINATION lib/${PROJECT_NAME}
)
ament_package()
temp.cpp (from temp_pkg
)
#include "temp_lib/temp.hpp"
int main(int argc, char *argv[]) {
std::cout << "it works" << std::endl;
return 0;
}
Edit: for legibility I removed some repeated standard parts of the CMake files, these are
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
and
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
I think @morten's answer is a good example using ament_cmake (and I see no reason not to).
One question that's potentially just a typo. I did notice that your example temp_lib install function only installs
*.h
headers rather than*.hpp
headers. If that's the case then temp_lib/temp.hpp won't be installed since it doesn't patch the pattern.that was definitely a typo