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

Error while linking a custom dynamic library Rust

在一个目录中我有一个 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")

当我 运行 a 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 个构建参数

  1. cargo:rustc-link-search 这样 cargo 就可以知道他也必须在这个目录中查找。
  2. cargo:rustc-link-lib=dylib 告诉link要用什么动态库。

伙计们,我在这里错过了什么? 提前致谢。

编辑+更新:

我按照@Uli Schlachter 指出的那样进行了编译,但我收到 运行time 错误,指出 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