Compiler GCC in C
Andres Castaneda
Software Developer | Python | Django | DRF | Flask | AWS | Git | coding enthusiast | continuous learning and professional development | incorporating new technologies and methodologies | 4 years of experience
GCC was first released March 22, 1987, available by FTP from MIT. Stallman was listed as the author but cited others for their contributions, including Jack Davidson and Christopher Fraser for the idea of using RTL as an intermediate language, Paul Rubin for writing most of the preprocessor, and Leonard Tower for "parts of the parser, RTL generator, RTL definitions, and of the Vax machine description." Described as the "first free software hit" by Peter H. Salus, the GNU compiler arrived just at the time when Sun Microsystems was unbundling its development tools from its operating system, selling them separately at a higher combined price than the previous bundle, which led many of Sun's users to buy or download GCC instead of the vendor's tools. more information here
Command and flags
- gcc [options] file... : compiler.
- -E : Preprocess only; do not compile, assemble or link.
- -S : Compile only; do not assemble or link.
- -c : Compile and assemble, but do not link.
- -o : select the name you would like, where the compiled output will be.
Steps of compiler GCC
1. Preprocessor
This command save in <file-name-sale> the output of file <file-name-c>, it's preprocess only.
gcc -E <file-name-c> -o <file-name-save>
Example:
gcc -E maintest.c -o maintest cat maintest
Result of 'cat maintest':
# 1 "maintest.c" # 1 "<built-in>" # 1 "<command-line>" # 31 "<command-line>" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 32 "<command-line>" 2 # 1 "maintest.c" # 1 "/usr/include/stdio.h" 1 3 4 # 27 "/usr/include/stdio.h" 3 4 # 1 "/usr/include/x86_64-linux-gnu/bits/libc-header-start.h" 1 3 4 # 33 "/usr/include/x86_64-linux-gnu/bits/libc-header-start.h" 3 4 # 1 "/usr/include/features.h" 1 3 4 # 461 "/usr/include/features.h" 3 4 # 1 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 1 3 4 # 452 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 3 4 # 1 "/usr/include/x86_64-linux-gnu/bits/wordsize.h" 1 3 4 # 453 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 2 3 4 # 1 "/usr/include/x86_64-linux-gnu/bits/long-double.h" 1 3 4 # 454 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 2 3 4 # 462 "/usr/include/features.h" 2 3 4 # 485 "/usr/include/features.h" 3 4 # 1 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 1 3 4 # 10 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 3 4 # 1 "/usr/include/x86_64-linux-gnu/gnu/stubs-64.h" 1 3 4 # 11 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 2 3 4 # 486 "/usr/include/features.h" 2 3 4 # 34 "/usr/include/x86_64-linux-gnu/bits/libc-header-start.h" 2 3 4 # 28 "/usr/include/stdio.h" 2 3 4 #### MORE CODE MORE CODE MORE CODE MORE CODE #### #### MORE CODE MORE CODE MORE CODE MORE CODE #### #### MORE CODE MORE CODE MORE CODE MORE CODE #### extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); # 858 "/usr/include/stdio.h" 3 4 extern int __uflow (FILE *); extern int __overflow (FILE *, int); # 873 "/usr/include/stdio.h" 3 4 # 2 "maintest.c" 2 # 8 "maintest.c" int main(void) { printf("Hello world"); return (0); }
At the beginning is everything contained in the library <stdio.h> that was included in the header; and the end, our function maintest.c
2. Compile
This command save in <file-name-sale> the output of file <file-name-c>, it's compile only.
gcc -S <file-name-c> -o <file-name-save>
Example:
gcc -S maintest.c -o maintestcompile
cat maintestcompile
Result of 'cat maintestcompile':
.file "maintest.c" .text .section .rodata .LC0: .string "Hello world" .text .globl main .type main, @function main: .LFB0: .cfi_startproc endbr64 pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 leaq .LC0(%rip), %rdi movl $0, %eax call printf@PLT movl $0, %eax popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE0: .size main, .-main .ident "GCC: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0" .section .note.GNU-stack,"",@progbits .section .note.gnu.property,"a" .align 8 .long 1f - 0f .long 4f - 1f .long 5 0: .string "GNU" 1: .align 8 .long 0xc0000002 .long 3f - 2f 2: .long 0x3 3: .align 8
4:
3. Compile and assemble
This command save in <file-name-sale> the output of file <file-name-c>, it's compile and assemble only.
gcc -c <file-name-c> -o <file-name-save>
Example:
gcc -c maintest.c -o maintestcompileassemble
cat maintestcompileassemble
Result of 'cat maintestcompileassemble':
??UH??H?=???]?Hello worldGCC: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0GNU?zRx W E?C ?? 'maintest.cmain_GLOBAL_OFFSET_TABLE_printf ???????? ???????? .symtab.strtab.shstrtab.rela.text.data.bss.rodata.comment.note.GNU-stack.note.gnu.property.rela.eh_frame @ X0 &``1` 90l+B?R?j?e@? ?8
(.?t
4. Preprocess, compile, assemble, linked and file executable
This command save in <file-name-sale> the output of file <file-name-c>, it's Preprocess, compile, assemble, linked and file executable (without flags).
gcc <file-name-c> -o <file-name-save>
Example:
gcc -c maintest.c -o maintestall
cat maintestall
Result 'cat maintestall':
@@@@??? ``?-?=?=X`?-?=?=?888 XXXDDS?td888 P?td DDQ?tdR?td?-?=?=HH/lib64/ld-linux-x86-64.so.2GNU?GNU?g?#\??sg? ????[GNU??e?m? ![ j "libc.so.6printf__cxa_finalize__libc_start_mainGLIBC_2.2.5_ITM_deregisterTMCloneTable__gmon_start___ITM_registerTMCloneTableui 3?@??????????????H?H??/H??t??H???5?/??%?/??h???????????%?/D????%u/D??1?I??^H?H?=??R/??H?=y/H?r/H9?tH?./H??t ?????H?=I/H?5B/H)?H??H???H??H?H??tH?/H????fD?????=/u+UH?=?.H??t H?=?.?????d?????.]??????w?????UH??H?=????????]????AWL?=;,AVI??AUI??ATA??UH?-,,SL)?H??_???H??t1??L??L??D??A??H??H9?u?H?[]A\A]A^A_?ff.??????H?H??Hello worldD???x0????@????P???`9????`????????8zRx ????/D$4???? FJ W ??:*3$"\????t?????a??? E?C D?h???eF?I?E ?E(?D0?H8?G@n8A0A(B BB?????@ ??????o?p? ? ???? ?????????o???o????o?=@GCC: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.08X|??? ( ? @P`? X ?=?=?=??@@? ??!7@F?=m@y?=??????\!????=??=??=? ???? ? o @3@?:Nm@z @? ?pe?@s`/?@?I ?@? ?"crtstuff.cderegister_tm_clones__do_global_dtors_auxcompleted.8060__do_global_dtors_aux_fini_array_entryframe_dummy__frame_dummy_init_array_entrymaintest.c__FRAME_END____init_array_end_DYNAMIC__init_array_start__GNU_EH_FRAME_HDR_GLOBAL_OFFSET_TABLE___libc_csu_fini_ITM_deregisterTMCloneTable_edataprintf@@GLIBC_2.2.5__libc_start_main@@GLIBC_2.2.5__data_start__gmon_start____dso_handle_IO_stdin_used__libc_csu_init__bss_startmain__TMC_END___ITM_registerTMCloneTable__cxa_finalize@@GLIBC_2.2.5.symtab.strtab.shstrtab.interp.note.gnu.property.note.gnu.build-id.note.ABI-tag.gnu.hash.dynsym.dynstr.gnu.version.gnu.version_r.rela.dyn.rela.plt.init.plt.got.plt.sec.text.fini.rodata.eh_frame_hdr.eh_frame.init_array.fini_array.dynamic.data.bss.comment#886XX$I|| W???o??a ??ip? ? D?X ??????=?-????@0?PP?``????
@00*@0. X`8
Result './maintestall' <- executable:
Hello world
'./' is for executing files.