Skip to content

Undefined reference to __rust_dealloc/ __rust_alloc when crate-type is ["staticlib", "dylib"] #56784

Closed
@mosyp

Description

@mosyp

Problem
A part of Cargo.toml:

[lib]
name = "my_lib"
crate-type = ["rlib", "staticlib", "dylib"]

When I build these rust artifact and try to link to it within c program I get a lot of undefined reference to '__rust_dealloc' / '__rust_alloc'. For example, the output of :

/usr/bin/ld: libmy_lib.a(std-52f862a21e09568c.std.6as1l5rw-cgu.0.rcgu.o): in function `alloc::alloc::alloc':
/rustc/abe02cefd6cd1916df62ad7dc80161bea50b72e8/src/liballoc/alloc.rs:78: undefined reference to `__rust_alloc'
/usr/bin/ld: libmy_lib.a(std-52f862a21e09568c.std.6as1l5rw-cgu.0.rcgu.o): in function `alloc::alloc::dealloc':
/rustc/abe02cefd6cd1916df62ad7dc80161bea50b72e8/src/liballoc/alloc.rs:96: undefined reference to `__rust_dealloc'
/usr/bin/ld: /rustc/abe02cefd6cd1916df62ad7dc80161bea50b72e8/src/liballoc/alloc.rs:96: undefined reference to `__rust_dealloc'

Compiling c code as gcc main.c my_lib.a -lm -ldl ....

But once I remove a dylib from Cargo.toml, it complies correctly with a success

Notes

Actually I don't need "dylib" for a linux development, but do need for windows. However I couldn't do something like [target.'cfg(windows)'.lib] (as for dependencies)

Output of cargo version:

cargo 1.31.0 (339d9f9c8 2018-11-16)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesE-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions