How to export a character from a Rust executable?

I am trying to export a character from the Rust executable:

#[allow(non_upper_case_globals)]
#[no_mangle]
pub static exported_symbol: [u8; 1] = *b"\0";

fn main() {
    println!("Hello, world!");
}

exported_symbol not output as a result of a binary file:

$ cargo build
$ nm ./target/debug/test_export| grep exported_symbol

On the other hand, if I create a library with the same source, the symbol is exported:

$ rustc --crate-type cdylib src/main.rs
$ nm libmain.so| grep exported_symbol
0000000000016c10 R exported_symbol

I am using Rust 1.18.0 for Linux x86-64.

+6
source share
2 answers

You can pass linker options to rustc, for example:

$ rustc src/main.rs --crate-type bin -C link-args=-Wl,-export-dynamic
$ nm main|grep export
00000000000d79c4 R exported_symbol

You probably want to put this in your rusty flags in . cargo / config something like:

[target.x86_64-unknown-linux-gnu]
rustflags = [ "-C", "link-args=-Wl,-export-dynamic" ]
+3
source

I would recommend this in the file .cargo/configinstead of the one above:

[build]
rustflags = ["-C", "link-args=-rdynamic"]

-rdynamicmore portable. In particular, it works on both Linux and MacOS.

, Rust/LLVM, , , , .

, , ( , , main).

:

pub fn init() {
    let funcs: &[*const extern "C" fn()] = &[
        exported_function_1 as _,
        exported_function_2 as _,
        exported_function_3 as _      
    ];
    std::mem::forget(funcs);
}

, , #[no_mangle]:

#[no_mangle]
pub extern "C" fn exported_function_1() {
  // ...
}
+1

Source: https://habr.com/ru/post/1676041/


All Articles