You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ROS2中使用PCL VoxelGrid遇setInputCloud未定义引用问题求助

问题:PCL VoxelGrid滤波触发未定义引用错误

从ROS消息提取点云后,使用PCL库的VoxelGrid进行滤波时,无论使用自主编写的代码还是参考网上教程的代码,均出现pcl::PCLBase<pcl::PCLPointCloud2>::setInputCloud(std::shared_ptr<pcl::PCLPointCloud2 const> const&)的未定义引用错误。

代码示例

自主编写的代码

pcl::PCLBase<pcl::PCLPointCloud2>::PCLPointCloud2ConstPtr cloudPtr(cloud_pc2);
pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
sor.setInputCloud(cloudPtr);
sor.setLeafSize(0.1f, 0.1f, 0.1f);
sor.filter(cloud_filtered);

参考教程的代码

pcl::PCLPointCloud2::Ptr cloud_filtered (new pcl::PCLPointCloud2 ());
pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
sor.setInputCloud (cloud);
sor.setLeafSize (0.01f, 0.01f, 0.01f);
sor.filter (*cloud_filtered);

CMakeLists配置

cmake_minimum_required(VERSION 3.5)
project(loam_os2)

# 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)
  set(CMAKE_CXX_STANDARD 17)
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(std_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(PCL 1.10 REQUIRED)
find_package(pcl_conversions REQUIRED)
find_package(tf2_ros REQUIRED)
find_package(tf2 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS} include)
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable(pcl_example_node src/pcl_example.cpp src/pcl_example_node.cpp)

target_link_libraries( pcl_example_node
  ${PCL_LIBRARIES}
)

ament_target_dependencies(
  pcl_example_node
  rclcpp 
  std_msgs 
  sensor_msgs 
  pcl_conversions
  tf2
  tf2_ros
  )

install(TARGETS
  pcl_example_node
  DESTINATION lib/${PROJECT_NAME})

# Install launch files.
install(DIRECTORY
  launch
  DESTINATION share/${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 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()

完整错误信息

usr/bin/ld: CMakeFiles/pcl_example_node.dir/src/pcl_example.cpp.o: in function Pcl_example::lidar_callback(std::shared_ptr<sensor_msgs::msg::PointCloud2_<std::allocator<void> > >)':
pcl_example.cpp:(.text+0x122f): undefined reference to`pcl::PCLBase<pcl::PCLPointCloud2>::setInputCloud(std::shared_ptr<pcl::PCLPointCloud2 const> const&)'
collect2: error: ld returned 1 exit status
make[2]: * [CMakeFiles/pcl_example_node.dir/build.make:360: pcl_example_node] Error 1
make[1]: * [CMakeFiles/Makefile2:78: CMakeFiles/pcl_example_node.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
Failed   <<< loam_os2 [12.2s, exited with code 2]
Summary: 0 packages finished [12.5s]
  1 package failed: loam_os2
  1 package had stderr output: loam_os2

解决建议

  • 调整链接顺序:在ROS2的ament构建环境中,将target_link_libraries移至ament_target_dependencies之后。ament_target_dependencies会设置ROS相关的链接参数,顺序错误可能导致PCL库无法正确链接。
  • 明确指定PCL组件:修改find_package(PCL 1.10 REQUIRED)为find_package(PCL 1.10 REQUIRED COMPONENTS filters common),VoxelGrid属于filters组件,显式声明可确保链接到正确的库文件。
  • 验证PCL版本:确认系统中安装的PCL版本确实为1.10,若版本不匹配,可放宽版本限制为find_package(PCL REQUIRED),让CMake自动寻找兼容版本。
  • 清理缓存重建:删除包目录下的build、install、log文件夹,重新执行colcon build,避免旧缓存导致的链接问题。

内容的提问来源于stack exchange,提问作者Matteo Azzini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 01:40:34