0

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.

Joon. P
  • 2,238
  • 7
  • 26
  • 53
  • Have you performed any **research** of your problem? For build 3d-party project during building your own could be performed with `ExternalProject_Add` command. You could find some examples of its usage in that question and its answers: https://stackoverflow.com/questions/15175318/cmake-how-to-build-external-projects-and-include-their-targets – Tsyvarev May 16 '21 at 16:45
  • @Tsyvarev Thanks for the link! I think this was what I was looking for – Joon. P May 20 '21 at 05:49

2 Answers2

2

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

Ryan Friedman
  • 115
  • 1
  • 9