• 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

    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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

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

    这个目录下的toml文件如下

    [package]
    name = "ctp_api"
    version = "0.1.0"
    authors = ["songroom"]
    edition = "2021"
    
    [dependencies]
    
    
    [build-dependencies]
    bindgen = "0.55.1"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    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文件

    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");
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

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

    [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"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

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

    warning: `ctp_strategy` (bin "ctp_strategy") generated 42 warnings
        Finished release [optimized] target(s) in 3.92s
    dbfund@iZgw041dtbsye3lhp8lz3eZ:~/rust_ctp/ctp/ctp_strategy$ 
    
    • 1
    • 2
    • 3

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

  • 相关阅读:
    配置Java环境变量不生效的解决办法
    VS Code C# 开发工具包正式发布
    双指针算法——移动零
    MySQL进阶04_索引_索引使用_索引设计原则
    【C++学习第八讲】简单变量(二)
    LeetCode 204. 计数质数
    QT-界面控件学习笔记
    夯实基础中篇-图解作用域链和闭包
    深圳国际物流详解:深圳国际物流公司有哪些
    Web应用程序漏洞-X-Forwarded-For注入
  • 原文地址:https://blog.csdn.net/wowotuo/article/details/133420744