Linking Rust to LLVM API in Cargo.toml - rust

I'm using a library that wraps the LLVM-C API (inkwell) so I need to link my Rust binary to the LLVM library. If I export the following rust flags with:
export RUSTFLAGS=-lLLVM-12 -lm -ldl -lc -lpthread -lutil -lgcc_s -C link-args=-L/usr/lib/llvm/12/lib64
Then compilation runs fine.
If I then insert these lines into my Cargo.toml file for the project however:
[build]
rustflags = ["-lLLVM-12", "-lm","-ldl","-lc","-lpthread","-lutil","-lgcc_s", "-C", "link-args=-L/usr/lib/llvm/12/lib64"]
Then I get linking errors against LLVM-C functions.
Why does this work with an environment variable but not in my cargo config file? Am I msiconfiguring cargo in some way?

According to Cargo documentation, rustflags property is documented via .cargo/config.toml, not Cargo.toml.

Related

Error while linking a custom dynamic library Rust

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
cargo:rustc-link-search so that cargo can know that he has to do a look up in this directory as well.
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?
Compile your shared object with
gcc -c -fPIC f.c -o f.o
gcc f.o -shared -o f.so -lssl

How do I change the default rustc / Cargo linker?

I would like to make rustc use lld as a linker instead of ld in a particular crate. So I create .cargo/config in my project directory with the following:
[target.x86_64-unknown-linux-gnu]
linker = "ld.lld"
Which leads to linker errors:
$ cargo build
...
= note: ld.lld: error: unable to find library -ldl
ld.lld: error: unable to find library -lrt
ld.lld: error: unable to find library -lpthread
ld.lld: error: unable to find library -lgcc_s
ld.lld: error: unable to find library -lc
ld.lld: error: unable to find library -lm
ld.lld: error: unable to find library -lrt
ld.lld: error: unable to find library -lpthread
ld.lld: error: unable to find library -lutil
ld.lld: error: unable to find library -lutil
Same thing with rust-lld. If I set linker = "ld" (which should be the default, right?), I just get
= note: ld: cannot find -lgcc_s
I tried to resolve all the missing libraries manually (with -C link-arg=--library-path=/usr/lib/x86_64-linux-gnu and the like), but it only lead to wrong linkage and a segfaulting binary.
Interestingly enough, if I replace /usr/bin/ld with a symlink to /usr/bin/ld.lld, it works great (no errors, and from the compiled binary I see that it was indeed linked with lld). However, I don't want to make lld my system-wide linker, I just want to use it in a particular Rust crate.
So what is the proper way to change the default rustc linker?
Thanks to #Jmb comment, I found a solution. Turns out that the default linker that rustc uses is actually cc (which makes sense - it supplies all the needed defaults to compile/link C code, which also work for Rust). We can pass an argument to cc to make it link with lld:
[target.x86_64-unknown-linux-gnu]
rustflags = [
"-C", "link-arg=-fuse-ld=lld",
]
Now cargo build links with lld.
Adding this to Cargo.toml or .cargo/config.toml (in the project) forces using lld
[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "link-arg=-fuse-ld=lld"]
[target.x86_64-pc-windows-gnu]
rustflags = ["-C", "link-arg=-fuse-ld=lld"]
[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "linker=clang", "-C", "link-arg=-fuse-ld=lld"]
For more information see this:
https://github.com/rust-lang/rust/issues/71515
You can remove "-C", "linker=clang" on Linux, if you have Gcc 9 or newer installed
This also works and is what I think #Jmb actually asked.
rustflags = [
"-C", "linker=clang-12", # change the version as needed
]

How can I specify linker flags/arguments in a build script?

I'm using Rust, bindgen, and a build script to work on some FFI bindings to a library.
This library is built using OpenMP, so when linking against it, I'd normally pass the -fopenmp flag to the compiler.
How can I get this flag to be set by build.rs when the library is built by Cargo?
Currently, building using Cargo fails, with the failing command being something like:
cc -Wl,--as-needed -Wl,-z,noexecstack -m64 -l gomp -l stdc++
...skipping dozens of paths/files...
-Wl,-Bdynamic -l dl -l rt -l pthread -l gcc_s -l c -l m -l rt -l pthread -l util
which fails with hundreds of undefined reference to 'GOMP_parallel_end' errors.
Rerunning the generated command above with the -fopenmp flag manually added succeeds.
I can specify the flag using RUSTFLAGS='-C link-args=-fopenmp' before compiling, but is there a way of specifying it from within build.rs?
This feature has been added to Cargo and was stabilized in Cargo 1.56. The accepted answer is now out-of-date.
Linker arguments can be specified in build.rs like so:
// Pass `-fopenmp` to the linker.
println!("cargo:rustc-link-arg=-fopenmp");
You cannot could not. See the sibling answer from ecstaticm0rse for an updated answer.
Before then, you can use a Cargo configuration file.
.cargo/config
[build]
rustflags = ["-C", "link-args=-fsome-artisanal-option"]
Execution
$ cargo build --verbose
Compiling example v0.1.0 (file:///private/tmp/example)
Running `rustc ...blah blah blah... -C link-args=-fsome-artisanal-option`
error: linking with `cc` failed: exit code: 1
|
= note: "cc" "-m64" ...blah blah blah... "-fsome-artisanal-option"
= note: clang: error: unknown argument: '-fsome-artisanal-option'
See also:
How to get the linker to produce a map file using Cargo
How can I globally configure a Cargo profile option?
Is it possible to specify `panic = "abort"` for a specific target?

Expose symbols to dynamic linker when linking with native library in Rust

I am building a binary executable in Rust and it needs to link to a native library, say foo.a. foo.a contains a symbol void bar(void), which I would like to expose to the dynamic linker as a callback function that can be called by functions in a dlopen-style dynamically loaded library.
This can be done in ld by supplying -rdynamic if we use C source.
gcc -rdynamic -o a_dynamic main.c foo.c
What is the proper way of doing this in Rust? I have tried using cargo:rustc-flags=-rdynamic in build.rs, as well as
#![feature(link_args)]
#[link_args = "-rdynamic"]
Neither seems to work.
As of today, Rust toolchain discourages passing arbitrary flags to the linker. The closest we can do as a proper way is using cargo rustc and manually add the linking arguments.
cargo rustc -- -C link-args='-rdynamic'

undefined reference when cross-compiling for ARM with static OpenCV libraries

I'm trying to compile a simple test program using static OpenCV libraries that have been compiled using an ARM compiler. But when I try to compile it with the command
$arm-linux-gnueabihf-g++ `pkg-config --static opencv` -I/usr/local/include -L<path to static libary> -lopencv_imgproc -lopencv_core ARMtest2.cpp -o ARMtest2
This gives
/tmp/ccxNeUbK.o: In function main':
ARMtest2.cpp:(.text+0x8a): undefined reference tocv::Mat::ones(int, int, int)'
/tmp/ccxNeUbK.o: In function cv::Mat::~Mat()':
ARMtest2.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x20): undefined reference tocv::fastFree(void*)'
/tmp/ccxNeUbK.o: In function cv::Mat::release()':
ARMtest2.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x30): undefined reference tocv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
The code itself is just some simple test code that prints a Mat type variable.
I compiled the static OpenCV library with cmake-gui. I selected UNIX Makefile and then selected 'specify options for cross-compiling' where I gave the path to the ARM (arm-linux-gnueabihf) gcc and g++ compiler. Then I unticked BUILD_SHARED_LIB so it compiled static libraries. It seemed to compile fine without errors. After that I did make & sudo make install.
I also tried it with shared libraries and that worked fine on the ARM board (once I copied the libraries to the board and exported the library path).
The static .a files landed nicely in the build folder. Apparently it can also find it when I -L link to it. I have tried reversing the order of the libraries, but to no avail.
So I'm a bit at a loss what is going wrong.
I solved it. Using the normal --static pkg-config command to compile with OpenCV libraries;
`pkg-config --libs --static opencv`
Of course I installed the static libraries also to the folder /usr/local/lib where libraries get installed first. But still i don't know what I missed in the command line I tried to use. I had a look in the config file /usr/local/lib/pkgconfig/opencv.pc
Here is whats in it:
# Package Information for pkg-config
prefix=/usr/local
exec_prefix=${prefix}
libdir=
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 2.4.9
Libs: ${exec_prefix}/lib/libopencv_calib3d.so ${exec_prefix}/lib/libopencv_contrib.so ${exec_prefix}/lib/libopencv_core.so ${exec_prefix}/lib/libopencv_features2d.so ${exec_prefix}/lib/libopencv_flann.so ${exec_prefix}/lib/libopencv_gpu.so ${exec_prefix}/lib/libopencv_highgui.so ${exec_prefix}/lib/libopencv_imgproc.so ${exec_prefix}/lib/libopencv_legacy.so ${exec_prefix}/lib/libopencv_ml.so ${exec_prefix}/lib/libopencv_nonfree.so ${exec_prefix}/lib/libopencv_objdetect.so ${exec_prefix}/lib/libopencv_ocl.so ${exec_prefix}/lib/libopencv_photo.so ${exec_prefix}/lib/libopencv_stitching.so ${exec_prefix}/lib/libopencv_superres.so ${exec_prefix}/lib/libopencv_ts.a ${exec_prefix}/lib/libopencv_video.so ${exec_prefix}/lib/libopencv_videostab.so -lrt -lpthread -lm -ldl
Cflags: -I${includedir_old} -I${includedir_new}
I believe this is what is being called with the pkg-config <--something_or_other> opencv line.
And saw some other things that probably get linked when compiling -lrt -lpthread -lm -ldl Not really sure though as I tried it ina normal command line and I apparently still missed somethings.
But it worked, so didn't really bother too much with it much further :)
In your command:
*$arm-linux-gnueabihf-g++ `pkg-config --static opencv` -I/usr/local/include -L<path to static libary> -lopencv_imgproc -lopencv_core ARMtest2.cpp -o ARMtest2*
The cflags was missing.
Try with:
$arm-linux-gnueabihf-g++ `pkg-config --cflags --static opencv` -I/usr/local/include -L<path to static cross-compiled libary> -lopencv_imgproc -lopencv_core ARMtest2.cpp -o ARMtest2
Assuming here that your cross-compiled headers are saved in /usr/local/include (as you specified).

Resources