环境: UBUNTU20.04 GTSAM4.0.3 EIGEN3.4.0 ROS-NOETIC
在项目中链接GTSAM时,编译可以通过,但是每次运行节点都会直接中断,gdb调试信息如下:
Program received signal SIGSEGV, Segmentation fault.
__GI___libc_free (mem=0x20) at malloc.c:3103
3103 malloc.c: No such file or directory.
(gdb) bt
#0 __GI___libc_free (mem=0x20) at malloc.c:3103
#1 0x00007ffff7829d2c in gtsam::noiseModel::Diagonal::Sigmas(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&, bool) () from /usr/local/lib/libgtsam.so.4
#2 0x00007ffff7660c34 in _GLOBAL__sub_I_lago.cpp () from /usr/local/lib/libgtsam.so.4
#3 0x00007ffff7de5733 in call_init (env=0x7fffffffd7b8, argv=0x7fffffffd7a8, argc=1, l=<optimized out>) at dl-init.c:72
#4 _dl_init (main_map=0x7ffff7ffe170, argc=1, argv=0x7fffffffd7a8, env=0x7fffffffd7b8) at dl-init.c:119
#5 0x00007ffff7dd60ca in _dl_start_user () from /lib64/ld-linux-x86-64.so.2
#6 0x0000000000000001 in ?? ()
#7 0x00007fffffffdc69 in ?? ()
#8 0x0000000000000000 in ?? ()
该类问题由GTSAM编译时配置的“march=native”导致,只需修改GTSAM的cmake文件夹下的GtsamBuildTypes.cmake文件即可,需关闭“march=native”,具体修改如下:
修改前:
if (NOT MSVC)
option(GTSAM_BUILD_WITH_MARCH_NATIVE "Enable/Disable building with all instructions supported by native architecture (binary may not be portable!)" ON)
if(GTSAM_BUILD_WITH_MARCH_NATIVE)
# Add as public flag so all dependant projects also use it, as required
# by Eigen to avid crashes due to SIMD vectorization:
list_append_cache(GTSAM_COMPILE_OPTIONS_PUBLIC "-march=native")
endif()
endif()
修改后(将ON置为OFF):
if (NOT MSVC)
option(GTSAM_BUILD_WITH_MARCH_NATIVE "Enable/Disable building with all instructions supported by native architecture (binary may not be portable!)" OFF)
if(GTSAM_BUILD_WITH_MARCH_NATIVE)
# Add as public flag so all dependant projects also use it, as required
# by Eigen to avid crashes due to SIMD vectorization:
list_append_cache(GTSAM_COMPILE_OPTIONS_PUBLIC "-march=native")
endif()
endif()
然后重新编译安装GTSAM即可:
mkdir build
cd build
cmake ..
make -j8
sudo make install
由于关闭了“march=native”,可能会导致GTSAM性能下降,可在本地项目的Cmakelists.txt中添加:
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -march=native")
至此,大功告成!
参考链接:GTSAM segfault on noiseModel::Diagonal::Sigmas · Issue #75 · borglab/gtsam · GitHub