在这里,我们将看到如何在C ++程序中编译多个cpp文件。任务很简单。我们可以将名称作为列表提供给g ++编译器,以将其编译为一个可执行文件
要一次编译多个文件,例如abc.cpp和xyz.cpp,语法将如下所示:
g++ abc.cpp xyz.cpp
要运行程序,我们可以使用它-
./a.out
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
}#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;
}输出结果
$ 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 $