在 c ++ 程式中編譯多個 .cpp 檔案


在此我們將瞭解如何在 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
$

更新於:2019 年 7 月 30 日

1 千次以上瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.