Why are my compiled files so huge?

Hey everyone,

At the end of each day I usually tar my files together and back up my work by emailing to to myself. I'm working on a relatively small project, but I noticed that the tarball was 40MB!

I looked at the files individually and noticed that my source files are relatively small (~5kb), but their corresponding executables are 15MB each! Why is this?

Here is a sample of one that is 2827B in its source code, but 15MB when compiled:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef _LAYER3D_
#define _LAYER3D_


#include <iostream>
#include <cmath>
using namespace std;
#include "Point3d.h"
#include "Point.h"
#include <vector>
#include "Grid.h"


class Layer3d{

	/*
	There are two grids: the "real" one and the "display" one.
	The real one is the coordinates of the grid in 3 dimensional space.
	The display one is the real grid, projected onto the 2D screen.
	*/
	public:
		vector<Point3d> real_grid;
		vector<Point> display_grid;
		double spacing;
		int numPts_x, numPts_y, numOfPts;
		Point3d StartingPoint;
		Point3d RotatePt;

		Layer3d(){
			real_grid.clear();
			display_grid.clear();
			Point3d tempPt(0,0,0);
			Point tempPt2(0,0);
			StartingPoint = tempPt;
			RotatePt = tempPt;
			spacing = 1;
			real_grid.push_back(tempPt);
			display_grid.push_back(tempPt2);
			numPts_x = 1;
			numPts_y = 1;
			numOfPts = 1;
			
		}//Default constructor.
		
		Layer3d(Grid grid_in){

			numPts_x = grid_in.numPts_x;
			numPts_y = grid_in.numPts_y;
			spacing = grid_in.spacing;
			StartingPoint = grid_in.StartingPoint;
			RotatePt = grid_in.RotatePt;
			numOfPts = grid_in.numOfPts;
			real_grid.clear();
			display_grid.clear();
			real_grid = grid_in.grid;
			for(int i = 0; i<numOfPts; i++){
				Point tempPt_display(real_grid[i].x, real_grid[i].y);
				display_grid.push_back(tempPt_display);
			}//for i


		}//Layer3d

		void rotateAroundX(double theta){//Theta needs to be in radians. This rotates the vector around the axis in question with respect to the right hand rule with the other two axes.
			for(int i = 0; i<numOfPts; i++){
				Point3d realPt = real_grid[i];
				Point3d relToRP = realPt - RotatePt;	
				Point3d newPoint(RotatePt.x + relToRP.x,RotatePt.y + cos(theta)*relToRP.y - sin(theta)*relToRP.z,RotatePt.z + sin(theta)*relToRP.y + cos(theta)*relToRP.z);
				real_grid[i] = newPoint;
				display_grid[i].x = newPoint.x;
				display_grid[i].y = newPoint.y;
			}//i
		}//rotateAroundX


		void rotateAroundY(double theta){
			for(int i = 0; i<numOfPts; i++){
				Point3d realPt = real_grid[i];
				Point3d relToRP = realPt - RotatePt;	
				Point3d newPoint(RotatePt.x + relToRP.x*cos(theta) + relToRP.z*sin(theta),RotatePt.y + relToRP.y,RotatePt.z + -sin(theta)*relToRP.x + cos(theta)*relToRP.z);
				real_grid[i] = newPoint;
				display_grid[i].x = newPoint.x;
				display_grid[i].y = newPoint.y;
			}//i
		}//rotateAroundY


		void rotateAroundZ(double theta){
			for(int i = 0; i<numOfPts; i++){
				Point3d realPt = real_grid[i];
				Point3d relToRP = realPt - RotatePt;	
				Point3d newPoint(RotatePt.x + cos(theta)*relToRP.x - sin(theta)*relToRP.y,RotatePt.y + sin(theta)*relToRP.x + cos(theta)*relToRP.y,RotatePt.z + relToRP.z);
				real_grid[i] = newPoint;
				display_grid[i].x = newPoint.x;
				display_grid[i].y = newPoint.y;
			}//i
		}//rotateAroundZ

};


#endif 


Why does this happen? It is a .h file. When I compile it, I compile it with the rest of the files, like so:

g++ -o runme onefile.cpp anotherfile.cpp someclass.h someotherclass.h

Could that be why?

Thanks!
Although you may think that's all the code in that compiled unit, this is no the case. There's a lot more code in those C++ header files you're pulling in.

If you really want to see what the compiler has to contend with run:

cpp onefile.cpp


Do you really pass .h files to the compiler?
Ahhhh, so it's grabbing <vector> and all that stuff?


Errr...is passing .h files to the compiler bad practice? My makefile looks like this right now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
objects =  dispLattice.o

sfmllibs = -lsfml-graphics -lsfml-window -lsfml-system

DispLatticeRun: $(objects)
	g++ -o DispLatticeRun $(objects) $(sfmllibs) -ggdb

dispLattice.o : dispLattice.cpp Point.h Point3d.h Layer3d.h Grid.h
	g++ -ggdb -Wall -c dispLattice.cpp Point.h Point3d.h Layer3d.h Grid.h

clean : 
	rm rundsmc2d $(objects)

cleanh:
	make clean && make

Don't pass headers to the makefile. Their only purpose is to be #included in source files.
You are also compiling with debug information (-ggdb) which will bloat the .o size and executable size considerably. Also, you are linking 3 SFML libraries. How big are those?
Ahhh, ok...I didn't know that I shouldn't pass headers to the makefile. But wait! If I don't, and I have an error in a header file, will it find it when it's compiling just because it's included in another file that's explicitly being compiled?

Ahhhhhhhhh. Yeah, I just kind of compile it with gdb variables so they're there on the rare occasion that I need to use them.

I'm not sure about how big SFML libs are, but they could be huge. Good point.

So, for gits and shiggles I tried compiling with a few different things and looking at the sizes. Here is the simplest, not compiling the header files and no -ggdb:

$ g++ -o DispLatticeRun dispLattice.cpp -Wall -lsfml-graphics -lsfml-window -lsfml-system

This only produces the executable DispLatticeRun and it is 72 kB.

Running with -ggdb:

$ g++ -o DispLatticeRun dispLattice.cpp -Wall -lsfml-graphics -lsfml-window -lsfml-system -ggdb

Produces just DispLatticeRun again but it is 215 kB.

Now, compiling them all, without -ggdb:

1
2
$ g++ -c dispLattice.cpp Point.h Point3d.h Layer3d.h Grid.h
$ g++ -o DispLatticeRun dispLattice.o -lsfml-graphics -lsfml-window -lsfml-system


This produces all these .gch files that are so large:

1
2
3
4
5
$ ls -sh
total 34M
4.0K dispLattice.cpp  4.0K Grid.h       14M Layer3d.h.gch  4.0K pix            4.0K Point.h
160K dispLattice.o     13M Grid.h.gch  4.0K makefile       4.0K Point3d.h      1.8M Point.h.gch
 72K DispLatticeRun   4.0K Layer3d.h   4.0K oldfiles       5.5M Point3d.h.gch


If I do the same, but with the -ggdb flag:

1
2
$ g++ -c dispLattice.cpp Point.h Point3d.h Layer3d.h Grid.h -ggdb
$ g++ -o DispLatticeRun dispLattice.o -lsfml-graphics -lsfml-window -lsfml-system


I also get enormous files, but they're slightly larger:

1
2
3
4
5
$ ls -sh
total 37M
4.0K dispLattice.cpp  4.0K Grid.h       15M Layer3d.h.gch  4.0K pix            4.0K Point.h
464K dispLattice.o     15M Grid.h.gch  4.0K makefile       4.0K Point3d.h      1.8M Point.h.gch
216K DispLatticeRun   4.0K Layer3d.h   4.0K oldfiles       5.9M Point3d.h.gch


So it looks like the real culprit is compiling the header files. But why are they so large? The main file is much larger than any of them but only compiles to be about 400kB.
Topic archived. No new replies allowed.