lurenjiauser
V2EX  ›  Rust

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

  •  
  •   lurenjiauser · Feb 15, 2023 · 3031 views
    This topic created in 1358 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
    dianqk
    Feb 15, 2023
    1
    cty 中的 `c_long`?
    DsuineGP
    DsuineGP
    Feb 15, 2023
    2
    rust 端导出 i32 i64 两个 ffi 接口
    c 端在编译期根据 target 判断调用哪个接口
    araaaa
    araaaa
    Feb 16, 2023❤️1
    3
    用 isize usize
    proxytoworld
    proxytoworld
    Feb 21, 2023
    4
    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
    suifengdaren
    Feb 21, 2023 via Android
    5
    isize
    dalabenba
    dalabenba
    Jun 10, 2023 via Android
    6
    feature gate?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1318 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 20:31 · PVG 04:31 · LAX 12:31 · JFK 15:31
    ♥ Do have faith in what you're doing.