Compiling multiple .cpp files in c++ program


Here we will see how to compile multiple cpp file in C++ program. The task is very simple. We can provide the names as a list to the g++ compiler to compile them into one executable file

To compile multiple files like abc.cpp, and xyz.cpp at once, the syntax will be like this −

g++ abc.cpp xyz.cpp

To run the program, we can use this −

./a.out

Example

float area(float r){
   return (3.1415*r*r); //area of a circle
}
float area(float l, float w) {
   return (l * w); //area of a rectangle
}

Example

#include <iostream>
#include "area.cpp"
using namespace std;
main() {
   cout << "Area of circle with radius 2.5 is: " << area(2.5) << endl;
   cout << "Area of rectangle with length and width are 5 and 7 is: " << area(5, 7) << endl;
}

Output

$ g++ area.cpp find_area.cpp
$ ./a.out
Area of circle with radius 2.5 is: 19.6344
Area of rectangle with length and width are 5 and 7 is: 35
$

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements