Home » Development and Build » Compilation » Understanding gcc compilation steps : Linux compilation steps

Understanding gcc compilation steps : Linux compilation steps

For any input file to the GCC compiler, the extension determines what kind of compilation stages needs to be performed.

file.c => The file name with extension “dot c” is the C source code that must be preprocessed.

file.i => C source code that should not be preprocessed.

file.h => C, header file to be turned into a precompiled header (default)

file.s => Assembler code.

file.S => Assembler code that must be preprocessed.

$ vim helloworld.c
#include <stdio.h>

#define MY_NAME "DevBee"

int main(int argc, char **argv) {
        printf("validating preprocessor string: %s\n", MY_NAME);
        printf("Hello world for understanding compilation\n");
        return 0;
}

Check the GCC version as,

$ gcc --version
gcc (Ubuntu 12.2.0-3ubuntu1) 12.2.0

Generate all the temporary files which are created during compilation as,

 $ gcc -o helloworld -v -save-temps helloworld.c
$ tree
.
├── helloworld
├── helloworld.c
├── helloworld.i
├── helloworld.o
└── helloworld.s

0 directories, 5 files

If we check the each file types, it is as below,

$ file helloworld*

helloworld:   ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=32deb5f47e5b1f4bac853292e55fc3eddc28ff6f, for GNU/Linux 3.2.0, not stripped

helloworld.c: C source, ASCII text

helloworld.i: C source, ASCII text

helloworld.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

helloworld.s: assembler source, ASCII text

Where, helloworld.c is the C source code which we have written and is in human readable format.

helloworld.i – is the preprocessed source code. In this all the headers “#include” and macros “#define” will get replaced with actual files and values.

helloworld.o – This is the object files, which the output of compilation and next step of preprocessing.

helloworld.s – This is assembly files and output of assembler stage.

helloworld – This is final executable.

Using GCC arguments we can also try the single steps during compilation as below,

-E – Preprocess only; do not compile, assemble or link ( -E Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output. )

$ gcc -E helloworld.c

-S – Compile only, do not assemble or link ( -S Stop after the stage of compilation proper; do not assemble. The output is in the form of an assembler code file for each non-assembler input file specified. By default, the assembler file name for a source file is made by replacing the suffix .c, .i, etc., with .s. )

$ gcc -S helloworld.c

-c – Compile and assemble, but do not link (-c Compile or assemble the source files, but do not link. The linking
stage simply is not done. The ultimate output is in the form of an object file for each source file. By default, the object file name for a source file is made by replacing the suffix .c, .i, .s, etc., with .o. )

$ gcc -c helloworld.c

-o file – Place the output into file ( Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code. )

$ gcc -o helloworld helloworld.c

———— code start ———————

If you only want some of the stages of compilation, you can use -x (or
filename suffixes) to tell gcc where to start, and one of the options -c,
-S, or -E to say where gcc is to stop. Note that some combinations (for
example, -x cpp-output -E) instruct gcc to do nothing at all.

-c Compile or assemble the source files, but do not link. The linking
stage simply is not done. The ultimate output is in the form of an
object file for each source file.

By default, the object file name for a source file is made by
replacing the suffix .c, .i, .s, etc., with .o.

Unrecognized input files, not requiring compilation or assembly, are
ignored.

-S Stop after the stage of compilation proper; do not assemble. The
output is in the form of an assembler code file for each non-
assembler input file specified.

By default, the assembler file name for a source file is made by
replacing the suffix .c, .i, etc., with .s.

Input files that don’t require compilation are ignored.

-E Stop after the preprocessing stage; do not run the compiler proper.
The output is in the form of preprocessed source code, which is sent
to the standard output.

Input files that don’t require preprocessing are ignored.

-o file
Place output in file file. This applies to whatever sort of output
is being produced, whether it be an executable file, an object file,
an assembler file or preprocessed C code.

###############################################################

echo “our helloworld program is”

echo “

#include <stdio.h>

#define MY_NAME \”DevBee\”

int main(int argc, char **argv) {
printf(\”validating preprocessor string: %s\n\”, MY_NAME);
printf(\”Hello world for understanding compilation\n\”);
return 0;
}

” > helloworld.c

#################################################################
echo “——————————————–“
echo “displaying gcc version”
echo ” “
gcc –version

echo “——————————————–“
echo “-dumpspecs – Display all of the built in spec strings”
echo ” “
gcc -dumpspec helloworld.c

echo “——————————————–“
echo “-dumpversion – Display the version of the compiler”
echo ” “
gcc -dumpversion helloworld.c

echo “——————————————–“
echo “-dumpmachine – Display the compiler’s target processor”
echo ” “
gcc -dumpmachine helloworld.c

echo “——————————————–“
echo “-print-search-dirs – Display the directories in the compiler’s search path”
echo ” “
gcc -print-search-dirs helloworld.c

echo “——————————————–“
echo “-print-libgcc-file-name – Display the name of the compiler’s companion library”
echo ” “
gcc -print-libgcc-file-name helloworld.c

echo “——————————————–“
echo “-print-file-name=<lib> Display the full path to library <lib>”
echo ” “
gcc -print-file-name=gcc helloworld.c

echo “——————————————–“

echo ” -print-prog-name=<prog> Display the full path to compiler component <prog> “

echo “——————————————–“

echo ” -print-multiarch Display the target’s normalized GNU triplet, used as a component in the library path “
echo ” “
gcc -print-multiarch helloworld.c
echo “——————————————–“

echo ” -print-multi-directory Display the root directory for versions of libgcc “
echo ” “
gcc -print-multi-directory helloworld.c
echo “——————————————–“

echo ” -print-multi-lib Display the mapping between command line options and multiple library search directories “
echo ” “
gcc -print-multi-lib helloworld.c
echo “——————————————–“

echo ” -print-multi-os-directory Display the relative path to OS libraries “
echo ” “
gcc -print-multi-os-directory helloworld.c
echo “——————————————–“

echo ” -print-sysroot Display the target libraries directory “
echo ” “
gcc -print-sysroot helloworld.c
echo “——————————————–“

echo ” -print-sysroot-headers-suffix Display the sysroot suffix used to find headers “
echo ” “
gcc -print-sysroot-headers-suffix helloworld.c
echo “——————————————–“
echo “-save-temps – Do not delete intermediate files”
echo ” “
gcc -o helloworld -v -save-temps helloworld.c
echo “——————————————–“

echo “-pipe – Use pipes rather than intermediate files”
echo ” “
gcc -pipe helloworld.c
echo “——————————————–“

echo “-time – Time the execution of each subprocess”
echo ” “
gcc -time helloworld.c
echo “——————————————–“

echo ” -E – Preprocess only; do not compile, assemble or link “
echo ” “
gcc -E helloworld.c
echo “——————————————–“

echo ” -S – Compile only, do not assemble or link “
echo ” “
gcc -S helloworld.c
echo “——————————————–“

echo ” -c – Compile and assemble, but do not link “
echo ” “
gcc -c helloworld.c
echo “——————————————–“

echo ” -o file – Place the output into file “
echo ” “
gcc -o helloworld helloworld.c
echo “——————————————–“

————— code end ——————–


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

1 thought on “Understanding gcc compilation steps : Linux compilation steps”

Leave a Comment