libg2o linker errors on OS X

569 views Asked by At

I am getting the following linker errors:

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)

when linking against g2o.

The library is listed in those it is linking against and nm shows that the symbols are there.

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

To reproduce this issue, please follow the following steps:

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

Now, to get the linker error:

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

this should produce the linker error documented above.

1

There are 1 answers

0
msbit On

One thing that stands out, from the original report, is that the missing symbols are:

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*)

and the original reporter believed that the symbols were defined in their build output libg2o_csparse_extension.dylib:

__ZN3g2o17csparse_extension14cs_cholsolsymbEPK12cs_di_sparsePdPK14cs_di_symbolicS4_Pi
__ZN3g2o17csparse_extension14writeCs2OctaveEPKcPK12cs_di_sparseb
__ZN3g2o17csparse_extension17cs_chol_workspaceEPK12cs_di_sparsePK14cs_di_symbolicPiPd

However, if you demangle these symbols (using c++filt for example), you can see that the symbols are actually:

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*)

the differences being uses of cs_sparse and cs_symbolic vs cs_di_sparse and cs_di_symbolic.

Now, on my Mac, running the commands you'd provided worked fine (with the same caveats as @alex-reinking regarding <omp.h> and GCC), but the built library contained the symbols with cs_di_*, even though there is no mention of cs_di_* in the source proper.

Digging a bit deeper (with the use of -DCMAKE_EXPORT_COMPILE_COMMANDS=1 to get the actual build commands) I discovered that I had suite-sparse installed. This library provides a header at /usr/local/include/cs.h, which has both the definitions for:

typedef struct cs_di_sparse
{
...
} cs_di ;

typedef struct cs_di_symbolic
{
...
} cs_dis ;

and the defines:

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

which means the g2o project will build properly with that installed version of SuiteSparse if it's available. Otherwise, it will build with the included (older) EXTERNAL/csparse/cs.h header.

After uninstalling suite-sparse, running the commands you've provided still worked fine, so my takeaway is that:

  • some of your translation units are compiled looking at the included cs.h and
  • some looking at an installed version somewhere else on your system

To isolate this, see exactly which objects/libs have which version of these symbols using nm and work out the build commands for each.