gcc -o writes the build output to an output file.
gcc -O sets the compiler's optimization level.
Write the build output to an output file.
$ gcc [options] [source files] [object files] -o output file
myfile.c:
// myfile.c
#include <stdio.h>
void main()
{
printf("Program run\n");
}
Build myfile.c on terminal and run the output file myfile:
$ gcc myfile.c -o myfile
$ ./myfile
Program run
$
Set the compiler's optimization level.
option | optimization level | execution time | code size | memory usage | compile time |
---|---|---|---|---|---|
-O0 | optimization for compilation time (default) | + | + | - | - |
-O1 or -O | optimization for code size and execution time | - | - | + | + |
-O2 | optimization more for code size and execution time | -- | + | ++ | |
-O3 | optimization more for code size and execution time | --- | + | +++ | |
-Os | optimization for code size | -- | ++ | ||
-Ofast | O3 with fast none accurate math calculations | --- | + | +++ |
+increase ++increase more +++increase even more -reduce --reduce more ---reduce even more
$ gcc -Olevel [options] [source files] [object files] [-o output file]
myfile.c:
// myfile.c
#include <stdio.h>
void main()
{
printf("Program run\n");
}
Build myfile.c on terminal and run the output file myfile:
$ gcc -O myfile.c -o myfile
$ ./myfile
Program run
$