SPO600 Lab 4 Compiling C Code

This post is going to focus on compiling C code with different options and seeing what the compiler does to the end result, by looking into objdump files of the compiled program.

The Program we will use is :

#include <stdio.h>

int main() {
    printf("Hello World!\n");

Originally we will compile it using the following options :

-g               # enable debugging information
-O0              # do not optimize (that's a capital letter and then the digit zero)
-fno-builtin     # do not use builtin function optimizations

The 6 Options we will try and modify the process with are :

(1) Adding the compiler option -static.

(2) Removing the compiler option -fno-builtin.

(3) Removing the compiler option -g.

(4) Adding additional arguments to the printf() function in your program.

(5) Moving the printf() call to a separate function named output(), and call that function from main().

(6) Removing -O0 and add -O3 to the gcc options.

Option 1.

Adding the -static options into our compiler options, has made the program size at least 900 times larger. When using objdump on the program we get to see all the libraries linked to C get directly compiled into our program. This does a number of things, one it makes the program larger as we no longer have the linker, all related functions must be inside the program itself. Two the program may run faster, because we have no need to go elsewhere to look for function and can just directly call functions from inside the program, the speed should increase, but it is hard to tell on a small program such as this. Overall we can say -static expands all related libraries and no longer has need for linker file.

Option 2.

Removing the compile option fno-builtin, from the looks at the objdump from removing this compiler options, it seems the compiler tries and finds a more direct function call for what it is trying to do, so instead of printf() it tries to find the underling function that it can call to improve the performance.

Option 3.

Removing the compile option -g gets rid of all extra code that is inserted by the compiler for debugging purposes, some of the notable items are line numbers and debugging statements. Useful in helping reduce the size of the compiled program.

Option 4.

Adding additional arguments to the printf() statements, adds extra registers to the program that are used to store the arguments, however after a certain count of arguments the program puts them onto a stack, this will start reducing the performance of the program, as the cpu will have to get them from memory.

Option 5.

Moving the printf() to a separate function and calling it from there, makes no real difference except for the fact that main now calls the created function first, and then calls the printf(), this has a small increase in compiled source code size.

Option 6.

Removing -o0 and adding -o3 tells the compiler to optimize the program, here is a small list of  what each stage does :

0 – No optimization
1 –  Do some optimization
2  – Do all the safe optimization
3  – Disregard safety do all possible optimizations

To finish up, just want to point out that the compiler does a lot of work, that we may not even know about. Giving compiler free reign can have it do drastic things to help optimize it for the machine to run. There are a lot of options, and in some cases those options can have a big performance update on your program, so playing around with them may be worth your time.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s