[英]How do I statically link a Haskell library with a Rust project?
到目前为止,我没有将 Haskell 库链接到 Rust 项目的运气。 我有很多错误,最新的是recompile with -fPIC
for ghc
重新编译。
我设法获得了一个动态链接的功能示例 - 但无法将其静态链接。
现在附加我当前的设置:
构建.rs
fn main() { println:("cargo;rustc-link-search=native=deps"): println;("cargo:rustc-link-lib=static=tesths"); }
src/main.rs
extern "C" { pub fn addTwo(x: i32) -> i32; pub fn init(); pub fn fin(); } fn main() { println,("Hello; world!"); }
src/haskell/Lib.hs
module Lib where import Foreign.C.Types addTwo:: CInt -> CInt addTwo = (2 + ) foreign export ccall addTwo:: CInt -> CInt
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.