ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

[ROS2] Including a cpp header from another package

asked 2020-09-25 09:20:33 -0600

Majed gravatar image

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-09-25 12:24:36 -0600

mirella melo gravatar image

Hi. Seems that "camera_pkg" explicitly depends on "library_pkg" which depends on "export_library_pkg". Consider adding in your config file related to library_pkg :

 find_package(export_library_pkg REQUIRED)
 ament_target_dependencies(${PROJECT_NAME} 
              .... 
              export_library_pkg)
edit flag offensive delete link more

Comments

When I add this to the Cmakelist of the camera-pkg I got and error

CMake Error at CMakeLists.txt:22 (find_package):
  By not providing "Findexport_library_pkg.cmake" in CMAKE_MODULE_PATH this
  project has asked CMake to find a package configuration file provided by
  "export_library_pkg", but CMake did not find one.

  Could not find a package configuration file provided by
  "export_library_pkg" with any of the following names:

    export_library_pkgConfig.cmake
    export_library_pkg-config.cmake

  Add the installation prefix of "export_library_pkg" to CMAKE_PREFIX_PATH or
  set "export_library_pkg_DIR" to a directory containing one of the above
  files.  If "export_library_pkg" provides a separate development package or
  SDK, be sure it has been installed.


make: *** [cmake_check_build_system] Error 1
Majed gravatar image Majed  ( 2020-09-25 13:43:46 -0600 )edit
1

Hmmm, are you aware about export_library_pkg? I mean, it is a package? Where is it? But, anyway, I just notice that this is a warning, not an error. So, now I'm thinking the problem is in "add_executable(...)" from the camera_pkg config file. This error you're getting is really similar to one I was having when I was porting a C++ project to ROS2. I was not adding/linking all .cpp files needed, so I got ''undefined reference to...'' everywhere. rs. To solve that, I looked in my main file to "all" #includes <file>.cpp called (related to the project), and started adding them in "add_executable(...)", until all "undefined reference to..." was gone! For example, I believe that inside CameraMain.cpp there is a #include for CameraCapture.cpp. So CameraCapture.cpp is necessary to be in "add_executable(... src/CameraCapture.cpp)". I hope this is causing your error, and I ...(more)

mirella melo gravatar image mirella melo  ( 2020-09-25 14:19:47 -0600 )edit

Honestly, I am very new to ROS2 so I might be missing so key information, like the location of export_library_pkg.

As to making add_executable to CameraCapture how would this be done in ROS2 CMakeList. Giving that CameraCapture does not contain any nodes. Is it acceptable in ROS2 to add executable to none nodes?

Thank you for your help.

Majed gravatar image Majed  ( 2020-09-26 10:35:43 -0600 )edit

Thank you @mirella-melo. You have given me a hint into how to compile it. I have just shifted the Classes around and it has done the trick.

Majed gravatar image Majed  ( 2020-09-27 04:05:49 -0600 )edit
1

This is great news! I was thinking of a better way to answer you, but I'm happy that you solved it first!! So, just to make it clear, you "left behind" this warning about "export_library_pkg", and focused on including the external classes in which you explicitly used in "CameraMain", that is it? Then I can edit the main comment in case others face the same issue. Tks! :)

mirella melo gravatar image mirella melo  ( 2020-09-27 10:12:49 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-09-25 09:20:33 -0600

Seen: 4,286 times

Last updated: Sep 25 '20