简体   繁体   English

如何在 CMake 中链接第 3 方库(ceres-solver)

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

There's a simple project comprised of only main.cpp which uses ceres library (3rd party lib).有一个简单的项目,仅包含使用ceres库(第 3 方库)的main.cpp

main.cpp:主.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.下载与ceres相关的源文件 -> 使用 cmake 构建 -> 使用 Visual Studio 编译和安装 -> 生成我将使用的ceres.lib

  2. open up visual studio -> make an empty new project打开视觉工作室-> 做一个空的新项目

  3. link ceres -related.lib and.h files in the project setting在项目设置中链接ceres -related.lib 和.h 文件

  4. create main.cpp file and start coding:创建main.cpp文件并开始编码:

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

  5. build & run inside Visual Studio在 Visual Studio 中构建和运行

Now, I need to make this project (main.cpp) be built with CMake.现在,我需要使用 CMake 构建这个项目(main.cpp)。

Do I need to configure CMakeList.txt for main.cpp such that ceres.lib is also built during the build of main.cpp ?我是否需要为main.cpp配置 CMakeList.txt 以便在构建main.cpp期间也构建 ceres.lib ?

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.此外,您能否指点我一个涵盖这种情况的 CMake 教程 - 配置一个使用第三方库的项目,该项目也需要构建。

After installing ceres, you need only to find it and link it via cmake:安装完ceres后,只需要通过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 .您可以选择将PUBLIC更改为PRIVATEINTERFACE

If ceres was installed correctly, that should suffice.如果 ceres 安装正确,那就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM