Urgent.News

What's breaking now, across thousands of outlets.

Tech

GCC Flags Notes

My notes on GNU C Compiler (GCC) Flags Basic Flags -c It tells GCC to compile the source file into an object file (.o), but do not link it into an executable. gcc -c main.c This produces: main.o Then you can link it separately: gcc main.o -o main -o It specifies the output filename. gcc main.c -o main We get: main -l It means "link this library". For example: gcc main.c -lm -o main Here: -l ->…

GNU C Compiler (GCC) flags allow users to control the compilation process. The -c flag compiles source code into an object file without linking it into an executable. The resulting object file has a .o extension. The output filename is specified using the -o flag. For example, gcc main.c -o main produces an executable named main.

Linking a library is done using the -l flag followed by the library name. For instance, gcc main.c -lm -o main links the math library (libm) and produces an executable named main. The library must be present in the standard search paths of GCC.

The -I flag specifies an additional directory to search for header files. For example, gcc -Iinclude main.c -o main searches for header files in the include/ directory. The -L flag specifies an additional directory to search for libraries during linking. For example, gcc main.c -L ./lib -lmylib -o main searches for libraries in ./lib and links libmylib to produce an executable named main.

The -static flag tells the linker to create a statically linked executable. This means the executable includes copies of all required libraries, rather than relying on external libraries. The -static flag allows the executable to run without needing to locate libraries in system directories.

Optimization flags are used to improve the performance of the compiled code. The -O flag sets the level of optimization. For example, gcc -O2 main.c -o main sets the optimization level to 2, which produces smaller and faster binaries. The optimization levels range from 0 (does nothing) to 3 (produces fast binaries, may not be small in size). The -O1 flag is suitable for fast compile times, while -O2 is recommended for production environments.

Additional optimization-related flags include -O0 (does nothing), -O1 (basic optimization), -O2 (smaller binaries), and -O3 (faster binaries, may not be small). The -O flag can also be combined with other optimization-related flags, such as -flto (link-time optimization).

The -std flag specifies the C standard the compiler should follow. For example, gcc -std=c17 main.c -o main compiles code according to the C17 standard. Some common C standards include C89, C99, C11, and C17.

Several warning-related flags are available to help identify potential issues in the code. The -Wall flag enables all warning messages. The -Wextra flag adds additional warnings not included in -Wall. The -Wpedantic flag warns about nonstandard C constructs, while -Werror treats warnings as errors, causing the compilation to fail if any warnings are issued.

Debugging-related flags help generate debugging information in the executable or object file. The -g flag generates debugging information, allowing debuggers like GDB to provide more detailed debugging information. This is particularly useful for identifying issues during development.

Lastly, the -E flag runs only the preprocessor, without compiling the code. This can be helpful for examining preprocessed code or identifying issues with header files. The -D flag defines a macro from the command line. For example, gcc -DTEST main.c expands the macro TEST during preprocessing.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

The malware that shows scanners a clean page

A cloaked link serves a clean page to scanners and malware to real visitors. Every reputation engine that checked one recent case rated it safe for months, because each engine looked from a single…

More from Friday 11 September →