Search code examples
c++compiler-optimizationfilesizeobject-files

My C++ object file is too big


I am working on a C++ program and the compiled object code from a single 1200-line file (which initializes a rather complex state machine) comes out to nearly a megabyte. What could be making the file so large? Is there a way I can find what takes space inside the object file?


Solution

  • There can be several reasons when object files are bigger than they have to be at minimum:

    • statically including dependent libraries
    • building with debug information
    • building with profiling information
    • creating (extremely) complex data structures using templates (maybe recursive boost-structures)
    • not turning on optimizing flags while compiling (saves not that much and can cause difficulties if used too extremely)

    At first I suggest to check if you're building with debug information, this causes the most bloat in my experience.