1
votes

In a directory I have a C file and its header

/home/test/c_pro

  f.c
  f.h
  libf.so

I have compiled the f.c into a dll called libf.so using the following command

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

I want to use this in my Rust project.

So in Rust project I have a build.rs

println!("cargo:rustc-link-search=/home/test/c_pro");
println!("cargo:rustc-link-lib=dylib=f")

When I run a cargo build the build fails with the following errors

/home/test/c_pro/f.so: undefined reference to `EC_KEY_new_by_curve_name'
      collect2: error: ld returned 1 exit status

In my f.c I do some imports from openssl

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

and use symbols from these libraries.

Any ideas as to why the build fails? I am following the official doc and am relying on 2 build parameters

  1. cargo:rustc-link-search so that cargo can know that he has to do a look up in this directory as well.
  2. cargo:rustc-link-lib=dylib to tell what dynamic library to link to.

What am I missing here folks? Thanks in advance.

EDIT+UPDATE:

I did as pointed out by @Uli Schlachter and it compiles but I get a runtime error stating that libf.so is not found.

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

Any ideas?

1
"I have compiled the f.c into a dll called libf.so" - How did you do that? Does your shared object correctly report its dependency on OpenSSL? Did you build it with -lssl? - Uli Schlachter
Put differently: Can you try writing a short program in C that uses libf.so and run that? I bet that this question is not related to Rust, but rather about how to build a working shared object. - Uli Schlachter
@UliSchlachter, I have updated the comment. - amrx
Looks like you need to add -lssl? - Edd Barrett

1 Answers

1
votes

Compile your shared object with

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