lurenjiauser
V2EX  ›  Rust

请教一些各位 ruster 是如何在源代码级别兼容 32/64 位的啊?

  •  
  •   lurenjiauser · Feb 15, 2023 · 2698 views
    This topic created in 1244 days ago, the information mentioned may be changed or developed.

    比如我提供一个.so 中的加法器:

    32 位系统中的 c 来调用:

    int adder(int a, int b);
    

    64 位系统中的 c 来调用

    int64 adder(int64 a, int64 b);
    

    这种有办法条件编译?

    6 replies    2023-06-10 21:08:02 +08:00
    dianqk
        1
    dianqk  
       Feb 15, 2023
    cty 中的 `c_long`?
    DsuineGP
        2
    DsuineGP  
       Feb 15, 2023
    rust 端导出 i32 i64 两个 ffi 接口
    c 端在编译期根据 target 判断调用哪个接口
    araaaa
        3
    araaaa  
       Feb 16, 2023   ❤️ 1
    用 isize usize
    proxytoworld
        4
    proxytoworld  
       Feb 21, 2023
    c 里面

    #ifdef x64_86
    int adder(int a, int b);
    #else
    int64 adder(int64 a, int64 b);

    rust 里面
    #[cfg(target_arch = "x86_64")]
    fn adder(a:i64,b:i64) -> i64
    #[cfg(target_arch = "x86")]
    fn adder(a:i32,b:i32) -> i32
    suifengdaren
        5
    suifengdaren  
       Feb 21, 2023 via Android
    isize
    dalabenba
        6
    dalabenba  
       Jun 10, 2023 via Android
    feature gate?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1113 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 17:53 · PVG 01:53 · LAX 10:53 · JFK 13:53
    ♥ Do have faith in what you're doing.