c compilation process
DESCRIPTION
This ppt will demonstrate the compilation process stages in C language. C compilation process has 4 stages. They are pre-processor, compiler, assembler and linker. Each stage has its own functionality. The final outcome of these stages would be a C executable file. GCC tool is used in linux platform to compile C code. And It will generate *.out files as an executable.TRANSCRIPT
![Page 1: C compilation process](https://reader038.vdocuments.us/reader038/viewer/2022100600/55614ea3d8b42adb6b8b50d1/html5/thumbnails/1.jpg)
04/12/2023 RAJ KUMAR RAMPELLI
Compilation Process in C
R RAJ KUMAR
![Page 2: C compilation process](https://reader038.vdocuments.us/reader038/viewer/2022100600/55614ea3d8b42adb6b8b50d1/html5/thumbnails/2.jpg)
04/12/2023 RAJ KUMAR RAMPELLI
Compilation process in C
• Compilation process ?– Constructing an executable image for an application– FOUR stages– Command:
gcc <options> <source_file.c>• Compiler Tool– gcc
• man gcc (on Linux m/c)
![Page 3: C compilation process](https://reader038.vdocuments.us/reader038/viewer/2022100600/55614ea3d8b42adb6b8b50d1/html5/thumbnails/3.jpg)
04/12/2023 RAJ KUMAR RAMPELLI
4 stages of compilation processPreprocessorgcc –E app.c –o app.iapp.c Hello.c
Compilergcc –S app.i –o app.s
Assemblergcc –c app.s –o app.o
Linkergcc app.o –o app
Output Executable (a.out)Run ./app (Loader)
![Page 4: C compilation process](https://reader038.vdocuments.us/reader038/viewer/2022100600/55614ea3d8b42adb6b8b50d1/html5/thumbnails/4.jpg)
04/12/2023 RAJ KUMAR RAMPELLI
4 stages of compilation process• Preprocessor
– Expansion of Header files– Substitute macros and inline functions
• Compiler– Generates assembly language– Verification of functions usage using prototypes– Header files: Prototypes declaration
• Assembler– Generates re-locatable object file (contains m/c instructions)– nm app.o
0000000000000000 T main U puts
– Nm or objDump tool used to view object files
![Page 5: C compilation process](https://reader038.vdocuments.us/reader038/viewer/2022100600/55614ea3d8b42adb6b8b50d1/html5/thumbnails/5.jpg)
04/12/2023 RAJ KUMAR RAMPELLI
4 stages of compilation proces (contd..)
• Linker– Generates executable file (nm tool used to view exe file)– Binds appropriate libraries
• Static Linking• Dynamic Linking (default)
Timing of linkingAt Load timeAt Run time
• Loader– Evaluate size of code and data segment– Allocates address space in the user mode and transfers them into memory– Load dependent libraries needed by program and links them– Invokes Process Manager Program registration
![Page 6: C compilation process](https://reader038.vdocuments.us/reader038/viewer/2022100600/55614ea3d8b42adb6b8b50d1/html5/thumbnails/6.jpg)
04/12/2023 RAJ KUMAR RAMPELLI
Compiling a C Program
• gcc <options> program_name.c
Options:------------Wall: Shows all warnings-o output_file_name: By default a.out executable file is created when we compile our program with gcc. Instead, we can specify the output file name using "-o" option.-g: Include debugging information in the binary.
![Page 7: C compilation process](https://reader038.vdocuments.us/reader038/viewer/2022100600/55614ea3d8b42adb6b8b50d1/html5/thumbnails/7.jpg)
04/12/2023 RAJ KUMAR RAMPELLI
Linking Multiple files to make executable file
• If we have two programs say prog1.c and prog2.c for one single task then we can make single executable file using following instructions
First, compile these two files with option "-c"gcc -c prog1.cgcc -c prog2.c
-c: Tells gcc to compile and assemble the code, but not link.
We get two files as output, prog1.o and prog2.oNow, we can link these object files into single executable file using below instruction.
gcc -o prog prog1.o prog2.o
Now, the output is prog executable file.We can run our program using./prog
![Page 8: C compilation process](https://reader038.vdocuments.us/reader038/viewer/2022100600/55614ea3d8b42adb6b8b50d1/html5/thumbnails/8.jpg)
04/12/2023 RAJ KUMAR RAMPELLI
Linking with other libraries• Normally, compiler will read/link libraries from /usr/lib directory to
our program during compilation process.But, there are some case where we need to link our programs with libraries like pthreads and realtime libraries (rt library).Use below instruction for this purpose
gcc <options> program_name.c -lpthread -lrt
-lpthread: Link with pthread library-lrt: Link with rt libraryOption here is "-l<library>"
Another option "-L <dir>" used to tell gcc compiler search for libraries in given <dir> directory.
![Page 9: C compilation process](https://reader038.vdocuments.us/reader038/viewer/2022100600/55614ea3d8b42adb6b8b50d1/html5/thumbnails/9.jpg)
04/12/2023 RAJ KUMAR RAMPELLI
Thank You