使用pybind11封装Draco库时链接失败问题咨询
Hey there! Let's break down why you're hitting that undefined symbol: _ZN5draco4MeshC1Ev error and fix it up.
What's Causing the Issue?
That error means your Python extension module can't find the constructor for draco::Mesh. Even though you added add_subdirectory(draco) to your CMakeLists.txt, CMake only knows to build Draco's libraries—it doesn't automatically link those libraries to your pybind11 module. You've told CMake to compile Draco, but not to include its code in your final extension file.
How to Fix It
You need to explicitly link Draco's library to your pybind11 module using target_link_libraries. Here's the updated CMakeLists.txt you should use:
cmake_minimum_required(VERSION 3.10.0) project(test) # Build Draco's libraries from its source directory add_subdirectory(draco) # Add pybind11's build configuration add_subdirectory(pybind11) # Create your pybind11 extension module pybind11_add_module(test src/main.cpp ) # Link Draco's library to your module (this is the missing piece!) target_link_libraries(test PRIVATE draco)
Let's Break This Down
add_subdirectory(draco): This tells CMake to process Draco's internal CMake files and build its core libraries (by default, Draco builds static libraries, which works perfectly for this use case).target_link_libraries(test PRIVATE draco): This is the critical line. It instructs CMake to bundle Draco's compiled code (including symbols like thedraco::Meshconstructor) into your final.somodule. Now when Python imports your module, it can find all the Draco-related symbols it needs.
Verify the Fix
After updating your CMakeLists.txt, re-run your build steps:
mkdir -p build && cd build cmake .. make
Try importing your module in Python again—this should resolve the undefined symbol error.
Bonus Tip (If You Need Shared Libraries)
If you ever want to build Draco as a shared library instead of static, add this line before add_subdirectory(draco):
set(BUILD_SHARED_LIBS ON CACHE BOOL "Build shared libraries" FORCE)
But for most pybind11 wrapping scenarios, linking the static library works smoothly without extra setup.
内容的提问来源于stack exchange,提问作者binbin

