Bug? Library size increases by 10x with "lib" crate-type

Hi there,

I am not sure whether this is just me not fully understanding something or an actual bug (because the impact is so immense).

For our project I setup a library with FFI bindings to a few internal functions and set the crate-type "staticlib" and "cdylib" in the Cargo.toml. Which is fine, it compiles and works.

A while later, we wanted to introduce JNI bindings for Android. So I created a new crate in our workspace and added the original FFI crate to its dependencies. For it to be found in the scope, I also added "lib" to the crate-type of the (non-JNI) FFI crate. Which seemed fine, it compiles and works. But then I noticed that the size of the (non-JNI) FFI library increased by 10x.

Without "lib" (libconnector.a 9MiB):

> cargo build --release -p connector && grep lib -A 1 libconnector/Cargo.toml && ls $CARGO_TARGET_DIR/release/lib* -lah
   Compiling connector v6.1.0 (/tmp/rust/libconnector)
    Finished release [optimized] target(s) in 45.49s
[lib]
name = "connector"
crate-type = ["staticlib", "cdylib"]

-rw-rw-r-- 2 me me 9,0M Nov 26 15:10 /tmp/cargo-target-2039/release/libconnector.a
-rw-rw-r-- 1 me me 5,9K Nov 26 15:10 /tmp/cargo-target-2039/release/libconnector.d
-rw-rw-r-- 2 me me 5,9M Nov 26 14:55 /tmp/cargo-target-2039/release/libconnector.rlib
-rwxrwxr-x 2 me me 3,8M Nov 26 15:10 /tmp/cargo-target-2039/release/libconnector.so

With "rlib" [or "lib"] (libconnector.a 93MiB).

> cargo build --release -p connector && grep lib -A 1 libconnector/Cargo.toml && ls $CARGO_TARGET_DIR/release/lib* -lah
   Compiling connector v6.1.0 (/tmp/rust/libconnector)
    Finished release [optimized] target(s) in 14.07s
[lib]
name = "connector"
crate-type = ["rlib", "staticlib", "cdylib"]

-rw-rw-r-- 2 me me  93M Nov 26 15:11 /tmp/cargo-target-2039/release/libconnector.a
-rw-rw-r-- 1 me me 5,9K Nov 26 15:10 /tmp/cargo-target-2039/release/libconnector.d
-rw-rw-r-- 2 me me 7,2M Nov 26 15:11 /tmp/cargo-target-2039/release/libconnector.rlib
-rwxrwxr-x 2 me me 7,2M Nov 26 15:11 /tmp/cargo-target-2039/release/libconnector.so

So my question: Is this the intended behaviour? The solution would then be to split my "connector" (FFI) crate into smaller pieces to not mix "lib" and "staticlib"/"cdylib" - or is this a bug?

Binary size of .a archives is often irrelevant. They contain more than just code that is linked into the application. You should compare binary size after linking, and especially after linking with LTO if you care about binary size.

You can see what files are in the .a archive with ar x libconnector.a. It probably contains tons of debug info, and a whole copy or two of the Rust standard library just in case.

3 Likes

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.