Compiling Multiple files to build an Application

Introduction

To do this, multiple files need to be compiled into a single file creating a single application, providing a Programmer to debug the necessary file, recompile only the modified file, and execute the application. 'C' Provides multiple file compilation to facilitate the generation of a single application.

Solution 1. The 'C' Compiler, cc can be used directly to compile multiple files into a single application. All a programmer has to do is to specify the names of the file one after the other as an argument to cc command and a single executable file will be created.

Example

cc file1.c file2.c file3.c file4.c

The above command will independently compile all four files, create an object file, and then link all these object files into a single executable file a.out.

The 'C' Compiler can also be used to compile only the files that were modified and attach them to the executable file. The following example illustrates this

Example

cc file1.c file2.o file3.o file4.o

In the above example, only file1.c is compiled and is linked with the other three object code files to make a single executable file a.out.

Solution 2. We can use the #include preprocessor directive to instruct the 'C' compiler, to include the different files in which we have created functions that are to be linked with the master file program.

Consider the following source codes.

/* file1.c - Name of the file */
#include "file2.c"
#include "file3.c"
#include "file4.c"

void main() {
    // Executable portion of main program
    // ........
    // ........

    function1();
    function2();
    function3();
}

void function1() {
    // Executable code
    // ........
}

void function2() {
    // Executable code
    // ........
}

void function3() {
    // Executable code
    // ........
}

To compile the above four files into an application, we'll give the following command.

c cfile1.c. the above command will create a single executable file that will contain all the different functions present in different files.

Code Explanation

#include directive, instruct the 'C' compiler to search for the function(s) in the specified files. The function could be the standards 'C' function or the user design function as used above. The compiler automatically picks up all the files specified after the #include directive, complies with them, and creates a single executive file a.out.

Note. When we use the #include directive to include a file containing functions, the file that contains functions, does not have the main() function. Why....? this is because when the #include directive is used, the functions are treated as part of itself, and since only one main() function is allowed in the 'C' program, the file containing functions does not have the main() function.

Precautions

The file used to be #include directive, having a function name instead of the main() cannot be compiled independently. These can be compiled only with the main function containing the main function.

Summary

Compiling multiple files in C allows a programmer to build a single application by combining code from different source files. Using the C compiler (cc), multiple files can be compiled either by listing all files in a single command or by compiling each file separately into object files and linking them together. This method helps in incremental compilation, where only modified files are recompiled, improving efficiency. Another approach involves using the #include preprocessor directive to include additional source files within a main file. This ensures all functions across files are compiled into a single executable. However, only one file should contain the main() function, and the included files should only define additional functions.


Similar Articles