Zum Inhalt

GCC - GNU Compiler Collection

generieren

Die GNU Compiler Collection (GCC) steht als eines der einflussreichsten und weit verbreiteten Compilersysteme in der Geschichte des Computings. Ursprünglich von Richard Stallman 1987 als GNU C Compiler geschaffen, hat sich GCC zu einer umfassenden Sammlung von Compilern entwickelt, die mehrere Programmiersprachen wie C, C++, Ziel-C, Fortran, Ada, Go und D unterstützen. Als offizieller Compiler für GNU- und Linux-Systeme hat GCC eine zentrale Rolle bei der Entwicklung von Frei- und Open-Source-Software gespielt, die die Grundlage für unzählige Anwendungen, Betriebssysteme und eingebettete Systeme weltweit bietet. Die robusten Optimierungsfunktionen, die umfangreiche Plattformunterstützung und die Einhaltung von Sprachstandards haben es zum Compiler der Wahl für alles von kleinen Embedded-Projekten bis hin zu großformatigen Unternehmensanwendungen und leistungsstarken Rechensystemen gemacht.

Installation und Inbetriebnahme

GCC auf verschiedenen Plattformen installieren

```bash

Ubuntu/Debian installation

sudo apt update sudo apt install gcc g++ build-essential

Install specific version

sudo apt install gcc-11 g++-11

Install development tools

sudo apt install libc6-dev linux-libc-dev

CentOS/RHEL/Fedora installation

sudo dnf groupinstall "Development Tools" sudo dnf install gcc gcc-c++

Or using yum (older systems)

sudo yum groupinstall "Development Tools" sudo yum install gcc gcc-c++

macOS installation (via Xcode Command Line Tools)

xcode-select --install

Or using Homebrew

brew install gcc

Windows installation (MinGW-w64)

Download from https://www.mingw-w64.org/

Or using MSYS2

pacman -S mingw-w64-x86_64-gcc

Verify installation

gcc --version g++ --version

Check supported languages

gcc --help=target

List all installed GCC versions

ls /usr/bin/gcc*

Set default version (Ubuntu/Debian)

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 60 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 60 ```_

GCC Konfiguration und Umwelt

```bash

Environment variables

export CC=gcc export CXX=g++ export CFLAGS="-O2 -Wall" export CXXFLAGS="-O2 -Wall -std=c++17"

Cross-compilation setup

export CC=arm-linux-gnueabihf-gcc export CXX=arm-linux-gnueabihf-g++

Library paths

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH

Include paths

export C_INCLUDE_PATH=/usr/local/include:$C_INCLUDE_PATH export CPLUS_INCLUDE_PATH=/usr/local/include:$CPLUS_INCLUDE_PATH

Check GCC configuration

gcc -v gcc -dumpmachine gcc -dumpversion

Show default include paths

gcc -E -v - </dev/null g++ -E -v - </dev/null

Show default library paths

gcc -print-search-dirs

Show built-in defines

gcc -dM -E - </dev/null

Show supported targets

gcc --print-targets

Show supported multilib

gcc -print-multi-lib ```_

Grundlegende Erstellung

Einfache Compilation Commands

```bash

Compile C program

gcc hello.c -o hello gcc -o hello hello.c

Compile C++ program

g++ hello.cpp -o hello g++ -o hello hello.cpp

Compile without linking (object file only)

gcc -c source.c g++ -c source.cpp

Compile multiple source files

gcc file1.c file2.c file3.c -o program g++ file1.cpp file2.cpp file3.cpp -o program

Link object files

gcc file1.o file2.o file3.o -o program

Compile and run in one line

gcc hello.c -o hello && ./hello

Compile with specific C standard

gcc -std=c99 program.c -o program gcc -std=c11 program.c -o program gcc -std=c17 program.c -o program

Compile with specific C++ standard

g++ -std=c++11 program.cpp -o program g++ -std=c++14 program.cpp -o program g++ -std=c++17 program.cpp -o program g++ -std=c++20 program.cpp -o program

Compile for different architectures

gcc -m32 program.c -o program32 # 32-bit gcc -m64 program.c -o program64 # 64-bit

Generate assembly output

gcc -S source.c # Creates source.s gcc -S -o output.s source.c

Generate preprocessed output

gcc -E source.c # Output to stdout gcc -E source.c -o source.i # Save to file

Verbose compilation

gcc -v source.c -o program

Show compilation commands without executing

gcc -### source.c -o program ```_

Compilation Stages

```bash

Four stages of compilation: preprocessing, compilation, assembly, linking

Stage 1: Preprocessing (cpp)

gcc -E source.c -o source.i cpp source.c source.i

Stage 2: Compilation (cc1)

gcc -S source.i -o source.s gcc -S source.c -o source.s

Stage 3: Assembly (as)

gcc -c source.s -o source.o as source.s -o source.o

Stage 4: Linking (ld)

gcc source.o -o program ld source.o -o program

Stop after specific stage

gcc -E source.c # Stop after preprocessing gcc -S source.c # Stop after compilation gcc -c source.c # Stop after assembly

Save intermediate files

gcc -save-temps source.c -o program

Use specific tools

gcc -B/path/to/tools source.c # Use tools from specific directory ```_

Optimierungsoptionen

Optimierungsstufen

```bash

No optimization (default, fastest compilation)

gcc -O0 source.c -o program

Basic optimization (good balance)

gcc -O1 source.c -o program

Standard optimization (recommended for most cases)

gcc -O2 source.c -o program

Aggressive optimization (may increase compilation time)

gcc -O3 source.c -o program

Optimize for size

gcc -Os source.c -o program

Optimize for fast compilation

gcc -Ofast source.c -o program

Optimize for debugging

gcc -Og source.c -o program

Profile-guided optimization (two-step process)

Step 1: Compile with profiling

gcc -O2 -fprofile-generate source.c -o program ./program # Run with representative input

Step 2: Compile with profile data

gcc -O2 -fprofile-use source.c -o program_optimized

Link-time optimization

gcc -O2 -flto source1.c source2.c -o program

Specific optimization flags

gcc -O2 -funroll-loops source.c -o program gcc -O2 -finline-functions source.c -o program gcc -O2 -fomit-frame-pointer source.c -o program ```_

Erweiterte Optimierung

```bash

Vectorization

gcc -O2 -ftree-vectorize source.c -o program gcc -O3 -march=native source.c -o program

Loop optimizations

gcc -O2 -funroll-loops source.c -o program gcc -O2 -funroll-all-loops source.c -o program gcc -O2 -fpeel-loops source.c -o program

Function inlining

gcc -O2 -finline-functions source.c -o program gcc -O2 -finline-small-functions source.c -o program gcc -O2 -finline-functions-called-once source.c -o program

Architecture-specific optimization

gcc -O2 -march=native source.c -o program gcc -O2 -march=skylake source.c -o program gcc -O2 -mtune=generic source.c -o program

Show optimization report

gcc -O2 -fopt-info source.c -o program gcc -O2 -fopt-info-vec source.c -o program gcc -O2 -fopt-info-loop source.c -o program

Disable specific optimizations

gcc -O2 -fno-inline source.c -o program gcc -O2 -fno-unroll-loops source.c -o program

Optimization for specific use cases

gcc -O2 -ffast-math source.c -o program # Fast math (may reduce precision) gcc -O2 -fno-math-errno source.c -o program # Don't set errno for math functions gcc -O2 -funsafe-math-optimizations source.c -o program ```_

Warn- und Fehleroptionen

Warnstufen

```bash

Basic warnings

gcc -Wall source.c -o program

Extra warnings

gcc -Wextra source.c -o program

All warnings

gcc -Wall -Wextra source.c -o program

Treat warnings as errors

gcc -Werror source.c -o program

Specific warning categories

gcc -Wpedantic source.c -o program # Strict ISO C compliance gcc -Wconversion source.c -o program # Type conversion warnings gcc -Wshadow source.c -o program # Variable shadowing gcc -Wunused source.c -o program # Unused variables/functions gcc -Wuninitialized source.c -o program # Uninitialized variables gcc -Wformat source.c -o program # Format string checking gcc -Wcast-align source.c -o program # Cast alignment issues gcc -Wwrite-strings source.c -o program # String literal modifications

Disable specific warnings

gcc -Wall -Wno-unused-variable source.c -o program gcc -Wall -Wno-format source.c -o program

C++-specific warnings

g++ -Wall -Weffc++ source.cpp -o program # Effective C++ guidelines g++ -Wall -Wold-style-cast source.cpp -o program g++ -Wall -Woverloaded-virtual source.cpp -o program

Enable additional useful warnings

gcc -Wall -Wextra -Wpedantic -Wformat=2 -Wconversion \ -Wshadow -Wunused -Wuninitialized -Wcast-align \ -Wwrite-strings source.c -o program

Show all available warnings

gcc --help=warnings ```_

Fehlerbehebung

```bash

Stop on first error

gcc -Wfatal-errors source.c -o program

Maximum number of errors

gcc -fmax-errors=5 source.c -o program

Colorized output

gcc -fdiagnostics-color=always source.c -o program gcc -fdiagnostics-color=auto source.c -o program

Show column numbers in diagnostics

gcc -fdiagnostics-show-location=every-line source.c -o program

Show option that triggered warning

gcc -fdiagnostics-show-option source.c -o program

Template backtrace limit (C++)

g++ -ftemplate-backtrace-limit=0 source.cpp -o program

Syntax-only check (no compilation)

gcc -fsyntax-only source.c

Check for undefined behavior

gcc -fsanitize=undefined source.c -o program ```_

Debugging und Profiling

Debug Information

```bash

Generate debug information

gcc -g source.c -o program

Different debug levels

gcc -g0 source.c -o program # No debug info gcc -g1 source.c -o program # Minimal debug info gcc -g2 source.c -o program # Default debug info gcc -g3 source.c -o program # Maximum debug info

Debug format options

gcc -gdwarf-4 source.c -o program # DWARF version 4 gcc -gstabs source.c -o program # Stabs format

Debug with optimization

gcc -O2 -g source.c -o program

Split debug information

gcc -g -gsplit-dwarf source.c -o program

Compressed debug sections

gcc -g -gz source.c -o program

Debug macros

gcc -g3 -gdwarf-4 source.c -o program

Address sanitizer

gcc -g -fsanitize=address source.c -o program

Memory sanitizer

gcc -g -fsanitize=memory source.c -o program

Thread sanitizer

gcc -g -fsanitize=thread source.c -o program

Undefined behavior sanitizer

gcc -g -fsanitize=undefined source.c -o program

Leak sanitizer

gcc -g -fsanitize=leak source.c -o program ```_

Unterstützung bei der Profilierung

```bash

Generate profiling information

gcc -pg source.c -o program ./program gprof program gmon.out > analysis.txt

Coverage testing

gcc --coverage source.c -o program ./program gcov source.c lcov --capture --directory . --output-file coverage.info genhtml coverage.info --output-directory coverage_html

Profile-guided optimization

gcc -fprofile-generate source.c -o program ./program # Run with representative data gcc -fprofile-use source.c -o program_optimized

Time profiling

gcc -ftime-report source.c -o program

Memory usage profiling

gcc -fmem-report source.c -o program

Stack usage analysis

gcc -fstack-usage source.c -o program ```_

Bibliotheken und Linking

Statische und dynamische Verknüpfung

```bash

Link with standard libraries

gcc source.c -lm -o program # Math library gcc source.c -lpthread -o program # POSIX threads gcc source.c -ldl -o program # Dynamic loading

Link with custom libraries

gcc source.c -L/path/to/lib -lmylib -o program

Static linking

gcc -static source.c -o program

Create static library

ar rcs libmylib.a file1.o file2.o file3.o

Create shared library

gcc -shared -fPIC file1.c file2.c -o libmylib.so

Link with shared library

gcc source.c -L. -lmylib -o program

Set runtime library path

gcc source.c -L/path/to/lib -lmylib -Wl,-rpath,/path/to/lib -o program

Show library dependencies

ldd program

Link order matters

gcc source.c -lmylib -lm -o program

Whole archive linking

gcc -Wl,--whole-archive -lmylib -Wl,--no-whole-archive source.c -o program

Version script for shared libraries

gcc -shared -Wl,--version-script=version.map file1.c -o libmylib.so ```_

Pfade und Preprozessor einschließen

```bash

Add include directories

gcc -I/path/to/headers source.c -o program gcc -I. -I../include source.c -o program

System include directories

gcc -isystem /path/to/system/headers source.c -o program

Define preprocessor macros

gcc -DDEBUG source.c -o program gcc -DDEBUG=1 source.c -o program gcc -DVERSION=\"1.0\" source.c -o program

Undefine macros

gcc -UDEBUG source.c -o program

Include file directly

gcc -include config.h source.c -o program

Show include search paths

gcc -E -v - </dev/null

Dependency generation

gcc -M source.c # Generate dependencies gcc -MM source.c # Generate dependencies (no system headers) gcc -MD source.c -o program # Compile and generate dependencies gcc -MMD source.c -o program # Compile and generate dependencies (no system headers) ```_

Cross-Compiling

Zielarchitektur

```bash

Install cross-compilation toolchain

sudo apt install gcc-arm-linux-gnueabihf sudo apt install gcc-aarch64-linux-gnu sudo apt install gcc-mingw-w64

Cross-compile for ARM

arm-linux-gnueabihf-gcc source.c -o program_arm

Cross-compile for ARM64

aarch64-linux-gnu-gcc source.c -o program_arm64

Cross-compile for Windows

x86_64-w64-mingw32-gcc source.c -o program.exe

Specify target explicitly

gcc -target arm-linux-gnueabihf source.c -o program_arm

Set sysroot for cross-compilation

gcc --sysroot=/path/to/target/sysroot source.c -o program

Cross-compilation environment variables

export CC=arm-linux-gnueabihf-gcc export CXX=arm-linux-gnueabihf-g++ export AR=arm-linux-gnueabihf-ar export STRIP=arm-linux-gnueabihf-strip

Configure for autotools

./configure --host=arm-linux-gnueabihf --prefix=/usr/local/arm

CMake cross-compilation

cmake -DCMAKE_TOOLCHAIN_FILE=arm-toolchain.cmake .. ```_

Embedded Systems

```bash

Compile for bare metal (no OS)

arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb source.c -o program.elf

Specify memory layout

arm-none-eabi-gcc -T linker_script.ld source.c -o program.elf

Generate binary file

arm-none-eabi-objcopy -O binary program.elf program.bin

Generate hex file

arm-none-eabi-objcopy -O ihex program.elf program.hex

Size information

arm-none-eabi-size program.elf

Disassemble

arm-none-eabi-objdump -d program.elf

Common embedded flags

arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=hard \ -mfpu=fpv4-sp-d16 -Os -ffunction-sections -fdata-sections \ -Wl,--gc-sections source.c -o program.elf ```_

Erweiterte Funktionen

Sprache-spezifische Optionen

```bash

C-specific options

gcc -std=c99 -pedantic source.c -o program gcc -std=gnu99 source.c -o program

C++-specific options

g++ -std=c++17 -pedantic source.cpp -o program g++ -std=gnu++17 source.cpp -o program

Objective-C

gcc -x objective-c source.m -o program

Mixed language compilation

gcc source.c module.cpp -lstdc++ -o program

Enable specific language features

g++ -fconcepts source.cpp -o program # C++20 concepts g++ -fcoroutines source.cpp -o program # C++20 coroutines g++ -fmodules-ts source.cpp -o program # C++20 modules

Disable language features

g++ -fno-exceptions source.cpp -o program g++ -fno-rtti source.cpp -o program ```_

Sicherheitsmerkmale

```bash

Stack protection

gcc -fstack-protector source.c -o program gcc -fstack-protector-strong source.c -o program gcc -fstack-protector-all source.c -o program

Position independent code

gcc -fPIC source.c -o program gcc -fPIE source.c -o program

Fortify source

gcc -D_FORTIFY_SOURCE=2 -O2 source.c -o program

Control flow integrity

gcc -fcf-protection source.c -o program

Return address protection

gcc -mretpoline source.c -o program

Address space layout randomization

gcc -fPIE -pie source.c -o program

Relocation read-only

gcc -Wl,-z,relro,-z,now source.c -o program

No execute stack

gcc -Wl,-z,noexecstack source.c -o program ```_

Leistungsanalyse

```bash

Generate assembly with C code comments

gcc -S -fverbose-asm source.c

Show optimization decisions

gcc -O2 -fopt-info-vec-optimized source.c -o program gcc -O2 -fopt-info-loop-optimized source.c -o program

Benchmark compilation time

time gcc -O2 source.c -o program

Memory usage during compilation

/usr/bin/time -v gcc -O2 source.c -o program

Parallel compilation

make -j$(nproc)

Use ccache for faster recompilation

ccache gcc source.c -o program

Profile compilation

gcc -ftime-report -fmem-report source.c -o program ```_

GCCs umfangreiches Feature-Set, reife Optimierungsfähigkeiten und breite Plattform-Unterstützung machen es zu einem unverzichtbaren Werkzeug für die Softwareentwicklung in verschiedenen Domänen. Von einfachen Kommandozeilendiensten bis hin zu komplexen Embedded-Systemen und Hochleistungs-Computing-Anwendungen bietet GCC die Zuverlässigkeit, Leistung und Flexibilität, die benötigt wird, um robuste Softwarelösungen aufzubauen. Sein Engagement für offene Standards, aktive Entwicklungsgemeinschaft und umfassende Dokumentation sorgen dafür, dass GCC an der Spitze der Compiler-Technologie bleibt und sich weiterhin mit den sich ändernden Anforderungen der Software-Entwicklungslandschaft weiterentwickelt.