Obviously this is about optimizations that the compiler can manage, if your code smells, then it is up to you to fix it.
-O-O3: This is the highest level of optimization in GCC, which aggressively optimizes the code for speed and size.-flto: LTO is a technique that allows the compiler to perform optimizations across multiple compilation units, further improving the performance of the final binary.-s: Removes all debugging information from the final binary, reducing its size.-fomit-frame-pointer: Omits the frame pointer, which can provide a slight performance boost.-finline-functions: Allows the compiler to inline function calls, which can improve performance.-march=native: Tells the compiler to use the most aggressive optimizations available for the target CPU.
-O0: Disable optimization.-g: Generate debug information (DWARF format by default).-gdwarf-4: Specify the version of DWARF to be used.- Make it
-ggdbforgdboriented format. -g3: To includes preprocessor macros in the debugging information.
-fno-inline: Prevent the compiler from inlining functions.-fno-omit-frame-pointer: To ensures that the frame pointer is not omitted, which can be helpful for stack unwinding during debugging.
Use --verbose or -v to make gcc tell more details about the compilation.
Use -Wl,--verbose to make gcc passes options to the linker (typically ld), here to give --verbose option to the linker. Notice the coma and absence of space.
To output object files but not linking them:
gcc -c main.c: Would output a main.o file.
-c: Compile or assemble the source files, but do not link.
To link object files:
gcc file1.o file2.o -o my_executable using gcc or ld file1.o file2.o -o my_executable using ld.
To output assembly to stdout:
gcc -S -o - your_source_file.c
-S means to stop after the stage of compilation proper; do not assemble.
Look into --save-temps used in combination with -S to save intermediate files like assembly.
Look into -fverbose-asm to add comments for better understanding of the assembly if needed.
On systems that rely on /dev/stdout, -o - (canonical form) will behave similarly to -o /dev/stdout.
To compile from stdin:
echo 'int main() { return 0; }' | gcc -x c -
To output the assembly code:
echo 'int main() { return 0; }' | gcc -x c -S - -o -
Compile using pkg-config (Here an example for compiling a GTK3 program):
gcc $(pkg-config --cflags gtk+-3.0) -o entries entries.c $(pkg-config --libs gtk+-3.0)
--cflags and --libs commands can be conbined into one like so (Here's an example for compiling an OpenGL and GTK3 program):
gcc -o rotating_cube rotating_cube.c $(pkg-config --cflags --libs gtk+-3.0 epoxy) -lopengl32 -lglu32
To investigate further:
gcc -c -g -O0 -fno-inline main.o -S main.swas promising in the intent of getting assembly code out of an object file usinggcc. Not working on Windows, Linux do not know. Even if it's purelly for proof of concept and this is contradictory with the purpose of gcc.