Understanding GCC: How Compilers, Linkers, and Loaders Work Together to Run C Programs
This guide delves into the system software involved in compiling a C program using GCC. It explains the roles of the preprocessor, compiler, assembler, linker, loader, and operating system in the compilation process. By defining the actions of each component, the text clarifies how they work together to transform source code into an executable program. A block diagram illustrates the order of operations, helping to visualize the relationships between these essential system programs.
Understanding GCC: How Compilers, Linkers, and Loaders Work Together to Run C Programs
E N D
Presentation Transcript
Today’s Topic • Breakdown of GCC script • Description of how different system programs work together to build (and run) a C program.
System Software (for today) • Assembler • Compiler • Linker • Loader • Operating system • Preprocessor
Today’s Objectives • Within context of “compiling” a C program • Define actions of linker, compiler, assembler, OS, macro preprocessor, loader • Draw block diagram showing order of linker, compiler, assembler, macro preprocessor, loader
Breaking Down CC Preprocessed Source source ASM Compiler Assembler Preprocessor .s O B J E C T Executable Program In Memory Executable Loader Linker .o a.out
Breaking Down CC Preprocessed Source source ASM Compiler Assembler Preprocessor .s O B J E C T Executable Program In Memory Executable Loader Linker .o a.out
C Preprocessor (cpp) • Pass over source • Insert included files • Perform macro substitutions • Define macros • #define NUM 100 • #define xx(v,name,op,metrics) \ v=xxinit(op,name,IR->metrics) • gcc –E example.c sends preprocessor output to stdout
Breaking Down CC Preprocessed Source source ASM Compiler Assembler Preprocessor .s O B J E C T Executable Program In Memory Executable Loader Linker .o a.out
Compiler • gcc actually name of a script • Compiler translates one language to another (or the same???) • gcc compiler translates C to assembler • gcc –S example.c “saves” example.s • Compiler consists of • Parser • Code generation • Mysticism
Breaking Down CC Preprocessed Source source ASM Compiler Assembler Preprocessor .s O B J E C T Executable Program In Memory Executable Loader Linker .o a.out
Assembler • Another translator ??? (as example.s) • Assembler to (binary) object • Why not compile straight to binary? • gcc –c example.c to “save” object • NM to look at object (nm example.o)
Breaking Down CC Preprocessed Source source ASM Compiler Assembler Preprocessor .s O B J E C T Executable Program In Memory Executable Loader Linker .o a.out
Linker • Combines objects, both user .o files and libraries; makes an executable file • gcc *.o –lm yields a.out • gcc –o myExec *.o –lm • Use nm to look at executable
Breaking Down CC Preprocessed Source source ASM Compiler Assembler Preprocessor .s O B J E C T Executable Program In Memory Executable Loader Linker .o a.out
Loader • Runs when you type ./a.out • Gets an address to place program (from the operating system) • Changes necessary addresses (if any) • Places code into memory
Operating System • OS is ALWAYS running • Oversees whole compilation process • “Recognizes” gcc example.c command • Parses flags and arguments • Invokes gcc script • Performs memory management (malloc) • Chooses “address” to place program
Compiler Optimization • What does optimization mean? • What does compiler optimization mean? • “Optimization” takes more time to compile in hopes of generating faster executable • -O# is the flag to ask for optimization • gcc –O3 tsp.c gives “highest” level