In C programming, different file types (.c, .o, .a, .so, etc.) serve specific purposes throughout the software development lifecycle. Here’s a breakdown of each:

1. .c Files

  • Purpose: These are source code files written in the C programming language.
  • Contents: Contain human-readable C code (functions, definitions, etc.).
  • Usage: These files are compiled into object files (.o) by a compiler like gcc.
  • Example:
#include <stdio.h>  
void hello() {     
    printf("Hello, World!\n");
}

2. .o Files

  • Purpose: These are object files, the result of compiling .c files.

  • Contents: Contain machine code for a single source file, not yet linked.

  • Usage:

    • Used as intermediate files in the build process.
    • Linked with other object files or libraries to create an executable or shared library.
  • Command to Generate:

  • gcc -c file.c -o file.o

  • Key Point: .o files are not standalone; they lack information about external dependencies.


3. .a Files

  • Purpose: These are static library files.

  • Contents: Contain a collection of .o files bundled together.

  • Usage: Linked into an executable at compile time. The code from the .a file is directly embedded into the final executable.

  • Command to Create:

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

  • Advantages:

    • Faster runtime as everything is embedded in the executable.
  • Disadvantages:

    • Larger executable size.
    • Updating requires recompiling the application with the new .a file.

4. .so Files

  • Purpose: These are shared library files (also called dynamic libraries).

  • Contents: Contain code similar to .a files but loaded dynamically at runtime.

  • Usage:

    • Allow multiple programs to share the same library file.
    • Reduce the size of executables.
  • Command to Create:

  • gcc -shared -o libmylib.so file1.o file2.o

  • Advantages:

    • Smaller executables.
    • Easier updates: only the .so file needs to be replaced.
  • Disadvantages:

    • Dependency on the .so file at runtime.
    • Requires proper installation (e.g., using LD_LIBRARY_PATH or placing it in standard library paths).

Comparison Summary

File Type

Purpose

Contents

Usage

.c

Source code

Human-readable C code

Compile to .o files

.o

Compiled object file

Machine code

Link to form executables/libraries

.a

Static library

Bundled .o files

Linked at compile time

.so

Shared library

Bundled .o files

Linked at runtime