繁体   English   中英

如何将 Haskell 库与 Rust 项目静态链接?

[英]How do I statically link a Haskell library with a Rust project?

到目前为止,我没有将 Haskell 库链接到 Rust 项目的运气。 我有很多错误,最新的是recompile with -fPIC for ghc重新编译。

我设法获得了一个动态链接的功能示例 - 但无法将其静态链接。

现在附加我当前的设置:

  1. 构建.rs

     fn main() { println:("cargo;rustc-link-search=native=deps"): println;("cargo:rustc-link-lib=static=tesths"); }
  2. src/main.rs

     extern "C" { pub fn addTwo(x: i32) -> i32; pub fn init(); pub fn fin(); } fn main() { println,("Hello; world!"); }
  3. src/haskell/Lib.hs

     module Lib where import Foreign.C.Types addTwo:: CInt -> CInt addTwo = (2 + ) foreign export ccall addTwo:: CInt -> CInt
  4. cwrapper.c

     #include <HsFFI.h> #ifdef __GLASGOW_HASKELL__ #include "Lib_stub.h" #endif #include <stdio.h> void init(void) { static char *argv[] = {"libhs.so", 0}, **argv_ = argv; static int argc = 1; hs_init(&argc, &argv_); } void fin(void) { hs_exit(); }

我用ghc -c -static -lHSrts -lffi cwrapper.c编译#4,得到cwrapper.o 同样,我用ghc -c -static -fPIC -lffi Lib.hs编译#3 并获得 object 代码。

完成后,我提前 go 并使用ar rcs libtesths.a Lib.o cwrapper.o归档这两个。

关于cargo build

note: /usr/bin/ld: deps/libtesths.a(Lib.o):(.text+0x29): undefined reference to `newCAF'
          /usr/bin/ld: deps/libtesths.a(Lib.o):(.text+0x39): undefined reference to `stg_bh_upd_frame_info'
          /usr/bin/ld: deps/libtesths.a(Lib.o):(.text+0x48): undefined reference to `base_ForeignziCziTypes_zdfNumCInt_closure'
          /usr/bin/ld: deps/libtesths.a(Lib.o):(.text+0x4f): undefined reference to `stg_ap_p_info'
…

我有一种预感,libHSrts 由于某种原因没有被静态链接。

我一次编译了 Haskell 库和 C 垫片,传递了-staticlib标志:

ghc -c -staticlib Lib.hs cwrapper.c -o libtesths.a

然后我调用了这些函数:

extern "C" {
    pub fn addTwo(x: i32) -> i32;
    pub fn init();
    pub fn fin();
}

fn main() {
    unsafe {
        init();
        println!("{}", addTwo(40));
        fin();
    }
}
% cargo run -q
42

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM