How to link 3rd party library (ceres-solver) in CMake

2.2k Views Asked by At

There's a simple project comprised of only main.cpp which uses ceres library (3rd party lib).

main.cpp:

#include <ceres/ceres.h>
int main() {
    ceres::some_function();
    return 0;
}

Previously, this is how I used to code as a very beginner:

  1. download ceres-related source files -> build with cmake -> compile & install using Visual Studio -> generates ceres.lib which I will be using.

  2. open up visual studio -> make an empty new project

  3. link ceres-related .lib and .h files in the project setting

  4. create main.cpp file and start coding:

    #include <ceres/ceres.h> ...

  5. build & run inside Visual Studio

Now, I need to make this project (main.cpp) be built with CMake.

Do I need to configure CMakeList.txt for main.cpp such that ceres.lib is also built during the build of main.cpp?

Additionally, can you point me to a CMake tutorial that covers this kind of situation - configuring a project that uses a third-party library which needs to be built as well.

2

There are 2 best solutions below

0
On

After installing ceres, you need only to find it and link it via cmake:

find_package(Ceres REQUIRED)

target_link_libraries(something PUBLIC
    ${CERES_LIBRARIES}
    )

target_include_directories(something PUBLIC
    ${CERES_INCLUDE_DIRS}
    )

You can choose to change PUBLIC to PRIVATE or INTERFACE.

If ceres was installed correctly, that should suffice.

0
On
find_package(Ceres REQUIRED)

target_link_libraries(something PUBLIC
    Ceres::ceres
    )

Reference: https://github.com/ceres-solver/ceres-solver/blob/fd6197ce0ef5794bba455fe6f907dcdabcf624eb/docs/source/installation.rst#using-ceres-with-cmake