CTP:关于cc和bindgen库及rust工程组织

有三个工程目录,cpt-api, ctp-sdk,ctp-strategy

1、ctp-sdk:

主要的目的是基于bindgen库生成与cpp的.h文件相对应一个binding.rs文件,后面供策略使用。

在这个目录下,建一个build.rs,用bindgen库生成cpp.h的头文件相应的rust绑定文件,生成的文件放在ctp-api目录下

相应的buildr.s

bash 复制代码
use std::env;
use std::path::PathBuf;

fn main() {
    
    let bindings = bindgen::Builder::default()
        // The input header we would like to generate
        // bindings for.
        .header("src/wrapper.hpp")
        /* // Tell cargo to invalidate the built crate whenever any of the
        // included header files changed.
        .parse_callbacks(Box::new(bindgen::CargoCallbacks)) */
        .ignore_methods()
        .rustified_enum(".*")
        .blacklist_item("CTP_SIDE_TYPE")
        .blacklist_function("TraderSpiStub_Rust.*")
        .blacklist_function("QuoteSpiStub_Rust.*")
        .generate_comments(false)// 不需形成doc ,默认true
        .layout_tests(false) //不需要test,默认true
        //.derive_copy(false) //实现copy的较少,去掉
        .derive_debug(true) //debug还是要的
        // .derive_hash(false) //不要实现hash
        /* .default_enum_style(bindgen::EnumVariation::Rust {
            non_exhaustive: true,
        }) */
        // Finish the builder and generate the bindings.
        .generate()
        // Unwrap the Result and panic on failure.
        .expect("Unable to generate bindings");

    let out_path = PathBuf::from("../ctp_api/src"); //-------------------需要更新,根据具体的目录 
    //songroom/ctp/ctp_api/src
    bindings
        .write_to_file(out_path.join("ctp_type.rs"))
        .expect("Couldn't write bindings!");
    println!("bindings => path: {:?}",out_path);
}

这里去除了test,这里这样设置就可以了 .layout_tests(false),否则文件太长了,6-7万行。

这个目录下的toml文件如下

bash 复制代码
[package]
name = "ctp_api"
version = "0.1.0"
authors = ["songroom"]
edition = "2021"

[dependencies]


[build-dependencies]
bindgen = "0.55.1"

2、ctp-api(是一个lib工程)

主要是对ctp进行相关的组织。

ctp-types.rs文件:
由ctp-sdk下的build.rs生成(在ctp-sdk下运行cargo build --release).。

在这个目录下,可以组织一些ctp相应的接口相关的文件。这里不展开。

这里的toml文件并无特别。

3、ctp-strategy

在这里主要是进行策略组织,在这里build.rs中,要用到cc库。

build.rs文件

bash 复制代码
use std::env;
use std::path::PathBuf;

fn main() {
    cc::Build::new()
        .file("../ctp_sdk/src/bridge/bridge.cpp")
        .cpp(true)
        .warnings(false)
        .flag("-std=c++11")
        .compile("bridge");
    println!("cargo:rustc-link-lib=thostmduserapi_se");// "="后面不能有空格 //=后加了lib,2022/2/4
    println!("cargo:rustc-link-lib=thosttraderapi_se");// "="后面不能有空格

    println!("cargo:rustc-link-search=native=../ctp_sdk/sdk");
    // Tell cargo to invalidate the built crate whenever the wrapper changes
    println!("cargo:rerun-if-changed=../ctp_sdk/src/wrapper.hpp");
    println!("cargo:rerun-if-changed=../ctp_sdk/src/bridge/bridge.hpp");
    println!("cargo:rerun-if-changed=../ctp_sdk/src/bridge/bridge.cpp");
}

其中,build.rs中cc部分内容;println!内容不能省。

bash 复制代码
[package]
name = "ctp_strategy"
version = "0.1.0"
authors = ["songroom"]
edition = "2021"
build   = "build.rs" # 事前运行

[dependencies]
lazy_static ="1.4"
libc = "0.2"
encoding = "0.2.32"
memchr = { version = "2", default-features = false }
simple-error = "0.2.1"
time = "0.1.43"
ctp_api = {path = "../../ctp/ctp_api"}
crossbeam = "0.7.3"
crossbeam-utils="0.7.0"
log = "0.4"
chrono = "0.4.11"
failure = "0.1"
toml = "0.5"
serde = { version = "1.0", features = ["derive"] }
serde_derive ="1.0.123"


[build-dependencies]
cc = "1.0"

在ctp-strategy目录下运行:cargo build --release;结果正常:

bash 复制代码
warning: `ctp_strategy` (bin "ctp_strategy") generated 42 warnings
    Finished release [optimized] target(s) in 3.92s
dbfund@iZgw041dtbsye3lhp8lz3eZ:~/rust_ctp/ctp/ctp_strategy$ 

在运行上,先在ctp-sdk目录下,cargo build 生成后续项目依赖的类bingings.rs文件;此后在ctp-strategy上运行cargo run --release即可。

相关推荐
JUNAI_Strive_ving8 分钟前
番茄小说逆向爬取
javascript·python
落落落sss12 分钟前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis
看到请催我学习17 分钟前
如何实现两个标签页之间的通信
javascript·css·typescript·node.js·html5
简单.is.good29 分钟前
【测试】接口测试与接口自动化
开发语言·python
twins352036 分钟前
解决Vue应用中遇到路由刷新后出现 404 错误
前端·javascript·vue.js
Yvemil71 小时前
MQ 架构设计原理与消息中间件详解(二)
开发语言·后端·ruby
程序员是干活的1 小时前
私家车开车回家过节会发生什么事情
java·开发语言·软件构建·1024程序员节
qiyi.sky1 小时前
JavaWeb——Vue组件库Element(3/6):常见组件:Dialog对话框、Form表单(介绍、使用、实际效果)
前端·javascript·vue.js
煸橙干儿~~1 小时前
分析JS Crash(进程崩溃)
java·前端·javascript
哪 吒1 小时前
华为OD机试 - 几何平均值最大子数(Python/JS/C/C++ 2024 E卷 200分)
javascript·python·华为od