简体   繁体   English

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

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

I want to integrate CasADi into a CMake -based C++ codebase as an ExternalProject .我想将CasADi作为ExternalProject集成到基于CMake的 C++ 代码库中。 For this purpose, I would like to use pre-compiled libraries because building from source is not recommended .为此,我想使用预编译库,因为不推荐从源代码构建。 So far, I have only managed to write the following:到目前为止,我只写了以下内容:

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)

and I noticed that all the binaries are correctly downloaded in the specified folder.我注意到所有二进制文件都已正确下载到指定文件夹中。 However, I do not know how to link my targets to CasADi , nor how to find the package.但是,我不知道如何将我的目标链接到CasADi ,也不知道如何找到 package。

There is a natural problem with ExternalProject_Add: ExternalProject_Add 有一个自然的问题:

ExternalProject_Add executes commands only on build. ExternalProject_Add 仅在构建时执行命令。

Hence, download will not happen at the configure stage of your project which makes it difficult to use find_package , because the files cannot be found during your first configure run.因此,下载不会发生在项目的配置阶段,这使得使用find_package变得困难,因为在第一次配置运行期间找不到文件。

Take this CMakeLists.txt:以这个 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)

In order to use it you have to do the following:要使用它,您必须执行以下操作:

  1. Configure your project配置您的项目
mkdir build
cd build
cmake ..
  1. Build (download) casadi-3.5.5构建(下载)casadi-3.5.5
cmake --build . --target casadi-3.5.5
  1. Reconfigure your project, because now find_package will find the needed files重新配置你的项目,因为现在find_package会找到需要的文件
cmake ..
  1. Build your targets建立你的目标
cmake --build .

If you want a one step build, there are ways to get around this problem如果你想要一步构建,有办法解决这个问题


Here is an example for the second option, which might be better since FetchContent doesn't have the full functionality of ExternalProject.下面是第二个选项的示例,它可能更好,因为 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)

The point is to have another cmake project under external/CMakeLists.txt , which gets configured and build via execute_process calls from the main cmake project.重点是在external/CMakeLists.txt下有另一个 cmake 项目,它通过来自主 cmake 项目的execute_process调用进行配置和构建。 Do note, that you can now have find_package(casadi REQUIRED...) at configure stage, because the download will happen just before.请注意,您现在可以在配置阶段拥有find_package(casadi REQUIRED...) ,因为下载将在之前进行。

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

相关问题 使用带有cmake的预编译张量流 - use pre-compiled tensorflow with cmake 在 CMake 中使用预编译的头文件 - Using pre-compiled headers with CMake C ++预编译头和包含的文件组织 - C++ Pre-compiled header and included file organization 用于预编译的boost库(Ubuntu)的包 - Package for pre-compiled boost libraries (Ubuntu) Objective-C ++预编译头文件 - Objective-C++ pre-compiled headers 如何通过预处理器指令检查programmatic是否在Visual C ++中需要预编译头? - How to check programmatic by preprocessor directive whether a pre-compiled header is required in Visual C++? C++ 预编译头文件:对于同一个源文件,两个生成的 .gch 永远不会相同 - C++ Pre-compiled Headers: Two generated .gch are never identical for the same source file 如何在C ++中的预编译可执行文件中调用函数(通过命令调用)? - How to call a function in a pre-compiled executable file in C++ (calling by command)? 可以在预编译的C ++和Android的Java之间传递什么类型的数据? - What type of data can be passed between pre-compiled C++ and Java for Android? 为什么C ++ Builder无法创建预编译的头文件? - Why is C++Builder failing to create pre-compiled headers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM