[ROS2] Including a cpp header from another package
Dear fellow developers:
I am a beginner in ROS2. I have designed a library that I want all packages in ROS2 to have access.
This is the warning I get when I try to build. This is causing an error.
CMake Warning at /home/jetson/ros2_ws/install/library_pkg/share/library_pkg/cmake/ament_cmake_export_libraries-extras.cmake:116 (message):
Package 'library_pkg' exports library 'export_library_pkg' which couldn't
be found
Call Stack (most recent call first):
/home/jetson/ros2_ws/install/library_pkg/share/library_pkg/cmake/library_pkgConfig.cmake:38 (include)
CMakeLists.txt:21 (find_package)
This is my CMakelist
cmake_minimum_required(VERSION 3.5)
project(library_pkg)
# 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()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(cv_bridge REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(image_transport REQUIRED)
find_package(std_msgs REQUIRED)
# Try for OpenCV 4.X, but settle for whatever is installed
find_package(OpenCV 4 QUIET)
if (NOT OpenCV_FOUND)
find_package(OpenCV REQUIRED)
endif ()
message(STATUS "Found OpenCV version ${OpenCV_VERSION}")
# Package includes not needed for CMake >= 2.8.11
include_directories(
include
${colcon_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
)
add_library(
${PROJECT_NAME} SHARED
src/CameraCapture.cpp
src/MJPEGWriter.cpp
src/CameraMainNode.cpp
)
target_include_directories(${PROJECT_NAME} PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include>")
ament_target_dependencies(
${PROJECT_NAME}
image_transport
OpenCV
rclcpp
sensor_msgs
std_msgs
)
ament_export_include_directories(include)
ament_export_libraries(export_${PROJECT_NAME})
#=============
# Install
#=============
install(
DIRECTORY include/
DESTINATION include
)
install(
TARGETS ${PROJECT_NAME}
EXPORT export_${PROJECT_NAME}
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
)
ament_package()
The error that it is causing
undefined reference
The full error
CMakeFiles/camera_capture.dir/src/CameraMain.cpp.o: In function `main':
CameraMain.cpp:(.text+0x224): undefined reference to `CameraCapture::CameraCapture()'
CameraMain.cpp:(.text+0x2bc): undefined reference to `void CameraCapture::Capture<std::shared_ptr<CameraNode> >(std::shared_ptr<CameraNode>, image_transport::Publisher, int, int, int, int, int, int)'
CameraMain.cpp:(.text+0x378): undefined reference to `CameraCapture::~CameraCapture()'
CameraMain.cpp:(.text+0x47c): undefined reference to `CameraCapture::~CameraCapture()'
collect2: error: ld returned 1 exit status
make[2]: *** [camera_capture] Error 1
make[1]: *** [CMakeFiles/camera_capture.dir/all] Error 2
make: *** [all] Error 2
This is the CMakelist on the package that include the library
project(camera_pkg)
# 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()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(image_transport REQUIRED)
find_package(OpenCV REQUIRED)
find_package(std_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(library_pkg REQUIRED)
# Try for OpenCV 4.X, but settle for whatever is installed
find_package(OpenCV 4 QUIET)
if (NOT OpenCV_FOUND)
find_package(OpenCV REQUIRED)
endif ()
message(STATUS "Found OpenCV version ${OpenCV_VERSION}")
add_executable(camera_capture src/CameraMain.cpp)
ament_target_dependencies(camera_capture rclcpp image_transport OpenCV std_msgs sensor_msgs library_pkg)
ament_export_dependencies(rclcpp image_transport OpenCV std_msgs sensor_msgs library_pkg)
install(TARGETS
DESTINATION lib/${PROJECT_NAME}
)
ament_package()
I am not sure what I am doing wrong. I would appricate any help or guidance.