understanding gcc compilation steps : linux compilation steps

Copy paste the below shell script and execute on your terminal.

$ vim understanding_compilation_with_gcc.sh

Copy below highlighted code and run the script as,

$ bash understanding_compilation_with_gcc.sh

 

————- code start ———————

#!/bin/bash

echo “
For any given input file, the file name suffix determines what kind of
compilation is done:

file.c
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.

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

READ  from source code to executable : how executable is created during compilation on linux

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 ——————–

When you run the above script, you will get following files generated,

$ tree
.
├── a.out
├── helloworld
├── helloworld.c
├── helloworld.i
├── helloworld.o
├── helloworld.s
└── understanding_compilation_with_gcc.sh

Leave a Reply

Your email address will not be published. Required fields are marked *