M K Saravanan | mksarav AT gmail DOT com May 2007 gcc-exercise-1 =============================================================================== $ cat /etc/redhat-release Red Hat Enterprise Linux ES release 4 (Nahant) $ uname -a Linux rdfwvoip 2.6.9-5.ELsmp #1 SMP Wed Jan 5 19:30:39 EST 2005 i686 i686 i386 G NU/Linux man gcc $ gcc --version gcc (GCC) 3.4.3 20041212 (Red Hat 3.4.3-9.EL4) Copyright (C) 2004 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ------------------------------------------------------------------------------- 1) using -S option 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. /* hello.c */ #include int main() { printf("Hello, world\n"); return 0; } /* end of hello.c */ gcc -S hello.c out: hello.s (open in a text editor and have a look) hello.s contents: .file "hello.c" .section .rodata .LC0: .string "Hello, world\n" .text .globl main .type main, @function main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp movl $0, %eax addl $15, %eax addl $15, %eax shrl $4, %eax sall $4, %eax subl %eax, %esp subl $12, %esp pushl $.LC0 call printf addl $16, %esp movl $0, %eax leave ret .size main, .-main .section .note.GNU-stack,"",@progbits .ident "GCC: (GNU) 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)" ------------------------------------------------------------------------------- 2) using -E option 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 which don't require preprocessing are ignored. /* ex1.c */ #include "ex1.h" int main() { return 0; } /* end of ex1.c */ /* ex1.h */ #ifndef _EX1_H_ #define _EX1_H_ #define ONE 1 #define TWO 2 #endif /* _EX1_H_ */ /* end of ex1.h */ gcc -E ex1.c > ex1.i out: ex1.i (open in a text editor and have a look) ex1.i contents: # 1 "ex1.c" # 1 "" # 1 "" # 1 "ex1.c" # 1 "ex1.h" 1 # 3 "ex1.c" 2 int main() { return 0; } The same effect can also be produced by using the GNU C pre-processor, cpp. cpp ex1.c > ex1.i man cpp ------------------------------------------------------------------------------- 3) using -c option 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. gcc -c hello.c out: hello.o "file" utility will tell you about the content of a file. Try this on hello.o. $ file ./hello.o ./hello.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripp ed The utility "objdump" will display info from object files. man objdump Try the following: objdump -x ./hello.o objdump -s ./hello.o objdump -t ./hello.o objdmp -T ./hello.o Also experiment with other options in objdump. ------------------------------------------------------------------------------- 4) all in one step -o is used to mention output file name. gcc -o hello hello.c $ file ./hello ./hello: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), not stripped $ ls -l hello -rwxr-xr-x 1 mksarav firmware 4743 May 17 14:23 hello Note the size of "hello". It is 4743 bytes. The "strip" utility discard symbols from object files. $ strip ./hello $ file ./hello ./hello: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped "file" now says "hello" is stripped. $ ls -l hello -rwxr-xr-x 1 mksarav firmware 2960 May 17 14:24 hello Note down the size of the "hello" now--it is 2960 bytes! -------------------------------------------------------------------------------