Issues Linking to Library in ROS2

asked 2021-07-19 17:52:44 -0600

jdong-sw gravatar image

I'm getting some "undefined reference to..." errors when trying to build my ROS2 packages.

I'm trying to integrate some linear stages with ROS2. The motor controller for the stages comes with some drivers and sample code: https://www.newmarksystems.com/motion...

To implement this, I've created two packages in ROS2. I have one called arcus_performax, which has a header file: ArcusPerformaxDriver.h, and a source file: ArcusPerformaxDriver.c. As far as I can tell, they take advantage of libusb to provide an API for sending commands to the controller. I've put the header file inside the arcus_performax/include/arcus_performax folder, and put the source file inside the arcus_performx/src folder.

I have a second package called xy_stage. I wrote a node within this package, stage_control.cpp, which references the ArcusPerformaxDriver.h file from the arcus_performax package to send commands to the controller. However, when I attempt to build these two packages, I get the error mentioned above for each of the functions from ArcusPerformaxDriver.h that I referenced.

I assume the issues are in my cmakelists files, since I'm still learning how this all works. My CMakeLists.txt inside the arcus_performax package looks like this:

    cmake_minimum_required(VERSION 3.5)
    project(arcus_performax)

    # 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()

    # find dependencies
    find_package(ament_cmake REQUIRED)
    # uncomment the following section in order to fill in
    # further dependencies manually.
    # find_package(<dependency> REQUIRED)

    include_directories(
      include
      ${colcon_INCLUDE_DIRS}
    )

    add_library(
        ${PROJECT_NAME} SHARED
        src/ArcusPerformaxDriver.c
    )

    target_include_directories(${PROJECT_NAME} PUBLIC
      "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
      "$<INSTALL_INTERFACE:include>")

    ament_export_include_directories(include)
    ament_export_libraries(export_${PROJECT_NAME})

    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
    )


    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()

    ament_package()

The CMakeLists.txt for my xy_stage package looks like this:

cmake_minimum_required(VERSION 3.5)
project(xy_stage)

# 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()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(arcus_performax REQUIRED)

add_executable(stage_control src/stage_control.cpp)
add_executable(test_node src/test_node.cpp)

ament_target_dependencies(stage_control rclcpp arcus_performax)
ament_target_dependencies(test_node rclcpp)

ament_export_dependencies(arcus_performax)

install(TARGETS stage_control
  DESTINATION lib/${PROJECT_NAME})
install(TARGETS test_node
  DESTINATION lib/${PROJECT_NAME})

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 ...
(more)
edit retag flag offensive close merge delete