Build a C++ Program that Have Multiple Source Code Files
Last Updated :
28 Apr, 2025
In C++ generally, we saw only a single file program but what if we have multiple source code files and we have to build a program by using all these files? Let's see how we can build a C++ program with multiple source files without including them as header files.
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 header file
- Using -gcc commands
1. Using header file
Example:
#include<iostream>
// Here all basic commands can be used using this files
So, how to use another file let us check with an example.
First Program:
C++
// firstFile.h
// C++ Program to implement
// using gcc commands
int add(int a, int b) { return a + b; }
Second Program:
C++
// secondFile.cpp
// C++ Program to implement
// Using gcc commands
#include <iostream>
#include "firstFile.h"
using namespace std;
// 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.
cout << add(14, 16) << endl;
cout << add(2, 3) << endl;
return 0;
}
Output:
Output with the method with a header file2. Using GCC Command to Build C++ Program
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.
Working on C++ Program
Firstly we need to get an idea of how to compile a C++ program with GCC commands. It goes from the following steps :Â
- 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
- Compiling: texts are converted to machine code but function calls are not mapped with function definitions.
- Linking: the mapping between function calls and function definitions happens 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
g++ -o main firstFile.cpp secondFile.cpp
By using the above command we can build a C++ executable. We can add multiple files as arguments in the given command.
Example:
First Program:
C++
// secondFile.cpp
// C++ Program to implement
// using gcc commands
int add(int a, int b) { return a + b; }
Second Program:
C++
// firstFile.cpp
// C++ Program to implement
// Using gcc commands
#include <iostream>
using namespace std;
// 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.
cout << add(14, 16) << endl;
cout << add(2, 3) << endl;
return 0;
}
Demonstration:
Output using GCC method
Similar Reads
Multiple Source Files in C 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
3 min read
Write a program that produces different results in C and C++ Write a program that compiles and runs both in C and C++, but produces different results when compiled by C and C++ compilers. There can be many such programs, following are some of them. 1) Character literals are treated differently in C and C++. In C character literals like 'a', 'b', ..etc are tre
2 min read
Writing First C++ Program - Hello World Example The "Hello World" program is the first step towards learning any programming language and is also one of the most straightforward programs you will learn. It is the basic program that demonstrates the working of the coding process. All you have to do is display the message "Hello World" on the outpu
4 min read
VS Code | Build, Run and Debug in C++ In this article, we will discuss the VS Code setup required for break-point debugging. Firstly create a file launch.json that configures the VS Code to launch the GDB debugger at the beginning of the debugging process. Then create a file tasks.json that tells VS Code how to build (compile) the progr
6 min read
How To Compile And Run a C/C++ Code In Linux C Programming Language is mainly developed as a system programming language to write kernels or write an operating system. C++ Programming Language is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance. In this article, we will be compiling and exe
4 min read