Linking more than one `rustc` compiled static libraries in a C project

What I'm trying to do

I am trying to link two rust projects, compiled as static libraries, in my C project. I use CMake for builds in my C project. The first library is added in the below manner.

add_library(library1 STATIC IMPORTED GLOBAL)
set_property(TARGET library1 PROPERTY IMPORTED_LOCATION ${DOWNLOAD_PATH})

and the second library is added in the below manner

add_custom_target(zk_crypto_target DEPENDS ${OUTPUT_PATH} )
add_library(zk_crypto_rs STATIC IMPORTED GLOBAL)
add_dependencies(zk_crypto_rs zk_crypto_target)
set_property(TARGET zk_crypto_rs PROPERTY IMPORTED_LOCATION ${OUTPUT_PATH} ) 

When the build script tries to link the two libraries I get "multiple definition" errors for functions belonging to the alloc.rs, panicking.rs and other files.

Error Log

../ssi/vade-evan/libvade_evan.a(std-3dd5f78c3b43746e.std.b5670ad2-cgu.0.rcgu.o): In function `rust_oom':
/rustc/e7119a0300b87a3d670408ee8e847c6821b3ae80/library/std/src/alloc.rs:351: multiple definition of `rust_oom'
../rust/zkcrypto/release/libzk_crypto.a(std-e12de7683a34c500.std.77oi1yt8-cgu.0.rcgu.o):/rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca//library/std/src/alloc.rs:326: first defined here
../ssi/vade-evan/libvade_evan.a(std-3dd5f78c3b43746e.std.b5670ad2-cgu.0.rcgu.o): In function `__rdl_alloc':
/rustc/e7119a0300b87a3d670408ee8e847c6821b3ae80/library/std/src/alloc.rs:376: multiple definition of `__rdl_alloc'
../rust/zkcrypto/release/libzk_crypto.a(std-e12de7683a34c500.std.77oi1yt8-cgu.0.rcgu.o):/rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca//library/std/src/alloc.rs:351: first defined here
../ssi/vade-evan/libvade_evan.a(std-3dd5f78c3b43746e.std.b5670ad2-cgu.0.rcgu.o): In function `std::sys::unix::alloc::<impl core::alloc::global::GlobalAlloc for std::alloc::System>::dealloc':
/rustc/e7119a0300b87a3d670408ee8e847c6821b3ae80/library/std/src/alloc.rs:389: multiple definition of `__rdl_dealloc'
../rust/zkcrypto/release/libzk_crypto.a(std-e12de7683a34c500.std.77oi1yt8-cgu.0.rcgu.o):/rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca//library/std/src/sys/unix/alloc.rs:42: first defined here
../ssi/vade-evan/libvade_evan.a(std-3dd5f78c3b43746e.std.b5670ad2-cgu.0.rcgu.o): In function `__rdl_realloc':
/rustc/e7119a0300b87a3d670408ee8e847c6821b3ae80/library/std/src/alloc.rs:393: multiple definition of `__rdl_realloc'
../rust/zkcrypto/release/libzk_crypto.a(std-e12de7683a34c500.std.77oi1yt8-cgu.0.rcgu.o):/rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca//library/std/src/alloc.rs:368: first defined here
../ssi/vade-evan/libvade_evan.a(std-3dd5f78c3b43746e.std.b5670ad2-cgu.0.rcgu.o): In function `__rdl_alloc_zeroed':
/rustc/e7119a0300b87a3d670408ee8e847c6821b3ae80/library/std/src/alloc.rs:408: multiple definition of `__rdl_alloc_zeroed'
../rust/zkcrypto/release/libzk_crypto.a(std-e12de7683a34c500.std.77oi1yt8-cgu.0.rcgu.o):/rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca//library/std/src/alloc.rs:383: first defined here
../ssi/vade-evan/libvade_evan.a(std-3dd5f78c3b43746e.std.b5670ad2-cgu.0.rcgu.o): In function `core::fmt::Arguments::new_v1':
/rustc/e7119a0300b87a3d670408ee8e847c6821b3ae80/library/std/src/panicking.rs:62: multiple definition of `__rust_drop_panic'
../rust/zkcrypto/release/libzk_crypto.a(std-e12de7683a34c500.std.77oi1yt8-cgu.0.rcgu.o):/rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca//library/std/src/panicking.rs:59: first defined here
../ssi/vade-evan/libvade_evan.a(std-3dd5f78c3b43746e.std.b5670ad2-cgu.0.rcgu.o): In function `core::fmt::Arguments::new_v1':
/rustc/e7119a0300b87a3d670408ee8e847c6821b3ae80/library/std/src/panicking.rs:70: multiple definition of `__rust_foreign_exception'
../rust/zkcrypto/release/libzk_crypto.a(std-e12de7683a34c500.std.77oi1yt8-cgu.0.rcgu.o):/rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca//library/std/src/panicking.rs:67: first defined here
../ssi/vade-evan/libvade_evan.a(std-3dd5f78c3b43746e.std.b5670ad2-cgu.0.rcgu.o): In function `rust_begin_unwind':
/rustc/e7119a0300b87a3d670408ee8e847c6821b3ae80/library/std/src/panicking.rs:504: multiple definition of `rust_begin_unwind'
../rust/zkcrypto/release/libzk_crypto.a(std-e12de7683a34c500.std.77oi1yt8-cgu.0.rcgu.o):/rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca//library/std/src/panicking.rs:443: first defined here
../ssi/vade-evan/libvade_evan.a(std-3dd5f78c3b43746e.std.b5670ad2-cgu.0.rcgu.o): In function `rust_panic':
/rustc/e7119a0300b87a3d670408ee8e847c6821b3ae80/library/std/src/panicking.rs:714: multiple definition of `rust_panic'
../rust/zkcrypto/release/libzk_crypto.a(std-e12de7683a34c500.std.77oi1yt8-cgu.0.rcgu.o):/rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca//library/std/src/panicking.rs:639: first defined here
../ssi/vade-evan/libvade_evan.a(std-3dd5f78c3b43746e.std.b5670ad2-cgu.0.rcgu.o): In function `rust_eh_personality':
/rustc/e7119a0300b87a3d670408ee8e847c6821b3ae80/library/std/src/personality/gcc.rs:244: multiple definition of `rust_eh_personality'
../rust/zkcrypto/release/libzk_crypto.a(panic_unwind-446a53c2e468675f.panic_unwind.2veyqdoo-cgu.0.rcgu.o):/rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca//library/panic_unwind/src/gcc.rs:282: first defined here
../ssi/vade-evan/libvade_evan.a(alloc-20f26f875d0170e2.alloc.93720895-cgu.0.rcgu.o): In function `__rdl_oom':
/rustc/e7119a0300b87a3d670408ee8e847c6821b3ae80/library/alloc/src/alloc.rs:407: multiple definition of `__rdl_oom'
../rust/zkcrypto/release/libzk_crypto.a(alloc-db37898996f45c13.alloc.ek29bnzo-cgu.0.rcgu.o):/rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca//library/alloc/src/alloc.rs:397: first defined here
../ssi/vade-evan/libvade_evan.a(alloc-20f26f875d0170e2.alloc.93720895-cgu.0.rcgu.o): In function `__rg_oom':
/rustc/e7119a0300b87a3d670408ee8e847c6821b3ae80/library/alloc/src/alloc.rs:413: multiple definition of `__rg_oom'
../rust/zkcrypto/release/libzk_crypto.a(alloc-db37898996f45c13.alloc.ek29bnzo-cgu.0.rcgu.o):/rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca//library/alloc/src/alloc.rs:403: first defined here
../ssi/vade-evan/libvade_evan.a(vade_evan.471hxc8eusf8jn8o.rcgu.o): In function `__rust_alloc':
471hxc8eusf8jn8o:(.text.__rust_alloc+0x0): multiple definition of `__rust_alloc'
../rust/zkcrypto/release/libzk_crypto.a(zk_crypto.1d64k9j2q2v9vem0.rcgu.o):1d64k9j2q2v9vem0:(.text.__rust_alloc+0x0): first defined here
../ssi/vade-evan/libvade_evan.a(vade_evan.471hxc8eusf8jn8o.rcgu.o): In function `__rust_dealloc':
471hxc8eusf8jn8o:(.text.__rust_dealloc+0x0): multiple definition of `__rust_dealloc'
../rust/zkcrypto/release/libzk_crypto.a(zk_crypto.1d64k9j2q2v9vem0.rcgu.o):1d64k9j2q2v9vem0:(.text.__rust_dealloc+0x0): first defined here
../ssi/vade-evan/libvade_evan.a(vade_evan.471hxc8eusf8jn8o.rcgu.o): In function `__rust_realloc':
471hxc8eusf8jn8o:(.text.__rust_realloc+0x0): multiple definition of `__rust_realloc'
../rust/zkcrypto/release/libzk_crypto.a(zk_crypto.1d64k9j2q2v9vem0.rcgu.o):1d64k9j2q2v9vem0:(.text.__rust_realloc+0x0): first defined here
../ssi/vade-evan/libvade_evan.a(vade_evan.471hxc8eusf8jn8o.rcgu.o): In function `__rust_alloc_zeroed':
471hxc8eusf8jn8o:(.text.__rust_alloc_zeroed+0x0): multiple definition of `__rust_alloc_zeroed'
../rust/zkcrypto/release/libzk_crypto.a(zk_crypto.1d64k9j2q2v9vem0.rcgu.o):1d64k9j2q2v9vem0:(.text.__rust_alloc_zeroed+0x0): first defined here
../ssi/vade-evan/libvade_evan.a(vade_evan.471hxc8eusf8jn8o.rcgu.o): In function `__rust_alloc_error_handler':
471hxc8eusf8jn8o:(.text.__rust_alloc_error_handler+0x0): multiple definition of `__rust_alloc_error_handler'
../rust/zkcrypto/release/libzk_crypto.a(zk_crypto.1d64k9j2q2v9vem0.rcgu.o):1d64k9j2q2v9vem0:(.text.__rust_alloc_error_handler+0x0): first defined here

Questions

  1. What is happening here?
  2. What are the suggested solutions for eliminating this problem?

It is not possible to link multiple staticlib produced by rustc together as each includes a copy of the rust standard library, causing symbol conflicts: Don't leak non-exported symbols from staticlibs · Issue #104707 · rust-lang/rust · GitHub You can either compile the rust code as cdylibs or create a third rust crate which depends on the former two (make sure to do use library1; use zk_crypto_rs; to ensure rustc actually adds them as dependencies) and then only compile this third crate as staticlib.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.