gpt4 book ai didi

rust - 链接自定义动态库Rust时出错

转载 作者:行者123 更新时间:2023-12-03 11:37:23 26 4
gpt4 key购买 nike

在目录中,我有一个C文件及其标题

/home/test/c_pro

  f.c
f.h
libf.so

我已使用以下命令将f.c编译为一个名为libf.so的dll。
gcc -c -fPIC f.c -o f.o
gcc f.o -shared -o f.so

我想在我的Rust项目中使用它。

所以在Rust项目中我有一个build.rs
println!("cargo:rustc-link-search=/home/test/c_pro");
println!("cargo:rustc-link-lib=dylib=f")

当我运行 cargo build时,构建失败并出现以下错误
/home/test/c_pro/f.so: undefined reference to `EC_KEY_new_by_curve_name'
collect2: error: ld returned 1 exit status

在我的f.c文件中,我做了一些从openssl导入的操作
#include <openssl/bio.h>
#include <openssl/bn.h>
#include <openssl/ecdsa.h>
#include <openssl/obj_mac.h>

并使用这些库中的符号。

关于为什么构建失败的任何想法?我正在关注官方文档,并依赖于2个构建参数
  • cargo:rustc-link-search,以便cargo可以知道他也必须在此目录中进行查找。
  • cargo:rustc-link-lib = dylib告诉要链接到的动态库。

  • 我在这里想念什么?
    提前致谢。

    编辑+更新:

    我按照@Uli Schlachter的指示进行了操作,并且可以编译,但是出现运行时错误,指出未找到 libf.so
    ldd ./target/debug/test_f
    libf.so => not found.

    有任何想法吗?

    最佳答案

    使用以下命令编译共享对象

    gcc -c -fPIC f.c -o f.o
    gcc f.o -shared -o f.so -lssl

    关于rust - 链接自定义动态库Rust时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62127345/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com