编译PyMesh(PyMesh2):可重定位代码链接问题求助
我正在编译PyMesh(PyMesh2),这是一个依赖CGAL、draco、mmg等第三方库的大型图形包。已通过降级至mmg 5.3.6和CGAL 4.10解决了多个编译错误,但在最终链接阶段失败,报错如下:
/usr/bin/ld:
/usr/local/lib/libdraco.a(sequential_integer_attribute_encoder.cc.o):
warning: relocation against
_ZTVN5draco36MeshPredictionSchemeTexCoordsEncoderIiNS_37PredictionSchemeWrapEncodingTransformIiiEENS_24MeshPredictionSchemeDataINS_24MeshAttributeCornerTableEEEEE'
in read-only section
.text._ZN5draco36MeshPredictionSchemeTexCoordsEncoderIiNS_37PredictionSchemeWrapEncodingTransformIiiEENS_24MeshPredictionSchemeDataINS_24MeshAttributeCornerTableEEEED0Ev[_ZN5draco36MeshPredictionSchemeTexCoordsEncoderIiNS_37PredictionSchemeWrapEncodingTransformIiiEENS_24MeshPredictionSchemeDataINS_24MeshAttributeCornerTableEEEED5Ev]'
/usr/bin/ld: /usr/local/lib/libdraco.a(decode.cc.o): relocation
R_X86_64_PC32 against symbol `_ZTVN5draco23PointCloudKdTreeDecoderE'
can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value collect2: error: ld returned
1 exit status make[2]: ***
[tools/Compression/CMakeFiles/lib_Compression.dir/build.make:117:
/home/me/PyMesh/python/pymesh/lib/libPyMesh-Compression.so] Error 1
make[1]: *** [CMakeFiles/Makefile2:1931:
tools/Compression/CMakeFiles/lib_Compression.dir/all] Error 2 make:
*** [Makefile:91: all] Error 2
我尝试在draco的CMakeLists.txt和主项目CMakeLists.txt中添加set(CMAKE_POSITION_INDEPENDENT_CODE ON),但引发了更早的CGAL兼容性错误:
/home/me/PyMesh/python/pymesh/third_party/include/CGAL/internal/boost/array_binary_tree.hpp:54:28:
error: expected template-name before ‘<’ token 54 | :
::boost::iterator<std::forward_iterator_tag, | ^
/home/me/PyMesh/python/pymesh/third_party/include/CGAL/internal/boost/array_binary_tree.hpp:54:28:
error: expected ‘{’ before ‘<’ token
/home/me/PyMesh/python/pymesh/third_party/include/CGAL/internal/boost/array_binary_tree.hpp:54:28:
error: expected unqualified-id before ‘<’ token In file included from
/home/me/PyMesh/tools/Tetrahedralization/CGAL/CGALMeshGen.h:11:
/home/me/PyMesh/tools/Tetrahedralization/CGAL/CGALMeshDomain.h: In
function ‘auto PyMesh::create_implicit_domain(const MatrixFr&, const
MatrixIr&, Oracle&)’:
/home/me/PyMesh/tools/Tetrahedralization/CGAL/CGALMeshDomain.h:218:49:
error: wrong number of template arguments (1, should be at least 2)
218 | using Domain = CGAL::Labeled_mesh_domain_3; | ^
我也尝试在主构建文件中设置set(CMAKE_POSITION_INDEPENDENT_CODE OFF),但问题仍未解决。我使用的是Ubuntu 24.04.3系统,之前曾在Ubuntu 20.x版本上成功编译过该项目,希望能得到后续解决思路。
解决思路建议
- 单独编译draco并启用-fPIC:进入draco的源码目录,执行
cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON ..后重新编译安装,确保生成的静态库包含位置无关代码,避免全局开启PIE影响CGAL编译。 - 适配CGAL与Boost版本:Ubuntu24.04自带的Boost版本较高,CGAL4.10可能存在兼容性问题。可尝试安装CGAL4.10推荐的Boost版本(如1.65.x),或修改CGAL头文件
array_binary_tree.hpp中的迭代器定义,补充缺失的模板参数或调整命名空间引用。 - 针对特定模块开启PIE:不在主CMakeLists中全局设置
CMAKE_POSITION_INDEPENDENT_CODE,仅在tools/Compression/CMakeLists.txt中添加该选项,让依赖draco的模块单独启用位置无关代码,不影响CGAL相关模块。 - 切换旧版本编译器:Ubuntu24.04默认gcc版本为12,而Ubuntu20.x为gcc9。安装gcc9并通过
update-alternatives切换编译器后重新编译,降低版本兼容性问题。
内容的提问来源于stack exchange,提问作者Kit Wolf

