Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi all,

We are working on CrossCore Tool, on which we have developed a embedded C++ application.
Our binary size is big, but we need to reduce the binary size so as to fit in different MCU!
Is there any techniques in C++ that really help us in reducing binary size?
The compiler we use is GCC.
Please help us in this regard!

We have tried

Inline Functions, Virtual Functions, Unused variables removal and Moving local variables to the inner most scope

But these techniques have helped us in improving performance rather than size, we need size reduction!

Raja Vignesh

What I have tried:

Hi all,

We have tried

-> Inline Functions
-> Virtual Functions
-> Unused variables removal
-> Moving local variables to the inner most scope

But these techniques have helped us in improving performance rather than size, we need size reduction!
Updated 20-Feb-18 6:16am
phil.o 20-Feb-18 6:52am    
Are you using a lot of macros? Macros can cause size overhead, depending on how often they are used and how they are defined.
Raja Vignesh. R 20-Feb-18 7:15am    
No macros used
OxEED0 20-Feb-18 8:32am    
Do you use STL or other template-based libraries? Templates are compile-time code generators, try to avoid them (STL, Boost, etc.).

It looks like you are trying to stuff too much code into too little memory. There is no standard procedure that will surely help. Too much is too much. One big question still is: Is your vexecutable too big to be installed or do you run out of memory at runtime? From your question I assume the first.

Try the following steps, but be advised that they become progressively desperate and may still not be enough to solve the problem.

1) Start looking for redundancy in your code. Having similar code more than once obviously is a big waste.

2) Look over your code and try to identify the functions that generate the longest code. Try to rewrite and optimize them. Use different data structures or algorithms that use less memory.

3) Throw out the code of minor features which may be nice to have, but are not essential.

4) Negotiate new requirements to make more code nonessential and then throw it out.
Share this answer
The techniques you have tried won't reduce your code size at all, some of them make your code even bigger.

- Inline functions: Are a tool to make your code faster and generally make your code bigger!

- Virtual functions: Have nothing to do with code size.

- Unused variable removal: Don't reduce your code size and the compiler will probably take care of them anyway.

- Moving local variables to the inner most scope: Has nothing to do with code size.

So what should you try? First of all you should analyze your code and see where the big chunks are. Take a linker listing and see what uses up most of your code size. Then work on those things.

If your code is big, because you have tens of thousands of functions, there is not much you can do except to try to find out common functionality and restructure your code accordingly.

In general there are two big issues that can potentially blow up your code:

1. The libraries you are using -- you don't have much influence there.

2. Usage of templates: This can blow up your code significantly if not done properly.

Also check whether you are still building in debug mode, instead of production mode. The debug information that the compiler adds can make your code several times bigger!

Good luck.
Share this answer
CPallini 20-Feb-18 9:04am    
CodeWraith 20-Feb-18 12:54pm    
Short inlined functions can make your executable smaller. Don't forget the hidden overhead of passing parameters and cleaning up the stack when calling regular functions.
nv3 20-Feb-18 15:33pm    
Therefore I wrote "generally" :-)
-> Inline Functions
Bad: inline functions explicitly increase code size to the benefit of performances due to less stack manipulation required.
-> Virtual Functions
No impact at best, actually it increases the code size a bit due to the virtual tables needing space.
-> Unused variables removal
Unless they are statics it does not change code size but the memory footprint of the application when running. Good but not for what you want to achieve.
-> Moving local variables to the inner most scope
What for? A variable declaration does not increase code size, only memory footprint. Inside a function ALL the variables are memorized in the stack. At best, with very specialized compilers, you earn a stack address reuse to minimize runtime memory footprint.

To minimize code size you should:
* Eliminate all unneeded static libraries.
* Reduce the call paths and the number of intermediate objects in the hierarchy.
* If possible, with functions having multiple signatures, using only a single one of them in order to not have the other variants linked at compile time. Careful: a single missed function will add its variant to the linked file.
* See if your cross platform framework has options (usually in form of #defines) to eliminate most of th marshalling and intermediate calls when compiled for a specific MCU. Cross platform frameworks are great but they do increase code size.
* Eliminate any huge library like boost, which is fantastic but bloated.

These are my 2 cents,
Share this answer
The fattest issue are normally resources like bitmaps or strings, but you didnt mention them. Did you use frameworks like libraries?

The next level is to use the compiler and linker for smaller size like with removing unused functions. Enhance the output in the build process.

Check that it is not a debug build or with some excessive diagnosis tool. My top tip ;-)

Then think about data size like having big buffers or structs. Always work with pointers and the smallest data size.
Share this answer
Take a look at the suggestions here: How to make smaller C and C++ binaries[^]
Share this answer
But these techniques have helped us in improving performance rather than size, we need size reduction!

Difficult to tell without seeing code.
General technique to reduce binary size
- Remove code duplicates: find code duplicates or almost duplicates and see how you can reduce the number of duplicates.
Example: you have a bubble sort and an insertion sort, chances are that a single subroutine can do the job.
- Ensure the code is efficient.

General technique to reduce runtime size
- When handling arrays, try to avoid intermediate copies of the arrays when possible.

Code optimizing is almost a job by itself, hiring a specialist can be your solution.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900