I'm working on a ROS2 Humble project. As always, I created the ament_cmake package, wrote a C++ node with Torch Scripts, and made Cmake - but I got this error: undefined reference to `rclcpp::Node::Node(std::string const&, rclcpp::NodeOptions const&)'. As far as I understand, it's all about the linker - for some reason it doesn't see the ROS methods. I've tried a lot, but I still don't fully understand what the problem is. Colcon build throws an error every time it is run. Here is my Cmake file:
cmake_minimum_required(VERSION 3.10)
project(tinker_publisher_udp)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# Find ROS 2 packages
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(CUDAToolkit REQUIRED)
include_directories(${CUDAToolkit_INCLUDE_DIRS})
set(Torch_DIR "/home/user1/libtorch/libtorch/share/cmake/Torch")
list(APPEND CMAKE_PREFIX_PATH "${libtorch_path}")
find_package(Torch REQUIRED)
add_executable(tinker_publisher_udp src/tinker_publisher_udp.cpp)
ament_target_dependencies(tinker_publisher_udp
rclcpp
)
include_directories(
${Torch_INCLUDE_DIRS}
)
target_link_libraries(tinker_publisher_udp
"${TORCH_LIBRARIES}"
rclcpp::rclcpp # Ensure this line is present
)
install(TARGETS tinker_publisher_udp
DESTINATION lib/${PROJECT_NAME}
)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
user1@desktop:~/my_test/LocomotionWithNP3O-masteroldx/ros2$ colcon build --packages-select tinker_publisher_udp
[0.312s] WARNING:colcon.colcon_ros.prefix_path.ament:The path '/home/user1/my_test/LocomotionWithNP3O-masteroldx/ros2/install/tinker_mujoco_sim' in the environment variable AMENT_PREFIX_PATH doesn't exist
[0.312s] WARNING:colcon.colcon_ros.prefix_path.ament:The path '/home/user1/my_test/LocomotionWithNP3O-masteroldx/ros2/install/test' in the environment variable AMENT_PREFIX_PATH doesn't exist
[0.313s] WARNING:colcon.colcon_ros.prefix_path.catkin:The path '/home/user1/my_test/LocomotionWithNP3O-masteroldx/ros2/install/test' in the environment variable CMAKE_PREFIX_PATH doesn't exist
Starting >>> tinker_publisher_udp
--- stderr: tinker_publisher_udp
CMake Warning at /home/user1/libtorch/libtorch/share/cmake/Caffe2/public/cuda.cmake:140 (message):
Failed to compute shorthash for libnvrtc.so
Call Stack (most recent call first):
/home/user1/libtorch/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:86 (include)
/home/user1/libtorch/libtorch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)
CMakeLists.txt:17 (find_package)
CMake Warning (dev) at /usr/local/cmake-3.31.7/share/cmake-3.31/Modules/FindPackageHandleStandardArgs.cmake:441 (message):
The package name passed to `find_package_handle_standard_args` (nvtx3) does
not match the name of the calling package (Caffe2). This can lead to
problems in calling code that expects `find_package` result variables
(e.g., `_FOUND`) to follow a certain pattern.
Call Stack (most recent call first):
/home/user1/libtorch/libtorch/share/cmake/Caffe2/public/cuda.cmake:178 (find_package_handle_standard_args)
/home/user1/libtorch/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:86 (include)
/home/user1/libtorch/libtorch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)
CMakeLists.txt:17 (find_package)
This warning is for project developers. Use -Wno-dev to suppress it.
CMake Warning at /home/user1/libtorch/libtorch/share/cmake/Caffe2/public/cuda.cmake:184 (message):
Cannot find NVTX3, find old NVTX instead
Call Stack (most recent call first):
/home/user1/libtorch/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:86 (include)
/home/user1/libtorch/libtorch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)
CMakeLists.txt:17 (find_package)
/usr/bin/ld: CMakeFiles/tinker_publisher_udp.dir/src/tinker_publisher_udp.cpp.o: in function `main':
tinker_publisher_udp.cpp:(.text+0x360): undefined reference to `rclcpp::shutdown(std::shared_ptr<rclcpp::Context>, std::string const&)'
/usr/bin/ld: CMakeFiles/tinker_publisher_udp.dir/src/tinker_publisher_udp.cpp.o: in function `rclcpp::ParameterTypeException::ParameterTypeException(rclcpp::ParameterType, rclcpp::ParameterType)':
tinker_publisher_udp.cpp:(.text._ZN6rclcpp22ParameterTypeExceptionC2ENS_13ParameterTypeES1_[_ZN6rclcpp22ParameterTypeExceptionC5ENS_13ParameterTypeES1_]+0x3e): undefined reference to `rclcpp::to_string(rclcpp::ParameterType)'
/usr/bin/ld: tinker_publisher_udp.cpp:(.text._ZN6rclcpp22ParameterTypeExceptionC2ENS_13ParameterTypeES1_[_ZN6rclcpp22ParameterTypeExceptionC5ENS_13ParameterTypeES1_]+0x50): undefined reference to `rclcpp::to_string(rclcpp::ParameterType)'
/usr/bin/ld: CMakeFiles/tinker_publisher_udp.dir/src/tinker_publisher_udp.cpp.o: in function `auto rclcpp::Node::declare_parameter<std::string>(std::string const&, std::string const&, rcl_interfaces::msg::ParameterDescriptor_<std::allocator<void> > const&, bool)':
tinker_publisher_udp.cpp:(.text._ZN6rclcpp4Node17declare_parameterISsEEDaRKSsRKT_RKN14rcl_interfaces3msg20ParameterDescriptor_ISaIvEEEb[_ZN6rclcpp4Node17declare_parameterISsEEDaRKSsRKT_RKN14rcl_interfaces3msg20ParameterDescriptor_ISaIvEEEb]+0x69): undefined reference to `rclcpp::ParameterValue::ParameterValue(std::string const&)'
/usr/bin/ld: tinker_publisher_udp.cpp:(.text._ZN6rclcpp4Node17declare_parameterISsEEDaRKSsRKT_RKN14rcl_interfaces3msg20ParameterDescriptor_ISaIvEEEb[_ZN6rclcpp4Node17declare_parameterISsEEDaRKSsRKT_RKN14rcl_interfaces3msg20ParameterDescriptor_ISaIvEEEb]+0x90): undefined reference to `rclcpp::Node::declare_parameter(std::string const&, rclcpp::ParameterValue const&, rcl_interfaces::msg::ParameterDescriptor_<std::allocator<void> > const&, bool)'
/usr/bin/ld: CMakeFiles/tinker_publisher_udp.dir/src/tinker_publisher_udp.cpp.o: in function `auto rclcpp::Node::declare_parameter<int>(std::string const&, int const&, rcl_interfaces::msg::ParameterDescriptor_<std::allocator<void> > const&, bool)':
tinker_publisher_udp.cpp:(.text._ZN6rclcpp4Node17declare_parameterIiEEDaRKSsRKT_RKN14rcl_interfaces3msg20ParameterDescriptor_ISaIvEEEb[_ZN6rclcpp4Node17declare_parameterIiEEDaRKSsRKT_RKN14rcl_interfaces3msg20ParameterDescriptor_ISaIvEEEb]+0x8a): undefined reference to `rclcpp::Node::declare_parameter(std::string const&, rclcpp::ParameterValue const&, rcl_interfaces::msg::ParameterDescriptor_<std::allocator<void> > const&, bool)'
/usr/bin/ld: CMakeFiles/tinker_publisher_udp.dir/src/tinker_publisher_udp.cpp.o: in function `RL_Tinymal_UDP::RL_Tinymal_UDP()':
tinker_publisher_udp.cpp:(.text._ZN14RL_Tinymal_UDPC2Ev[_ZN14RL_Tinymal_UDPC5Ev]+0xb7): undefined reference to `rclcpp::Node::Node(std::string const&, rclcpp::NodeOptions const&)'
/usr/bin/ld: tinker_publisher_udp.cpp:(.text._ZN14RL_Tinymal_UDPC2Ev[_ZN14RL_Tinymal_UDPC5Ev]+0x6a1): undefined reference to `rclcpp::Node::get_parameter(std::string const&) const'
/usr/bin/ld: tinker_publisher_udp.cpp:(.text._ZN14RL_Tinymal_UDPC2Ev[_ZN14RL_Tinymal_UDPC5Ev]+0x6b0): undefined reference to `rclcpp::Parameter::as_string() const'
/usr/bin/ld: tinker_publisher_udp.cpp:(.text._ZN14RL_Tinymal_UDPC2Ev[_ZN14RL_Tinymal_UDPC5Ev]+0x747): undefined reference to `rclcpp::Node::get_parameter(std::string const&) const'
/usr/bin/ld: tinker_publisher_udp.cpp:(.text._ZN14RL_Tinymal_UDPC2Ev[_ZN14RL_Tinymal_UDPC5Ev]+0x7e1): undefined reference to `rclcpp::Node::get_parameter(std::string const&) const'
/usr/bin/ld: tinker_publisher_udp.cpp:(.text._ZN14RL_Tinymal_UDPC2Ev[_ZN14RL_Tinymal_UDPC5Ev]+0x7f0): undefined reference to `rclcpp::Parameter::as_string() const'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/tinker_publisher_udp.dir/build.make:164: tinker_publisher_udp] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:153: CMakeFiles/tinker_publisher_udp.dir/all] Error 2
gmake: *** [Makefile:146: all] Error 2
---
Failed <<< tinker_publisher_udp [25.1s, exited with code 2]
I tried to change Cmake, but it seemed to have no effect.
I also noticed one interesting feature that if you remove work with ROS2 or work with Torch from the node code and try colcon build again, everything will be copied.