Recommended compiler and linker flags for GCC
Compiler flags:
gcc -Wall -Wextra -Wpedantic -std=c11 -O2 -march=native -c -o output.o input.c
Linker flags:
gcc -o output output.o -lm
Explanation:
1. -Wall
: Enables most warning messages to help identify potential issues in the code.
2. -Wextra
: Enables additional warning messages beyond what is enabled by -Wall
.
3. -Wpedantic
: Causes the compiler to strictly adhere to the C standard and issue warnings for non-standard code.
4. -std=c11
: Specifies the C programming language standard to be used (C11 in this case).
5. -O2
: Enables level 2 optimization to improve the performance of the compiled code.
6. -march=native
: Generates code optimized for the host machine's architecture.
7. -c
: Compiles the source code into an object file without linking.
8. -o output.o
: Specifies the name of the output object file.
9. -lm
: Links the math library (libm
) which is required for mathematical functions.
Linker flags:
1. -o output
: Specifies the name of the output executable.
2. output.o
: Specifies the input object file to be linked.
3. -lm
: Links the math library (libm
) again to resolve any unresolved symbols related to mathematical functions.