C Program Compilation Phases in Detail - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

C Program Compilation Phases in Detail

Share This


Let us discuss, how to write a program in this language. First, open a terminal and do the following. Open a file using a text editor (you may use vi editor). The extention of the file should be .c , not .C . If you are going to use vi editor, write the following command on your terminal.

#vi test.c

Now, write the following code in that file and save the content.

#include <stdio.h> int main (void) { printf("Hello World!"); re<urn 0; >
Now to compile the program, use cc /span> or gcc command as shown below. #cc test.c At this time, one file will be generated by the name a.out. To run this program do the following and check the result. #./a.out

Compilation of a program - step by step


Let us try with an example code of "Hello world!" program. Instead of compiling the code using gcc Test.c , now we are going to compile the code using gcc Test.c --save-temps , so that temporarily generated files are saved.

Content of Test.i


. . . static __inline int _putchar_unlocked(int _c) { struct _reent *_ptr; _ptr = (__getreent()); return (__sputc_r(_ptr, _c, ((_ptr)->_stdout))); } # 797 "/usr/include/stdio.h" 3 4 # 2 "Test.c" 2 # 3 "Test.c" int main (void) { printf("Hello World!"); return 0; }

Content of Test.s


.file "Test.c" .def __main; .scl 2; .type 32; .endef .section .rdata,"dr" .LC0: .ascii "Hello World!\0" .text .globl main .def main; .scl 2; .type 32; .endef .seh_proc main main: pushq %rbp .seh_pushreg %rbp movq %rsp, %rbp .seh_setframe %rbp, 0 subq $32, %rsp .seh_stackalloc 32 .seh_endprologue call __main leaq .LC0(%rip), %rcx call printf movl $0, %eax addq $32, %rsp popq %rbp ret .seh_endproc .ident "GCC: (GNU) 6.4.0" .def printf; .scl 2; .type 32; .endef

Content of Test.o


d† à    .text 0 , ¤  P`.data @ PÀ.bss € PÀ.rdata  \ @ P@.xdata l @ 0@.pdata x   @ 0@/4 â€Å¾ @ P@UH‰Ã¥Hē è HÂ� è ¸ HĀ ]ÃÂ�Â�Â�Â�Â�Â�Â�Â�Â�Â�Â�Â�Hello World! 2P $ GCC: (GNU) 6.4.0               .file þÿ gTest.c main   .text  $  .data   .bss   .rdata   .xdata   .pdata       __main  printf   .rdata$zzz .rdata$zzz


Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.