C++ Virtual Functions Generate Immense Executables

All design tool related questions: compiler, assembler, linker. Embedded programming questions: assembler, C code.

Moderator: nferre

Posts: 14
Joined: Mon Mar 11, 2013 12:48 am

C++ Virtual Functions Generate Immense Executables

Fri Oct 09, 2015 11:57 pm

I have been porting a collection of code of mine from Studio 6.2 to Studio 7. As part of that operation, I noticed that my executable was a lot larger than it used to be. Radical surgery to remove almost my entire codebase leaves me with the following minimal example, located within a single source file:

Code: Select all

class Task
        Task(int i);
        virtual void taskHandler() = 0;

Task::Task(int i) {}     //  <---- This is the critical line

class MyTask : Task {
        MyTask(int i) : Task(i) {}
        void taskHandler() {}

MyTask* m;

int main(void) {
    m = new MyTask(1);
The point is that I have a base class that declares a pure-virtual handler, and another class that inherits the original class and defines a handler. Compiling the code exactly as above gives me a footprint of 1.6K, which is not too bad since it brings in malloc() to perform the new(), and stuff like that. The important part is that things are OK when this example exists as 1 single main.cpp source file.

When I put the description of the base class into a separate Task.h file, that is still OK.

But when I put the definition of the Task constructor (the line marked as being critical) into its own Task.cpp file, and then compile the system from 2 .cpp files, the executable footprint grows from 1.6K to 68K. Putting the critical line back into main.cpp shrinks the footprint back down again.

I am stuck. The single-file solution is not a workaround: I need my base class to exist in a separate file since my system creates subclass tasks with different handlers all over the place. I am unable to find a way to get GNU 'ld' to explain why it is pulling so much stuff in, or what is triggering the cascade.

Any ideas?

I have tried many experiments, and it all comes down to whether the constructor line is in a separate file.
Posts: 14
Joined: Mon Mar 11, 2013 12:48 am

Re: C++ Virtual Functions Generate Immense Executables

Tue Oct 13, 2015 11:10 pm

In case anyone else runs into this, I found out a few of the issues. There was some good data on these two sites:

http://elegantinvention.com/blog/inform ... remetal-g/
http://www.webalice.it/fede.tft/cpp_on_ ... ricks.html

The main issue is that C++ exception handling can make your code huge. But even if you compile with '-fno-exceptions' to disable all C++ exception handling, there can be 'hidden' sources of exception handling from the C++ library that have a side effect of adding exception handling back into your system without your knowledge. In my case, the biggest code reduction came from adding an empty __verbose_terminate_handler(), as per the pages above. Simply doing that removed 50K from my build. Adding the __cxa__pure_virtual() error handler, and defining my own malloc-related routines reduced things by a bit more.

Return to “Development Tools”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest