Skip to content

Linker error when calling inline extern "C" function #61820

Closed
@thedataking

Description

@thedataking

Minimized example (problem discovered when translating bzip2 to Rust with C2Rust).

main.rs:

mod caller;
#[no_mangle]
#[inline]
pub unsafe extern "C" fn ext_fn() -> i32 { 0 }
pub fn main() { caller::call(); }

caller.rs

extern "C" { #[no_mangle] fn ext_fn() -> i32; }
pub fn call() { unsafe { ext_fn() }; }

NOTE: the problem has to do with the inline attribute; linking succeeds when removed.

$ rustc --version
rustc 1.35.0 (3c235d560 2019-05-20)
$ cargo build
undefined reference to `ext_fn'

whereas the following works (on stable and nightly):

$ RUSTFLAGS="-C link-dead-code" cargo build 

Maybe this is not expected to work since ext_fn isn't defined in a foreign library? On the other hand, it seems like a bug since using the inline attribute shouldn't prevent linking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-FFIArea: Foreign function interface (FFI)A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions