WiSaGaN
WiSaGaN

Reputation: 48077

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.

Upvotes: 3

Views: 902

Answers (1)

WiSaGaN
WiSaGaN

Reputation: 48077

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'

Upvotes: 2

Related Questions