GCC (LINUX)

COMPILATION SYNTAX

GENERAL SYNTAX: DEBUGGING STAGE

root@dev:~$ gcc -g -c {-Wall | -Werror} {-m32 | -m64} {-O#} sourceFileName.c -o destinationFileName.out

 * the -g adds debugging information to the compiled output.
    - this enabled devs or sre to use a debugger like gdb to step through the
      code line-by-line
 * the -c compiles only and doesn't link.
    - it creates an object file (programName.o), NOT an executable yet.
    
 * -O1 turns on compiler optimization
     - use -O2 or -O3 for more aggressive optimization
        - capital O not zero

GENERAL SYNTAX: PRODUCTION STAGE

root@dev:~$ gcc -c {-Wall | -Werror} {-m32 | -m64} {-O#} sourceFileName.c -o destinationFileName.out

 * do not add debugging symbols on production executables
 
 * the -c compiles only and doesn't link.
    - it creates an object file (programName.o), NOT an executable yet.
    
 * -O1 turns on compiler optimization
     - use -O2 or -O3 for more aggressive optimization
        - capital O not zero

GENERAL SYNTAX: COMBINING MULTIPLE SOURCE FILES

root@dev:~$ gcc -c {-Wall | -Werror} {-m32 | -m64} {-O#} *.c -o destinationFileName.out

 * do not add debugging symbols on production executables
 
 * the -c compiles only and doesn't link.
    - it creates an object file (programName.o), NOT an executable yet.
    
 * -O1 turns on compiler optimization
     - use -O2 or -O3 for more aggressive optimization
        - capital O not zero

x32

#OPTIMIZATION TURNED ON
root@dev:~$ gcc -g -m32 -O1 sourceFile.c outputFile.out
 * -g is used for debug symbols
    - displays friendly function names and may correlate the source code with assembly code when disassembling
 * -m32 compiles the source into a x32 bit program
    - can be used as a training aid when dealing with smaller registers
 * -O1 turns on compiler optimization
    - capital O not zero
 * -O0 turns off compiler optimization
 
 
 #OPTIMIZATION TURNED OFF
 root@dev:~$ gcc -g -m32 -O0 sourceFile.c outputFile.out
 
 
 #
 root@dev:~$ sudo apt install gcc-multilib -y
  * this install the x32 libraries required to compile x32 bit programs

x64

#OPTIMIZATION TURNED ON
root@dev:~$ gcc -g -m64 -O1 sourceFile.c -o outputFile.out
 * -g is used for debug symbols
    - displays friendly function names and may correlate the source code with assembly code when disassembling
 * -m32 compiles the source into a x32 bit program
    - can be used as a training aid when dealing with smaller registers
 * -O1 turns on compiler optimization
     - use -O2 or -O3 for more aggressive optimization
        - capital O not zero
 * -O0 turns off compiler optimization
 
 
 #OPTIMIZATION TURNED OFF
 root@dev:~$ gcc -g -m64 -O0 sourceFile.c -o outputFile.out
 
 
 #
 root@dev:~$ sudo apt install gcc-multilib -y
  * this install the x32 libraries required to compile x32 bit programs

FLAGS/OPTIONS

BUILD W/ DEBUGGING SYMBOLS

root@dev:~$ gcc -Wall -g -c programName.c -o objectFileOnly.o
root@dev:~$ ls
 programName.c  programName.o

 * the -g adds debugging information to the compiled output.
    - this enabled devs or sre to use a debugger like gdb to step through the
      code line-by-line
 * the -c compiles only and doesn't link.
    - it creates an object file (programName.o), NOT an executable yet.

DISPLAY HELP

root@dev:~$ gcc --help

DISPLAY COMMON WARNINGS AND ERROS

root@dev:~$ gcc -Wall source.c -o output

 * this will display important warnings such as uninitialized variables, bad casts, etc.
    - it is a good habit to always use -Wall
       - the -Wall flag is actually two sets of options
          - -W (warnings)
          - all (all warnings)
          
root@dev:~$ gcc -Wall -Werror source.c -o output

 * the -Werror causes gcc to treat all warnings as errors
    - this option will result in NO C program successfully building when any warnings
      are present

PRODUCE OBJECT CODE ONLY (DISABLE LINKER)

GCC can compile and link in one step, but will not produce a separate object file. This method is only used if a copy of an object file is needed for backup purposes

root@dev:~$ gcc -Wall -g -c programName.c
root@dev:~$ ls
 programName.c  programName.o

 * the -g adds debugging information to the compiled output.
    - this enabled devs or sre to use a debugger like gdb to step through the
      code line-by-line
       - do not enable debbuging symbols on production!
       - only use it prior to production
 * the -c compiles only and doesn't link.
    - it creates an object file (programName.o), NOT an executable yet.
#linking after producing an object file to create an executable
root@dev:~$ gcc -Wall objectFileName.o -o programName
root@dev:~$ ls
 programName  objectFileName.o

SAVE PREPROCESSOR OUTPUT

#gcc -E programName.c -o programName.i
 * the -E option allows the programmer to save the outputs of the PREPROCESSOR!

root@dev:~$ nano programName.c 
#include <stdio.h>
int main(void)
{
  printf("Hello World\n");
  return 0;
}

root@dev:~$ gcc -E programName.c -o programName.i
root@dev:~$ ls
 programName.c programName.i
root@dev:~$ cat programName.i
 ## 1 "tiny_program.c"
 ## 1 "<built-in>"
 ## 1 "<command-line>"
 ## 31 "<command-line>"
 ## 1 "/usr/include/stdc-predef.h" 1 3 4
 ## 32 "<command-line>" 2
 ## 1 "tiny_program.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
 ## 424 "/usr/include/features.h" 3 4
 ## 1 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 1 3 4
 ## 427 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 3 4
 ## 1 "/usr/include/x86_64-linux-gnu/bits/wordsize.h" 1 3 4
 ## 428 "/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
 ## 429 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 2 3 4
 ## 425 "/usr/include/features.h" 2 3 4
 ## 448 "/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
 ## 449 "/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
 **** About 700 lines ****

extern void funlockfile (FILE*__stream) __attribute__((__nothrow__,__leaf__));
## 868 "/usr/include/stdio.h" 3 4
## 2 "tiny_program.c" 2
## 3 "tiny_program.c"
int main(void)
{
  printf("Hello World\n");
  return 0;
}

 * ONLY the last four lines are C source code; the rest are code included in the
   program by the preprocessor
    - all of these code are passed to the gcc compiler!

Last updated