SyntaxStudy
Sign Up
C Compiling C with GCC
C Beginner 1 min read

Compiling C with GCC

The GNU Compiler Collection (GCC) is the most widely used C compiler on Linux and macOS. Compiling a C program turns your human-readable source file into a binary executable the CPU can run. The basic command is `gcc source.c -o output`, where `-o` specifies the output filename. Without `-o`, GCC produces a file named `a.out` on Unix-like systems. GCC accepts many flags that control how compilation works. The `-Wall` flag enables most common warnings, and `-Wextra` enables additional ones. The `-std=c11` flag tells GCC to compile against the C11 standard. Using `-g` includes debug symbols so you can step through code with GDB. During development you should always compile with at least `-Wall -Wextra -std=c11`. Compilation actually happens in several stages: preprocessing, compilation, assembly, and linking. You can see each stage separately with flags like `-E` (preprocess only) or `-S` (compile to assembly). Understanding the pipeline helps you diagnose errors that originate in headers, macros, or missing libraries rather than in your own source file.
Example
/*
 * Demonstrating common GCC compilation flags.
 *
 * Compile (basic):
 *   gcc greet.c -o greet
 *
 * Compile (recommended for development):
 *   gcc -Wall -Wextra -std=c11 -g greet.c -o greet
 *
 * Preprocess only (see macro expansion):
 *   gcc -E greet.c
 *
 * Compile to assembly:
 *   gcc -S greet.c          # produces greet.s
 */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    if (argc < 2) {
        fprintf(stderr, "Usage: %s <name>\n", argv[0]);
        return EXIT_FAILURE;   /* EXIT_FAILURE == 1 on most systems */
    }

    printf("Hello, %s!\n", argv[1]);
    return EXIT_SUCCESS;       /* EXIT_SUCCESS == 0 */
}