My C file is too big! Multifile source compilation

In this basic example, we will see how to use a header file, compile two source files into one executable, and separate a C program into its component parts using gcc.

File Structure:

  • main.c: The main source file.

  • functions.c: A source file containing additional functions.

  • functions.h: A header file containing function declarations.

functions.h:

// functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H

// Function declaration
void print_hello();

#endif // FUNCTIONS_H

functions.c:

// functions.c
#include "functions.h"
#include <stdio.h>

// Function definition
void print_hello() {
    printf("Hello, World!\n");
}

main.c:

// main.c
#include "functions.h"

int main() {
    // Call function from another file
    print_hello();
    return 0;
}

Compilation Steps:

  1. Compile functions.c into an object file:

     gcc -c functions.c -o functions.o
    

    This command compiles functions.c into an object file functions.o without linking.

  2. Compile main.c into an object file:

     gcc -c main.c -o main.o
    

    This command compiles main.c into an object file main.o without linking.

  3. Link the object files into a single executable:

     gcc main.o functions.o -o myprogram
    

    This command links main.o and functions.o together to create the final executable myprogram.

After these steps, you will have an executable file named myprogram that you can run on your system. To execute the program, use:

./myprogram

This will output:

Hello, World!

This is only a basic example; in practice, your program's complexity will determine the number of source and header files you require, as well as whether or not you need to link with other libraries or include additional compiler options.

0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.