0

I have a C-code which is optimized for ARM and is compiled with the armcc. I would like to reduce the size of the binary as much as possible. From the standard libraries I'm using

printf(); fopen(); fread(); fwrite(); memset();

I found the option "-nostdlib" which is given to the linker. This option reduces the binary size by a lot, but it gives me errors about undefined references for printf e.g. How would be the right way to exclude the standard libs but still use those functions?

tobkunz11
  • 15
  • 2
  • If you want to use standard-library functions you unfortunately need the standard library. You could possibly use another and more light-weight implementation of the library. – Some programmer dude Mar 16 '17 at 15:22
  • Possible duplicate of http://stackoverflow.com/questions/5307348/how-do-i-include-only-used-symbols-when-statically-linking-with-gcc – Sean Werkema Mar 16 '17 at 15:30
  • It's worth pointing out that a large portion of the standard library consists of the necessary code to implement `printf()` and `fopen()` and `fread()` and `fwrite()`; by the time you've used those four, you probably have half the surface area of the standard C library as a dependency anyway. (`printf()` uses `malloc()`, and numeric formatting, and `FILE` I/O, and a whole raft of other stuff under the hood, for example.) If you really want to cut out stdlib, you'll probably have to use raw `read` and `write` calls to the OS kernel, and implement string formatting yourself. – Sean Werkema Mar 16 '17 at 15:33
  • As Sean Werkema already said: these are very general hence very expensive function (save the `memset()`). `printf()` is the biggest but also most general function where you can safe the most, so what exactly(!) do you need to be printed? – deamentiaemundi Mar 16 '17 at 16:19

0 Answers0