The gcc command is used to compile C and C++ programs using the GNU Compiler Collection. It is the standard compiler on most Unix-based systems, including Linux.
gcc [options] source_file -o output_file
gcc hello.c -o hello./hellogcc -Wall file.cgcc -g program.c -o programgcc file1.c file2.c -o app-o: Specify output file name-Wall: Show all warnings-g: Include debugging information-O: Optimization level (e.g., -O2 for more optimization)-c: Compile only, do not link$ gcc -Wall test.c -o test
$ ./test
-Wall to catch potential issues early.-g if you plan to debug with tools like gdb.-O2 or -O3 for optimized release builds.The gcc command is a part of the GNU Compiler Collection and is used to compile C and C++ programs in Linux. It's one of the most powerful and commonly used tools by developers for building software on Unix-based systems. Whether you’re writing simple programs or complex applications, understanding how to use gcc effectively is essential.
gcc [options] file...
The basic usage compiles a C program and outputs an executable file named a.out by default.
gcc hello.c
This compiles hello.c into an executable named a.out.
./a.out
Runs the output program.
gcc hello.c -o hello
Generates an output file named hello instead of the default a.out.
gcc main.c module1.c module2.c -o myprogram
This links all specified source files into a single executable.
-o filename: Specify output filename-Wall: Enable all standard warnings-g: Include debugging information-O2: Optimize code (level 2)-c: Compile source files into object files (.o)-std=c99: Use a specific C standard (like C99, C11)gcc -g main.c -o main
This includes debugging symbols in the executable for use with gdb (GNU Debugger).
gcc -c file1.c
Generates file1.o (object file) without linking. This is useful for multi-file projects.
gcc file1.o file2.o -o output
Links multiple object files into a final executable.
gcc -Wall -Wextra -pedantic -std=c11 main.c -o app
This combination helps catch many common mistakes during compilation.
-O0: No optimization (default, useful for debugging)-O1: Basic optimizations-O2: Recommended level for general use-O3: Aggressive optimizations-Os: Optimize for sizegcc can be used for cross-compilation with the help of toolchains. For example:
arm-linux-gnueabi-gcc hello.c -o hello
This generates binaries for ARM architecture.
gcc primarily supports C, C++, Objective-C, Fortran, Ada, and Go (with appropriate frontends).
gcc --version
In the current directory, unless you specify a path with the -o option.
Yes, but it's better to use g++ which automatically links the C++ standard library.
a.out if no -o is specified.
The gcc command is a critical tool for Linux and open-source development. Its flexibility, powerful options, and wide language support make it indispensable for programmers. Mastering gcc can significantly enhance your coding efficiency and application performance.
The gcc command is a part of the GNU Compiler Collection and is used to compile C and C++ programs in Linux. It's one of the most powerful and commonly used tools by developers for building software on Unix-based systems. Whether you’re writing simple programs or complex applications, understanding how to use gcc effectively is essential.
gcc [options] file...
The basic usage compiles a C program and outputs an executable file named a.out by default.
gcc hello.c
This compiles hello.c into an executable named a.out.
./a.out
Runs the output program.
gcc hello.c -o hello
Generates an output file named hello instead of the default a.out.
gcc main.c module1.c module2.c -o myprogram
This links all specified source files into a single executable.
-o filename: Specify output filename-Wall: Enable all standard warnings-g: Include debugging information-O2: Optimize code (level 2)-c: Compile source files into object files (.o)-std=c99: Use a specific C standard (like C99, C11)gcc -g main.c -o main
This includes debugging symbols in the executable for use with gdb (GNU Debugger).
gcc -c file1.c
Generates file1.o (object file) without linking. This is useful for multi-file projects.
gcc file1.o file2.o -o output
Links multiple object files into a final executable.
gcc -Wall -Wextra -pedantic -std=c11 main.c -o app
This combination helps catch many common mistakes during compilation.
-O0: No optimization (default, useful for debugging)-O1: Basic optimizations-O2: Recommended level for general use-O3: Aggressive optimizations-Os: Optimize for sizegcc can be used for cross-compilation with the help of toolchains. For example:
arm-linux-gnueabi-gcc hello.c -o hello
This generates binaries for ARM architecture.
gcc also processes C preprocessor directives like #include, #define, #ifdef, etc. To see how the code is preprocessed, use:
gcc -E main.c
gcc can generate assembly code using:
gcc -S main.c
This creates a main.s file containing the assembly instructions.
gcc -E source.c -o output.igcc -S source.c -o output.sgcc -c output.s -o output.ogcc output.o -o finalgcc is often used in build systems with make. Example Makefile:
CC=gcc
CFLAGS=-Wall -g
all: program
program: main.o utils.o
$(CC) $(CFLAGS) -o program main.o utils.o
main.o: main.c
$(CC) $(CFLAGS) -c main.c
utils.o: utils.c
$(CC) $(CFLAGS) -c utils.c
clean:
rm -f *.o program
Use -l to link libraries and -L to specify library paths:
gcc program.c -L/usr/local/lib -lmylib -o program
PATH: Ensure gcc binary is in your pathLD_LIBRARY_PATH: Used for runtime linkingCFLAGS: Set default compile-time flags-fstack-protector-strong: Adds buffer overflow protection-D_FORTIFY_SOURCE=2: Enable compile-time buffer overflow detection-pie -fPIE: Create position-independent executables-Wformat -Werror=format-security: Warn and error out on insecure format stringsUse: gcc -fsanitize=address -g your_program.c -o your_program
Use: gcc -static your_program.c -o your_program
Use: gcc -fPIC -c lib.c and gcc -shared -o libmylib.so lib.o
Yes, but it is recommended to use g++ for automatic linking with the C++ standard library.
Both are compilers. clang is newer with faster compile times and more helpful diagnostics, but gcc is more mature with broader platform support.
The gcc command is an advanced and highly customizable compiler for developers working in C/C++. With features like optimization, cross-compilation, debugging support, and security features, it's no surprise that gcc is the backbone of Linux software development. Whether you're just starting out or building enterprise-level applications, mastering gcc is crucial to efficient and secure coding on Linux.