I am wrote code for ARM Cr4 then i compiled to create .a file (static lib) but the size in 6MB that is not good in embedded. what i have to do?
file size doesn't matter, this is true for libraries, but it's also true for the final ELF file: at the end of day, only the code segment (typically named .text
) is flashed to the device rom.
a large portion of the on-disk file is data used by developement tools such as debuggers or probes, so you can get better development experience.
Thank you for your response, is there any way to optimize code?
- using
strip
command, but mainly for executable file - build in release mode to remove debug info.
- check default features of dependencies in your project's
Cargo.toml
. Only use neccessary features, like:
regex = { version = "...", default-features = false, features = ["std"] }
unless you are exceeding your rom budget, typically you don't need special tricks to further reduce the code size, setting the opt-level
to "s"
in your "release" profile should be good enough. a brief description can be found in the embedded rust book:
https://docs.rust-embedded.org/book/unsorted/speed-vs-size.html
There are various guides about optimizing embedded Rust for size around. I don't have direct experience with these, so if someone suggestions a specific guide, take their word over mine -- I just found this example via web search.
But I've seen enough discussions to know that panic and formatting machinery play significant roles, if you can find ways reduce or eliminate those.
rustc -C opt-level=0 -C lto --target=armv7r-none-eabi lib.rs
from 6MB to 3.29MB also not a good result.
You need to say more about what you are doing:
How much program space do you have on your ARM board?
Do you have an operating system on there?
Do you need the standard library. Small embedded systems are built with no_std
How are you measuring code size? Simply looking at the size of an object file tells you nothing as it it will be full of information besides the executable code.
Perhaps you could show your code.
Have you read this: no_std - The Embedded Rust Book.
Rust is used on tiny ARM systems for example this : Rewriting m4vgalib in Rust - Cliffle so I'm sure what you want can be done.
The .a
files built by staticlib
crate type usually contain a substantial amount of unused code from the standard library and other imported libraries, which the linker is supposed to eliminate. You need to link it to a final executable to find the actual code size. You will also certainly want the linker flag -Wl,--gc-sections
.
I have to implement rust lib (or .rs) file to main.c project (Embedded), and target is AMR CR4(--target=armv7r-none-eabi).
so i created static lib from .rs to .a file, and .a file size is apx 6MB which is not good for embedded, so i want to make my static lib so small that can fit into ARM board.
so i want to reduce my .a file size.
Yes but why?
My understanding of ARM Cortex R4 systems is that they only have about
1MB or 2MB of Flash storage and 128KB or 160KB of RAM. For example: Cortex Evaluation Boards Comparison
That leads me to think you either have no operating system on there or some tiny OS that likely does not understand file systems and .a files.
It really would help if you would answer the questions I posted above.
For the record, -C opt-level=0
means no optimizations are enabled, which is already the default for rustc
. That typically results in quite large binaries.
To optimize your build, use -C opt-level=3
(which is the default for cargo build --release
), or -C opt-level=s
(to optimize for binary size like @nerditation mentioned above).
How much program space do you have on your ARM board?
Ans: 500kb FW
Do you have an operating system on there?
ans: threadX
Do you need the standard library. Small embedded systems are built with no_std
ans: No
How are you measuring code size?
Ans: final exe file.
I tired with -C opt-level=s (3,z) but result is in MB only , what i found when i unpack .a file it contains lots of .o file inside it, its better i can create .o file directly and pass with my C project while compilation. that will be good because .o file size comes with 1.44kb only.