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

PCL表面重建代码编译出现undefined reference链接错误求助

问题描述

在Ubuntu系统通过apt安装了PCL 1.12,尝试用点云做表面重建,代码如下:

#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/surface/marching_cubes_hoppe.h>

int main(int argc, char** argv)
{
  // 打开输入文本文件
  std::ifstream input_file("prereq/model.txt");

  // 创建PCL点云对象存储点数据
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);

  // 逐行解析文本文件提取点坐标
  std::string line;
  while (std::getline(input_file, line))
  {
    std::istringstream iss(line);
    float x, y, z;
    if (!(iss >> x >> y >> z))
    {
      // 处理解析错误
      break;
    }
    // 将点添加到PCL点云对象
    cloud->push_back(pcl::PointXYZ(x, y, z));
  }

   // 使用Marching Cubes算法创建三角网格
  pcl::PolygonMesh triangles;
  pcl::MarchingCubesHoppe<pcl::PointXYZ> mc;
  mc.setInputCloud(cloud);
  mc.setIsoLevel(0.025);
  mc.reconstruct(triangles);

  pcl::io::savePLYFile("output_mesh.ply", triangles);

  return 0;
}

使用以下命令编译:

g++ hmm.cpp -o a.out -I/usr/include/pcl-1.12/ -I/usr/include/eigen3 -lpcl_common -lpcl_io -lpcl_surface -lpcl_visualization -lpcl_octree -lpcl_recognition -lpcl_segmentation -lpcl_registration -lpcl_tracking -lpcl_search -lpcl_common -lpcl_surface -lpcl_kdtree -lpcl_features -lpcl_filters

出现链接错误:

/usr/bin/ld: /tmp/ccPoS1h1.o: in function `main':
hmm.cpp:(.text+0x2d1): undefined reference to `pcl::MarchingCubesHoppe<pcl::PointXYZ>::~MarchingCubesHoppe()'
/usr/bin/ld: hmm.cpp:(.text+0x3ba): undefined reference to `pcl::MarchingCubesHoppe<pcl::PointXYZ>::~MarchingCubesHoppe()'
/usr/bin/ld: /tmp/ccPoS1h1.o:(.data.rel.ro._ZTVN3pcl18MarchingCubesHoppeINS_8PointXYZEEE[_ZTVN3pcl18MarchingCubesHoppeINS_8PointXYZEEE]+0x10): undefined reference to `pcl::MarchingCubesHoppe<pcl::PointXYZ>::~MarchingCubesHoppe()'
/usr/bin/ld: /tmp/ccPoS1h1.o:(.data.rel.ro._ZTVN3pcl18MarchingCubesHoppeINS_8PointXYZEEE[_ZTVN3pcl18MarchingCubesHoppeINS_8PointXYZEEE]+0x18): undefined reference to `pcl::MarchingCubesHoppe<pcl::PointXYZ>::~MarchingCubesHoppe()'
/usr/bin/ld: /tmp/ccPoS1h1.o:(.data.rel.ro._ZTVN3pcl18MarchingCubesHoppeINS_8PointXYZEEE[_ZTVN3pcl18MarchingCubesHoppeINS_8PointXYZEEE]+0x60): undefined reference to `pcl::MarchingCubes<pcl::PointXYZ>::performReconstruction(pcl::PolygonMesh&)'
/usr/bin/ld: /tmp/ccPoS1h1.o:(.data.rel.ro._ZTVN3pcl18MarchingCubesHoppeINS_8PointXYZEEE[_ZTVN3pcl18MarchingCubesHoppeINS_8PointXYZEEE]+0x68): undefined reference to `pcl::MarchingCubes<pcl::PointXYZ>::performReconstruction(pcl::PointCloud<pcl::PointXYZ>&, std::vector<pcl::Vertices, std::allocator<pcl::Vertices> >&)'
/usr/bin/ld: /tmp/ccPoS1h1.o:(.data.rel.ro._ZTVN3pcl18MarchingCubesHoppeINS_8PointXYZEEE[_ZTVN3pcl18MarchingCubesHoppeINS_8PointXYZEEE]+0x70): undefined reference to `pcl::MarchingCubesHoppe<pcl::PointXYZ>::voxelizeData()'
/usr/bin/ld: /tmp/ccPoS1h1.o:(.data.rel.ro._ZTVN3pcl18MarchingCubesHoppeINS_8PointXYZEEE[_ZTVN3pcl18MarchingCubesHoppeINS_8PointXYZEEE]+0x78): undefined reference to `pcl::MarchingCubes<pcl::PointXYZ>::getGridValue(Eigen::Matrix<int, 3, 1, 0, 3, 1>)'
/usr/bin/ld: /tmp/ccPoS1h1.o:(.data.rel.ro._ZTVN3pcl13MarchingCubesINS_8PointXYZEEE[_ZTVN3pcl13MarchingCubesINS_8PointXYZEEE]+0x60): undefined reference to `pcl::MarchingCubes<pcl::PointXYZ>::performReconstruction(pcl::PolygonMesh&)'
/usr/bin/ld: /tmp/ccPoS1h1.o:(.data.rel.ro._ZTVN3pcl13MarchingCubesINS_8PointXYZEEE[_ZTVN3pcl13MarchingCubesINS_8PointXYZEEE]+0x68): undefined reference to `pcl::MarchingCubes<pcl::PointXYZ>::performReconstruction(pcl::PointCloud<pcl::PointXYZ>&, std::vector<pcl::Vertices, std::allocator<pcl::Vertices> >&)'
/usr/bin/ld: /tmp/ccPoS1h1.o:(.data.rel.ro._ZTVN3pcl13MarchingCubesINS_8PointXYZEEE[_ZTVN3pcl13MarchingCubesINS_8PointXYZEEE]+0x78): undefined reference to `pcl::MarchingCubes<pcl::PointXYZ>::getGridValue(Eigen::Matrix<int, 3, 1, 0, 3, 1>)'
collect2: error: ld returned 1 exit status

也尝试过CMake构建:

cmake_minimum_required(VERSION 3.10)
project(pointcloud-to-mesh)
find_package(PCL 1.12 REQUIRED COMPONENTS common io surface)
add_executable(pointcloud-to-mesh hmm.cpp)
target_include_directories(pointcloud-to-mesh PRIVATE ${PCL_INCLUDE_DIRS})
target_link_libraries(pointcloud-to-mesh PRIVATE ${PCL_LIBRARIES})

使用GreedyProjectionTriangulation和Poisson方法时也出现同类链接错误,求问题原因及解决方法。

问题原因与解决方法

核心原因

Ubuntu apt仓库提供的PCL预编译包默认未编译表面重建相关的可选模块,包括Marching Cubes、Poisson、GreedyProjectionTriangulation等算法的实现代码,不在默认链接的库文件中。

具体解决步骤

方法1:手动编译PCL源码(推荐)

  1. 安装编译依赖:
sudo apt-get install git build-essential libboost-all-dev libeigen3-dev libflann-dev libvtk7-dev libqhull-dev libusb-1.0-0-dev libopenni2-dev
  1. 克隆PCL 1.12源码并切换到对应版本:
git clone https://github.com/PointCloudLibrary/pcl.git
cd pcl
git checkout pcl-1.12.1
  1. 创建编译目录并配置CMake:
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_all_modules=ON ..

BUILD_all_modules=ON会编译所有模块,确保表面重建相关代码被包含;若只想编译必要模块,可指定BUILD_surface=ON并补充依赖模块如BUILD_features=ON。
4. 编译并安装:

make -j$(nproc)
sudo make install

编译完成后,重新用CMake构建项目即可正常链接。

方法2:优化CMake配置(源码编译后生效)

确保CMakeLists.txt包含所有依赖组件,比如使用Poisson时需要features组件,修改后的配置如下:

cmake_minimum_required(VERSION 3.10)
project(pointcloud-to-mesh)
find_package(PCL 1.12 REQUIRED COMPONENTS common io surface features)
add_executable(pointcloud-to-mesh hmm.cpp)
target_link_libraries(pointcloud-to-mesh PRIVATE ${PCL_LIBRARIES})

无需手动指定target_include_directories,find_package(PCL)会自动处理包含路径。

方法3:调整手动编译命令(不推荐)

若坚持用g++手动编译,需确保链接所有依赖库,并注意库的顺序(依赖库放在被依赖库之后),示例命令:

g++ hmm.cpp -o a.out -I/usr/local/include/pcl-1.12 -I/usr/include/eigen3 -L/usr/local/lib -lpcl_surface -lpcl_features -lpcl_search -lpcl_kdtree -lpcl_io -lpcl_common -lboost_system -lboost_filesystem

这种方式易遗漏依赖,不如CMake便捷。

内容的提问来源于stack exchange,提问作者bipin krishna

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 21:02:53