MacOS High Sierra下PCL迭代最近点编译报错:x86_64架构未定义符号
Hey there, let's work through this undefined symbols issue you're facing when compiling your ICP code. This error typically pops up when the linker can't find the implementation for pcl::KdTreeFLANN—most often because the required PCL libraries aren't being properly linked in your build setup. Here are the steps to fix it:
1. Double-Check Your CMakeLists.txt
The biggest culprit here is usually missing PCL components in your build configuration. Make sure your CMakeLists.txt explicitly includes the kdtree component (since that's where KdTreeFLANN lives) and links all required PCL libraries. Here's a corrected example:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR) project(iterative_closest_point) # Ensure we pull in the necessary PCL components find_package(PCL 1.8 REQUIRED COMPONENTS common io kdtree icp) include_directories(${PCL_INCLUDE_DIRS}) link_directories(${PCL_LIBRARY_DIRS}) add_definitions(${PCL_DEFINITIONS}) add_executable(iterative_closest_point iterative_closest_point.cpp) # Link against all detected PCL libraries target_link_libraries(iterative_closest_point ${PCL_LIBRARIES})
Without specifying kdtree in the find_package call, the linker won't know to include that library, leading to the undefined symbols error you're seeing.
2. Verify Your PCL Installation
On MacOS High Sierra, sometimes pre-built PCL packages (like those from Homebrew) can have missing or mismatched components. Try reinstalling PCL to ensure all libraries are present and compatible with x86_64 architecture:
brew reinstall pcl --build-from-source
Building from source ensures the library is compiled specifically for your system's architecture, avoiding any pre-built package mismatches.
3. Enforce x86_64 Architecture in CMake
MacOS High Sierra uses x86_64 by default, but if your build system is trying to target a different architecture (unlikely, but possible), you can explicitly set it in your CMakeLists.txt by adding:
set(CMAKE_OSX_ARCHITECTURES "x86_64")
This ensures your executable and linked libraries match the same architecture, preventing symbol resolution issues.
4. Clean and Rebuild from Scratch
Old build artifacts can sometimes cause weird linker issues. Wipe your build directory and start fresh:
rm -rf build && mkdir build && cd build && cmake .. && make
Why This Happens
The error message tells us the linker can't find the setInputCloud method for KdTreeFLANN. This method is implemented in the pcl_kdtree library—so if that library isn't included in your link step, the linker has no way to resolve the symbol. The fixes above ensure that library is properly pulled into your build.
内容的提问来源于stack exchange,提问作者Valerio M

