使用 CMake ExternalProject 将预编译库集成到 C++ 代码库中

Integrate pre-compiled libraries into C++ codebase with CMake ExternalProject

我想整合CasADi into a CMake-based C++ codebase as an ExternalProject. For this purpose, I would like to use pre-compiled libraries because building from source is not recommended。到目前为止,我只写了以下内容:

ExternalProject_Add(
  casadi-3.5.5
  URL https://github.com/casadi/casadi/releases/download/3.5.5/casadi-linux-py39-v3.5.5-64bit.tar.gz
  CONFIGURE_COMMAND ""
  BUILD_COMMAND ""
  INSTALL_COMMAND ""
  PREFIX ${CMAKE_BINARY_DIR}/external/casadi)

我注意到所有二进制文件都已正确下载到指定文件夹中。但是,我不知道如何 link 我的目标到 CasADi,也不知道如何找到包。

ExternalProject_Add自然有问题:

ExternalProject_Add executes commands only on build.

因此,在项目的配置阶段不会进行下载,这使得使用起来很困难find_package,因为在第一次配置时找不到文件运行。

拿这个CMakeLists.txt:

cmake_minimum_required(VERSION 3.21)
project(untitled)

set(CMAKE_CXX_STANDARD 17)

add_executable(untitled main.cpp)

include(ExternalProject)
ExternalProject_Add(
        casadi-3.5.5
        URL https://github.com/casadi/casadi/releases/download/3.5.5/casadi-linux-py39-v3.5.5-64bit.tar.gz
        CONFIGURE_COMMAND ""
        BUILD_COMMAND ""
        INSTALL_COMMAND ""
        PREFIX ${CMAKE_BINARY_DIR}/external/casadi)

find_package(casadi HINTS ${CMAKE_BINARY_DIR}/external/casadi/src/casadi-3.5.5/casadi)

target_link_libraries(untitled casadi)

要使用它,您必须执行以下操作:

  1. 配置您的项目
mkdir build
cd build
cmake ..
  1. 构建(下载)casadi-3.5.5
cmake --build . --target casadi-3.5.5
  1. 重新配置您的项目,因为现在 find_package 会找到需要的文件
cmake ..
  1. 建立你的目标
cmake --build .

如果您想要一步构建,有多种方法可以解决此问题

  • 使用FetchContent
  • 在包含所有 ExternalProject_Add 命令的子文件夹中创建一个 sub-cmake-project,并通过 execute_process 调用在您自己的 CMakeLists.txt 中手动执行适当的构建(下载)步骤:

这是第二个选项的示例,由于 FetchContent 不具备 ExternalProject 的全部功能,因此该选项可能更好。

  • main.cpp
#include <casadi/casadi.hpp>

int main()
{
    casadi_printf("This works!");
    return 0;
}
  • CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(untitled)

set(CMAKE_CXX_STANDARD 17)

# some default target
add_executable(untitled main.cpp)

# Configure and build external project
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/external)
execute_process(
        COMMAND ${CMAKE_COMMAND} ${CMAKE_SOURCE_DIR}/external
        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/external
)
execute_process(
        COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/external
)

# find and link externals
find_package(casadi REQUIRED HINTS ${CMAKE_BINARY_DIR}/external/external/casadi/src/casadi-3.5.5/casadi)
target_link_libraries(untitled casadi)
  • external/CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(external)

include(ExternalProject)
ExternalProject_Add(
        casadi-3.5.5
        URL https://github.com/casadi/casadi/releases/download/3.5.5/casadi-linux-py39-v3.5.5-64bit.tar.gz
        CONFIGURE_COMMAND ""
        BUILD_COMMAND ""
        INSTALL_COMMAND ""
        PREFIX ${CMAKE_BINARY_DIR}/external/casadi)

重点是在 external/CMakeLists.txt 下有另一个 cmake 项目,它通过来自主 cmake 项目的 execute_process 调用进行配置和构建。请注意,您现在可以在配置阶段拥有 find_package(casadi REQUIRED ...),因为下载将在之前进行。