C programming language, as it is a procedural programming language sometime we need two or more programs linked together to store all the methods together. So, let’s check how to link two C source files. Methods to compile multi-file c program are given below:
Methods to use two or more source code files in C
There are two methods to use two or more source code files in C as mentioned below:
- Using as a header file
- Using –gcc commands
1. Using as a Header file
After creating two separate files we can use one of them inside another file by using another file as a header file. Header files are used to use the properties from the already written files.
Example:
#include<stdio.h>
// Here we are loading stdio file containing data about printf,scanf,etc.
So, how we can use another file let’s check with an example program.
First Program:
// C Program second.c // to be used in first.c int sum( int a, int b) {
return a + b;
} int sub( int a, int b) {
return a - b;
} int multiply( int a, int b) {
return a * b;
} |
Second Program:
// C Program to use code another // file into this file #include "second.c" #include <stdio.h> int main()
{ // declared two variables
int a = 4, b = 5;
// sum function called
int ans = sum(a, b);
printf ( "Sum: %d" , ans);
// sub function called
ans = sub(a, b);
printf ( "Subtraction: %d" , ans);
// multiply function called
ans = multiply(a, b);
printf ( "Multiply: %d" , ans);
return 0;
} |
Output:
In the above program, we were able to use the functions declared in second.c by just including the program using #include “second.c”.
2. Using GCC commands binds both programs together
In this method, we will use the gcc command for running the program because of which we bind both the programs together while running. But, before running the program let’s check how C programs work.
How do C Programs work?
Firstly let’s get an idea of how a C program build. It goes from the following steps :
- Preprocessing
- Compiling
- Linking
In preprocessing only comments are removed from the source code and header files code is pasted in the file also macros are placed where they are used. After this, In compiling texts are converted to machine code but function calls are not mapped with function definitions. In linking the mapping between function calls and function definitions is happened as its name suggests ‘linking’. It links the function calls and function definitions.
At the linking phase, we have to add all the compiled C files (object files) so that the linker links all the files and executables can be formed.
Command to build a C file with more than one C source code file
gcc -o main firstFile.c secondFile.c
Example
First Program:
// secondFile.c // C Program to implement // using gcc commands int add( int a, int b) // basic add function
{ return a + b;
} |
Second Program:
// firstFile.c // C Program to implement // Using gcc commands #include <stdio.h> // just declaring the function int add( int a, int b);
int main()
{ // In this there is no add function definition. add
// function definition is in another file.
printf ( "%d\n" , add(14, 16));
printf ( "%d\n" , add(18, 16));
return 0;
} |
Demonstration:
See the commands in the below image for a demonstration.