CMake unable to find package G2O and include its headers

255 Views Asked by At

The title of post might need to be corrected. I have the following project structure.

scan_inconsistency
|-- CMakeLists.txt
|-- build
|-- cmake_modules
|-- libs
|   |-- CMakeLists.txt
|   |-- consist
|   `-- g2o
`-- src
    |-- CMakeLists.txt
    `-- main.cpp

The package consist is taken from this place (with slight modifications explained in the end). The package g2o is from the official g2o repository. The contents of cmake_modules is a direct copy-paste of the ones from consist/cmake (again, with slight modifications). The entire project is in my repository.

g2o is a dependency of consist. I want to use a class defined in a header file in consist: libs/consist/src/consist/visibility.h. To use the class I need to initialise variables of types defined in g2o which is inside g2o/g2o/types. In my scan_inconsistency/src/main.cpp I am only trying to include these files and initialise variables from classes declared in those files.

The problem(s): The compiler (or CMake?) does not find the header files in g2o.

  1. When only compiling consist: making a build folder inside consist, from inside consist/build running cmake .., and then running make from inside consist/build. Corresponding CMakeLists.txt file. Output of cmake ..
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for sgemm_
-- Looking for sgemm_ - not found
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Looking for sgemm_
-- Looking for sgemm_ - found
-- Found BLAS: /usr/lib/x86_64-linux-gnu/libblas.so  
-- Found Eigen3: /usr/include/eigen3 (Required is at least version "2.91.0") 
-- Found CHOLMOD: /usr/include/suitesparse  
-- Found QGLViewer: /usr/include/QGLViewer  
CMake Warning (dev) at /usr/share/cmake-3.16/Modules/FindOpenGL.cmake:275 (message):
  Policy CMP0072 is not set: FindOpenGL prefers GLVND by default when
  available.  Run "cmake --help-policy CMP0072" for policy details.  Use the
  cmake_policy command to set the policy and suppress this warning.

  FindOpenGL found both a legacy GL library:

    OPENGL_gl_LIBRARY: /usr/lib/x86_64-linux-gnu/libGL.so

  and GLVND libraries for OpenGL and GLX:

    OPENGL_opengl_LIBRARY: /usr/lib/x86_64-linux-gnu/libOpenGL.so
    OPENGL_glx_LIBRARY: /usr/lib/x86_64-linux-gnu/libGLX.so

  OpenGL_GL_PREFERENCE has not been set to "GLVND" or "LEGACY", so for
  compatibility with CMake 3.10 and below the legacy GL library will be used.
Call Stack (most recent call first):
  CMakeLists.txt:22 (FIND_PACKAGE)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Found OpenGL: /usr/lib/x86_64-linux-gnu/libOpenGL.so   
-- Found Doxygen: /usr/bin/doxygen (found version "1.8.17") found components: doxygen dot 
-- Disabling GUI support
-- Enabling documentation build
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ivengar/workspace/scan_inconsistency/libs/consist/build

Output of make:

Scanning dependencies of target consist
[  5%] Building CXX object CMakeFiles/consist.dir/src/consist/gridbisectionoptimizer.cpp.o
[ 11%] Building CXX object CMakeFiles/consist.dir/src/consist/hypothesistester.cpp.o
/home/ivengar/workspace/scan_inconsistency/libs/consist/src/consist/hypothesistester.cpp:23:10: fatal error: g2o/core/sparse_optimizer.h: No such file or directory
   23 | #include <g2o/core/sparse_optimizer.h>
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/consist.dir/build.make:76: CMakeFiles/consist.dir/src/consist/hypothesistester.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:135: CMakeFiles/consist.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
  1. When compiling the top level CMakeLists.txt: making build folder in the top-level, from inside scan_inconsistency/build running cmake .., and then running make from inside scan_inconsistency/build. Output of cmake ..
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for sgemm_
-- Looking for sgemm_ - not found
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Looking for sgemm_
-- Looking for sgemm_ - found
-- Found BLAS: /usr/lib/x86_64-linux-gnu/libblas.so  
-- Found Eigen3: /usr/include/eigen3 (Required is at least version "2.91.0") 
-- Found CHOLMOD: /usr/include/suitesparse  
-- Found QGLViewer: /usr/include/QGLViewer  
CMake Warning (dev) at /usr/share/cmake-3.16/Modules/FindOpenGL.cmake:275 (message):
  Policy CMP0072 is not set: FindOpenGL prefers GLVND by default when
  available.  Run "cmake --help-policy CMP0072" for policy details.  Use the
  cmake_policy command to set the policy and suppress this warning.

  FindOpenGL found both a legacy GL library:

    OPENGL_gl_LIBRARY: /usr/lib/x86_64-linux-gnu/libGL.so

  and GLVND libraries for OpenGL and GLX:

    OPENGL_opengl_LIBRARY: /usr/lib/x86_64-linux-gnu/libOpenGL.so
    OPENGL_glx_LIBRARY: /usr/lib/x86_64-linux-gnu/libGLX.so

  OpenGL_GL_PREFERENCE has not been set to "GLVND" or "LEGACY", so for
  compatibility with CMake 3.10 and below the legacy GL library will be used.
Call Stack (most recent call first):
  libs/consist/CMakeLists.txt:22 (FIND_PACKAGE)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Found OpenGL: /usr/lib/x86_64-linux-gnu/libOpenGL.so   
-- Found Doxygen: /usr/bin/doxygen (found version "1.8.17") found components: doxygen dot 
-- Disabling GUI support
-- Enabling documentation build
-- Compiling on Unix
-- Found AMD headers in: /usr/include/suitesparse
-- Found AMD library: /usr/lib/x86_64-linux-gnu/libamd.so
-- Found CAMD headers in: /usr/include/suitesparse
-- Found CAMD library: /usr/lib/x86_64-linux-gnu/libcamd.so
-- Found CCOLAMD headers in: /usr/include/suitesparse
-- Found CCOLAMD library: /usr/lib/x86_64-linux-gnu/libccolamd.so
-- Found CHOLMOD headers in: /usr/include/suitesparse
-- Found CHOLMOD library: /usr/lib/x86_64-linux-gnu/libcholmod.so
-- Found COLAMD headers in: /usr/include/suitesparse
-- Found COLAMD library: /usr/lib/x86_64-linux-gnu/libcolamd.so
-- Found SPQR headers in: /usr/include/suitesparse
-- Found SPQR library: /usr/lib/x86_64-linux-gnu/libspqr.so
-- Found Config headers in: /usr/include/suitesparse
-- Found Config library: /usr/lib/x86_64-linux-gnu/libsuitesparseconfig.so
-- Found Intel Thread Building Blocks (TBB) library (2020.1 / 11101) include location: . Assuming SuiteSparseQR was compiled with TBB.
-- Looking for shm_open in rt
-- Looking for shm_open in rt - found
-- Adding librt to SuiteSparse_config libraries (required on Linux & Unix [not OSX] if SuiteSparse is compiled with timing).
-- Could NOT find METIS (missing: METIS_INCLUDE_DIR METIS_LIBRARY) 
-- Found SuiteSparse: /usr/include/suitesparse (found version "5.7.1") found components: AMD CAMD CCOLAMD CHOLMOD COLAMD SPQR Config 
-- Enable support for Cholmod
-- Building LGPL code as a shared library
-- Found CSparse: /usr/include/suitesparse  
-- Enable support for CSparse
-- Compiling with OpenGL support
-- Compiling SLAM 3D types
-- Compiling built in types slam2d slam2d (addons) data sclam2d slam3d slam3d (addons) sba icp sim3
-- Compiling g2o apps
-- Compiling g2o examples
-- Performing Test SSE2_AVAILABLE
-- Performing Test SSE2_AVAILABLE - Success
-- Compiling with GCC
-- Generating position independent code for slam2d because Qt5 was built with -reduce-relocations
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ivengar/workspace/scan_inconsistency/build

Output of make:

In file included from /home/ivengar/workspace/scan_inconsistency/libs/consist/src/consist/visibility.h:25,
                 from /home/ivengar/workspace/scan_inconsistency/libs/consist/src/consist/visibility.cpp:22:
/home/ivengar/workspace/scan_inconsistency/libs/consist/src/consist/support.h:29:10: fatal error: g2o/core/optimizable_graph.h: No such file or directory
   29 | #include <g2o/core/optimizable_graph.h>
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [libs/CMakeFiles/visibility.dir/build.make:63: libs/CMakeFiles/visibility.dir/consist/src/consist/visibility.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:1380: libs/CMakeFiles/visibility.dir/all] Error 2
make: *** [Makefile:152: all] Error 2

It is my undersatnding that to use 3rd party packages like consist and g2o (which contain their own CMake files) I only need to place them in some sort of a library folder and point CMake to the subdirectories. Here are the contents of my respective CMakeLists files:

scan_inconsistency/CMakeLists.txt

project(scan_inconsistency)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_STANDARD 11)

# Add the necessary subdirectories
add_subdirectory(src)
add_subdirectory(libs)

include_directories(${PROJECT_SOURCE_DIR}/libs/consist/src ${PROJECT_SOURCE_DIR}/libs/g2o/g2o)

scan_inconsistency/libs/CMakeLists.txt

# Specify the libraries you want to include
add_subdirectory(consist)

add_library(visibility STATIC consist/src/consist/visibility.cpp)
target_include_directories(visibility PUBLIC consist/src/consist)

add_subdirectory(g2o)
add_library(raw_laser STATIC g2o/g2o/types/data/robot_laser.cpp)
# add_library(se2 STATIC g2o/g2o/types/slam2d/se2.cpp) # commented because se2 is a header-only library

scan_inconsistency/src/CMakeLists.txt

add_executable(scan_main main.cpp)


# does not work evem when this line in uncommented
# target_link_libraries(scan_main ${g2o_LIBRARIES} ${consist_LIBRARIES})
target_link_libraries(scan_main visibility raw_laser se2) # adding se2 here is useless, i am aware

These CMakeLists files are a weird amalgamation after reading many pages on "how to include third party libraries in CMake and C++" like this this and this, so I am sure there are probably some glaring errors. I am not that good at CMake yet.

The problem as I see it is probably simply in FindG2O.cmake but find_package did not complain so I am not sure. scan_inconsistencies/cmake_modules exists only so that consist can find g2o maybe because it cannot find packages that are on the same level or in parent directories (thus I tried adding the lines get_filename_component in libs/consist/CMakeLists.txt). I also tried to add the explicit path of where g2o is in the FindG2O.cmake file, but the same problem exists. I also tried to have the includes as #include<g2o/g2o/core/...> but no luck.

What should I be changing in any of my CMakeLists files to make this run?

Modifications: (tldr: the codebase of consist seems not updated since ca. 2014, so the changes are mostly to update simple things (i believe). the changed version is in my repository, so in theory it should not matter what the changes are)

  • The orginal source consist/cmake/FindQGLViewer.cmake from their website tries to find Qt4 instead of Qt5. The modified FindQGLViewer.cmake is a copy of the most recent g2o/cmake_modules/FindQGLViewer.cmake.
  • Changed references to #include <Eigen/...> to #include <eigen3/Eigen/...> in libs/consist/src/consist/support.h. This change should technically be in the entire repository, but for now to make this run, I only needed to change it in one place.
  • changed CMAKE_MODULE_PATH in libs/consist/CMakeLists.txt to point to top-level directory, so that it can find G2O which is on the same level as consist.
0

There are 0 best solutions below