6

我收到以下链接器错误:

cd /Users/mhoggan/Development/bluenote/image_pose_adjustment/image_pose_adjustment/native/graph/cmake-build-debug/src && /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_link_script CMakeFiles/graph_optimization.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++    -std=c++11 -Wall -Werror -O3 -DNDEBUG -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/graph_optimization.dir/main.cpp.o CMakeFiles/graph_optimization.dir/BundleRigidGraphOptimizer.cpp.o CMakeFiles/graph_optimization.dir/FlexibleGraphOptimizer.cpp.o CMakeFiles/graph_optimization.dir/LoopClosure.cpp.o CMakeFiles/graph_optimization.dir/PoseJsonReader.cpp.o  -o graph_optimization  -L/Users/mhoggan/Development/bluenote/image_pose_adjustment/image_pose_adjustment/native/graph/cmake-build-debug/g2o-src/lib  /usr/local/lib/libboost_filesystem-mt.a /usr/local/lib/libboost_system-mt.a -lg2o_cli -lg2o_core -lg2o_interface -lg2o_parser -lg2o_solver_csparse -lg2o_solver_dense -lg2o_solver_pcg -lg2o_stuff -lg2o_types_icp -lg2o_types_sba -lg2o_types_sim3 -lg2o_types_slam2d -lg2o_types_slam3d -lg2o_csparse_extension -lcsparse -ljsoncpp -Wl,-rpath,/Users/mhoggan/Development/bluenote/image_pose_adjustment/image_pose_adjustment/native/graph/cmake-build-debug/g2o-src/lib 
Undefined symbols for architecture x86_64:
  "g2o::csparse_extension::cs_cholsolsymb(cs_sparse const*, double*, cs_symbolic const*, double*, int*)", referenced from:
      g2o::LinearSolverCSparse<Eigen::Matrix<double, -1, -1, 0, -1, -1> >::solve(g2o::SparseBlockMatrix<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, double*, double*) in LoopClosure.cpp.o
  "g2o::csparse_extension::writeCs2Octave(char const*, cs_sparse const*, bool)", referenced from:
      g2o::LinearSolverCSparse<Eigen::Matrix<double, -1, -1, 0, -1, -1> >::solve(g2o::SparseBlockMatrix<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, double*, double*) in LoopClosure.cpp.o
  "g2o::csparse_extension::cs_chol_workspace(cs_sparse const*, cs_symbolic const*, int*, double*)", referenced from:
      g2o::LinearSolverCSparse<Eigen::Matrix<double, -1, -1, 0, -1, -1> >::solveBlocks(double**&, g2o::SparseBlockMatrix<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&) in LoopClosure.cpp.o
      g2o::LinearSolverCSparse<Eigen::Matrix<double, -1, -1, 0, -1, -1> >::solvePattern(g2o::SparseBlockMatrix<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, std::__1::vector<std::__1::pair<int, int>, std::__1::allocator<std::__1::pair<int, int> > > const&, g2o::SparseBlockMatrix<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&) in LoopClosure.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

与 g2o 链接时。

该库列在它所链接的库中,并且 nm 显示符号在那里。

mhoggan-C02S81PRG8WM:graph mhoggan$ nm /Users/mhoggan/Development/bluenote/image_pose_adjustment/image_pose_adjustment/native/graph/cmake-build-debug/g2o-src/lib/libg2o_csparse_extension.dylib | grep cs_cholsolsymb
0000000000002120 T __ZN3g2o17csparse_extension14cs_cholsolsymbEPK12cs_di_sparsePdPK14cs_di_symbolicS4_Pi
mhoggan-C02S81PRG8WM:graph mhoggan$ nm /Users/mhoggan/Development/bluenote/image_pose_adjustment/image_pose_adjustment/native/graph/cmake-build-debug/g2o-src/lib/libg2o_csparse_extension.dylib | grep writeCs2Octave
00000000000027d0 T __ZN3g2o17csparse_extension14writeCs2OctaveEPKcPK12cs_di_sparseb
mhoggan-C02S81PRG8WM:graph mhoggan$ nm /Users/mhoggan/Development/bluenote/image_pose_adjustment/image_pose_adjustment/native/graph/cmake-build-debug/g2o-src/lib/libg2o_csparse_extension.dylib | grep cs_chol_workspace
0000000000002210 T __ZN3g2o17csparse_extension17cs_chol_workspaceEPK12cs_di_sparsePK14cs_di_symbolicPiPd

要重现此问题,请按照以下步骤操作:

brew update

// basic dependencies
brew install pkg-config cmake git

// g2o dependencies
brew install suite-sparse

// OpenCV dependencies and OpenCV
brew install eigen
brew install ffmpeg
brew install opencv

// other dependencies
brew install yaml-cpp glog gflags

// Node.js
brew install node

现在,要获取链接器错误:

cd /path/to/working/dir
git clone https://github.com/RainerKuemmerle/g2o.git
cd g2o
git checkout 9b41a4ea5ade8e1250b9c1b279f3a9c098811b5a
mkdir build && cd build
cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr/local \
    -DCMAKE_CXX_FLAGS=-std=c++11 \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_UNITTESTS=OFF \
    -DG2O_USE_CHOLMOD=OFF \
    -DG2O_USE_CSPARSE=ON \
    -DG2O_USE_OPENGL=OFF \
    -DG2O_USE_OPENMP=ON \
    ..
make -j4
make install

这应该会产生上面记录的链接器错误。

4

1 回答 1

1

从原始报告中脱颖而出的一件事是缺少的符号是:

g2o::csparse_extension::cs_cholsolsymb(cs_sparse const*, double*, cs_symbolic const*, double*, int*)
g2o::csparse_extension::writeCs2Octave(char const*, cs_sparse const*, bool)
g2o::csparse_extension::cs_chol_workspace(cs_sparse const*, cs_symbolic const*, int*, double*)

最初的记者认为这些符号是在他们的构建输出中定义的libg2o_csparse_extension.dylib

__ZN3g2o17csparse_extension14cs_cholsolsymbEPK12cs_di_sparsePdPK14cs_di_symbolicS4_Pi
__ZN3g2o17csparse_extension14writeCs2OctaveEPKcPK12cs_di_sparseb
__ZN3g2o17csparse_extension17cs_chol_workspaceEPK12cs_di_sparsePK14cs_di_symbolicPiPd

但是,如果您对这些符号进行 demangle(c++filt例如使用),您可以看到这些符号实际上是:

g2o::csparse_extension::cs_cholsolsymb(cs_di_sparse const*, double*, cs_di_symbolic const*, double*, int*)
g2o::csparse_extension::writeCs2Octave(char const*, cs_di_sparse const*, bool)
g2o::csparse_extension::cs_chol_workspace(cs_di_sparse const*, cs_di_symbolic const*, int*, double*)

区别在于cs_sparseandcs_symboliccs_di_sparseand的用法cs_di_symbolic

现在,在我的 Mac 上,运行您提供的命令运行良好(与 @alex-reinking 关于<omp.h>和 GCC 的注意事项相同),但构建的库包含带有 的符号cs_di_*,即使cs_di_*源代码中没有提及.

深入挖掘(使用-DCMAKE_EXPORT_COMPILE_COMMANDS=1来获取实际的构建命令)我发现我已经suite-sparse安装了。该库在 处提供了一个标头/usr/local/include/cs.h,它具有以下两个定义:

typedef struct cs_di_sparse
{
...
} cs_di ;

typedef struct cs_di_symbolic
{
...
} cs_dis ;

和定义:

#define cs cs_di
#define css CS_NAME (s)
#define CS_NAME(nm) cs_di ## nm

这意味着该g2o项目将使用该已安装版本(SuiteSparse如果可用)正确构建。否则,它将使用包含的(较旧的)EXTERNAL/csparse/cs.h标头构建。

卸载后suite-sparse,运行您提供的命令仍然可以正常工作,所以我的结论是:

  • 您的一些翻译单元是根据包含的cs.h
  • 有些人正在查看系统上其他地方的已安装版本

要隔离这一点,请准确查看哪些对象/库使用了这些符号的哪个版本,nm并为每个对象制定构建命令。

于 2021-11-07T06:21:56.340 回答