Computer >> Máy Tính >  >> Lập trình >> C ++

Sử dụng G ++ để biên dịch nhiều tệp .cpp và .h

Để biên dịch nhiều tệp như file_name.h hoặc file_name.cpp cùng một lúc, chúng ta có thể sử dụng lần lượt các tệp như một danh sách. Cú pháp sẽ như thế này -

g++ abc.h xyz.cpp

Để chạy chương trình, chúng ta có thể sử dụng cái này -

./a.out

Ví dụ

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
}

Ví dụ

#include <iostream>
#include "area.h"
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;
}

Đầu ra

$ g++ area.h 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
$