Rust/C/C++ 混合构建 - Cmake集成Cargo编译动态库

Cmake是一个强大的多语言编译工具,其内置模块提供了强大的扩展能力。

Rust官方的构建工具是Cargo,其提供了依赖下载,上传和编译等多项功能,极大的便利了Rust项目的开发。

目前C++多数用Cmake构建,如果是Rust集成cmake构建c++动态库,可以用Cargo构建脚本集成cmake-rs构建。

如果要调用Cargo构建Rust编译的动态库,并且通过cmake作为构建工具统一调度,目前主流方案是使用Cmake内置的add_custom_target模块调用Cargo构建。

Playground

1、cargo new rust-lib ---lib 创建rust 项目,并在Cargo.toml中指定类型为cdylib

toml 复制代码
[package]
name = "rust_lib"
version = "0.1.0"
edition = "2021"

[dependencies]

[lib]
crate-type = ["cdylib"]

2、在 src/lib.rs 创建greet_from_rust动态库函数

rust 复制代码
#[no_mangle]
pub extern "C" fn greet_from_rust() {
    println!("Hello, greeting!")
}

3、创建call_rust.c,内容如下

c 复制代码
extern void greet_from_rust();

int main(void) {
    greet_from_rust();
    return 0;
}

4、编写Cmake构建规则,创建CMakeLists.txt, 其内容如下

复制代码
cmake_minimum_required(VERSION 3.22)
project(call_rust)

set(ARGS --target-dir ${CMAKE_BINARY_DIR}/target)

if (CMAKE_BUILD_TYPE STREQUAL "Release")
    message(STATUS "[RUST] Detected release build")
    set(build_profile "release")
    set(ARGS ${ARGS} --release)
else()
    message(STATUS "[RUST] Using default debug build")
    set(build_profile "debug")
endif()

add_custom_target(
    cargo_build ALL
    COMMAND 
        ${CMAKE_COMMAND} -E env 
        cargo build ${ARGS}
    DEPENDS src/lib.rs
)

add_executable(call_rust call_rust.c)
add_library(rust_lib SHARED IMPORTED)
set_property(TARGET rust_lib PROPERTY IMPORTED_LOCATION "${CMAKE_BINARY_DIR}/target/${build_profile}/librust_lib.so")
target_link_libraries(call_rust PUBLIC rust_lib)总结

add_custom_target调用Cargo将src/lib.rs 编译成动态库,为了方便将Cargo的构建目录指定在当前项目下。

add_libraryset_property 将生成的动态库绑定到rust_lib 这个target上,最后将动态库rust_lib通过

target_link_libraries 链接到call_rust二进制target上

Cmake构建配置完成,我们可以通过

cmake -DCMAKE_BUILD_TYPE:STRING=Release . cmake -DCMAKE_BUILD_TYPE:STRING=Debug .

这两个命令切换构建选项,这也对应了Cargo构建的---release选项,然后通过

cmake ---build . 构建当前项目, 输出

shell 复制代码
$ cmake  -DCMAKE_BUILD_TYPE:STRING=Release . && cmake --build . && ./call_rust
-- [RUST] Detected release build
-- Configuring done
-- Generating done
-- Build files have been written to: /root/workspace/build-tool-examples/rust_lib
Compiling rust_lib v0.1.0 (/root/workspace/build-tool-examples/rust_lib)
Finished release profile [optimized] target(s) in 0.44s
[  0%] Built target cargo_build
Consolidate compiler generated dependencies of target call_rust
[ 50%] Linking C executable call_rust
[100%] Built target call_rust
Hello, greeting!
相关推荐
特立独行的猫a2 小时前
Tauri 应用移植到 OpenHarmony/鸿蒙PC完整指南
华为·rust·harmonyos·tauri·移植·鸿蒙pc
搬砖魁首6 小时前
基础能力系列 - 多线程2 - 条件变量
c++·rust·条件变量·原子类型·线程同步互斥
Yuyubow10 小时前
gpui step by step 5. FocusHandle 焦点处理与键盘点击事件
rust
techdashen11 小时前
在 Fly.io 上使用 Rust 构建远程开发环境:从 Tokio 到 eBPF
开发语言·后端·rust
星栈14 小时前
用 Rust + Makepad 做一个 JSON 查看器:从零到能用的全过程
前端·rust
日取其半万世不竭15 小时前
Rust《腐蚀》 服务器低成本怎么开?配置、端口和存档避坑
服务器·开发语言·rust
techdashen15 小时前
Cargo 1.93 开发周期动态全解析
rust
Vallelonga16 小时前
Rust 中的枚举
开发语言·rust
薛定谔的猫-菜鸟程序员16 小时前
从Electron到Tauri,Rust+Vue(Tauri) 实现超高性能桌面日志应用开发,以及开发避坑指南
vue.js·rust·electron