gcc -c Option

The -c option in gcc compiles source files into object files (.o) without linking them into an executable. This enables separate compilation and is essential for large projects and library creation.

Syntax

gcc -c sourcefile.c

-c Option Details

  • Purpose: Compile source files to object files without linking
  • Output: Creates .o files (object files) instead of executables
  • Stage: Stops compilation after the assembly stage
  • Usage: Essential for modular programming and large projects

Compilation Process with -c

  1. Preprocessing: Handles #include, #define, etc.
  2. Compilation: Converts C code to assembly language
  3. Assembly: Converts assembly to machine code (object file)
  4. Linking:Skipped with -c option

Result: Object file (.o) containing machine code but no executable

Description

The -c option is crucial for separate compilation, allowing you to compile individual source files into object files that can later be linked together to create executables or libraries.

Key behaviors:

  • Creates object files with .o extension
  • Skips the linking stage entirely
  • Allows compilation of files with missing dependencies
  • Enables incremental builds in large projects
  • Required for creating static and shared libraries

Benefits of Separate Compilation

  • Faster Builds: Only recompile changed files
  • Modular Development: Work on individual components
  • Library Creation: Build reusable code libraries
  • Large Projects: Manage complex codebases efficiently
  • Parallel Compilation: Compile multiple files simultaneously

Common Examples

Command Description Output
gcc -c main.c Compile single file main.o
gcc -c *.c Compile all C files Multiple .o files
gcc -c -o myfile.o source.c Custom object name myfile.o
gcc *.o -o program Link object files program (executable)

Detailed Examples

Basic separate compilation

# Compile individual files
gcc -c main.c # Creates main.o
gcc -c utils.c # Creates utils.o
gcc -c math.c # Creates math.o

# Link all object files
gcc main.o utils.o math.o -o program
./program

Compiles each source file separately, then links them into an executable

Compile multiple files at once

gcc -c main.c utils.c math.c
ls *.o # Shows: main.o utils.o math.o
gcc *.o -o program

Compiles all source files to object files in one command

Incremental build example

# Initial compilation
gcc -c *.c
gcc *.o -o program

# After modifying only main.c
gcc -c main.c # Only recompile changed file
gcc *.o -o program # Relink with all objects

Only recompile changed files for faster builds

Creating a static library

# Compile library source files
gcc -c lib1.c lib2.c lib3.c

# Create static library
ar rcs libmylib.a lib1.o lib2.o lib3.o

# Use library in compilation
gcc -c main.c
gcc main.o -L. -lmylib -o program

Creates a reusable static library from object files

Combining with other options

gcc -c -g -Wall -O2 -o optimized.o source.c
gcc -c -DDEBUG -I./include module.c

Combines -c with debugging, warnings, optimization, and preprocessor options

Typical Workflow

  1. Development: Write and edit individual .c files
  2. Compilation: Use gcc -c to compile changed files
  3. Linking: Link all .o files into executable
  4. Testing: Run and test the program
  5. Iteration: Modify files and repeat from step 2

When to Use gcc -c

  • Large Projects: Projects with many source files
  • Library Development: Creating reusable code libraries
  • Incremental Builds: Faster compilation during development
  • Modular Programming: Separate compilation of modules
  • Build Systems: Integration with Makefiles and build tools
  • Cross-Platform: Compile for different architectures separately

See also