g++: Getting a small libstdc++

Apr 14, 2006 9 Replies

Hello!



I am interested in using C++ (moving away from C) for developing software for an ARM based system.



C++ is known for creating larger binaries compared to C. Additionally I have to add libstdc++ to the flash file system. The lib is about 900 kByte in size when using a recent version of g++.



Given the above limitations, I wonder if c++/g++ is popular for embedded systems.



I found a website that explains how to get rid of libstdc++ so the binaries are not linked against it any more:



formatting link



Of course, one has to abandon c++ library functions and STL. Pity, this method has more adverse effects: you also loose the new() operator and exceptions, which is bad :(



Is there a way I can use g++ with a small libstdc++ which includes new() and maybe even exceptions?



Cheers Daniel Kabs


If all required libraries are available as archives (.a) then you should be able statically link your app as a standalone program. Of course you would presumably still need the linux kernel. and creating a static app would probably affect the licenses of any open source libraries you are linking against.

Just because you link against a (.a) file doesn't mean every routine in the archive gets included in your executable. Only those modules (.o) in the library that are referenced (directly or indirectly) will go into your exec.

I would use this route except that I have five c++ execs running on my system and I actually save space by using shared libraries.

I agree, dynamically linking is the way to go.

The question is, how to create a tailor-made libstdc++.so. This would involve finding all symbols needed from libstdc++ and creating a lib including this minimum set of functions.

Cheers Daniel

not a trivial task by any stretch of the imagination.

You almost certainly do not want to be using exceptions in embedded code, and templates would be a disaster if they are not used efficiently.

Why are you doing this in the first place ?

Could you please be more verbose on that?

I thought it's nice to have exceptions e.g. to handle errors occuring in the constructor. Why do you advise against using exceptions?

Cheers Daniel

Yes, the C++ compiler generates a fairly significant amount of extra code in order to support syntactic sugar like templates and exceptions. Large code on an embedded machine is a bad thing, because (a) you've got less memory and little storage space; and (b) because larger code increases the probability of page faults and cache misses, which means that the code will likely run slower on an already slow CPU.

I agree that for a very large software project, when correctly used, they certainly make the code cleaner and easier to maintain. But for embedded projects I work on I always advise strict caution. There are certainly cases where templates in particular can improve code reuse and therefore (in theory) reduce code size, but care needs to be taken to ensure that this is the case.

Hello Geronimo!

Thanks for getting back to me. I know that memory and storage space is a critical factor regarding embedded systems. That was exactly the motivation for my question.

You are saying that I should abstain from templates and exceptions because they create bloated code. But how much extra code is created? Can I measure it by comparing the size compiling with and without exceptions?

Cheers Daniel

The only way to check would be to rewrite your code without templates and exceptions, and compare.

Naaa, it's not that hard :-) if you can do without STL.

I figured that there's already a library included with g++ that provides basic support for new(), delete() and rtti: libsupc++.a . There's also an auxiliary library for exceptions: libgcc_eh.a

Linking against these libraries instead of libstdc++ one can reduce the binary size and storage space for libraries considerably!

Cheers Daniel

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required