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

问题描述 投票:1回答:1

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

在我的文件中,我从openssl进行了一些导入

#include <openssl/bio.h>
#include <openssl/bn.h>
#include <openssl/ecdsa.h>
#include <openssl/obj_mac.h>

并使用这些库中的符号。

关于为什么构建失败的任何想法?我正在遵循官方文档,并依赖2个构建参数

  1. cargo:rustc-link-search,以便货物也可以知道他也必须在此目录中进行查找。
  2. cargo:rustc-link-lib = dylib告诉要链接到的动态库。

我在这里想念的人们是什么?预先感谢。

编辑+更新:

我按照@Uli Schlachter的指示进行了操作,并且可以编译,但是出现运行时错误,指出未找到libf.so

ldd ./target/debug/test_f
    libf.so => not found.

有什么想法吗?

rust dynamic-linking rust-cargo
1个回答
0
投票

使用...编译共享对象

gcc -c -fPIC f.c -o f.o
gcc f.o -shared -o f.so -lssl
© www.soinside.com 2019 - 2024. All rights reserved.