在 C++ 中使用 Makefile 以及其應用
在本教程中,我們將討論一個程式,以瞭解 C++ 中的 Makefile 及其應用。
這項任務通常透過建立 .cpp 檔案和 .h 檔案(其中包含所有類/功能)並將它們連結在一起來完成。
示例
main.cpp
#include <bits/stdc++.h> #include "function.h" using namespace std; //main execution program int main(){ int num1 = 1; int num2 = 2; cout << multiply(num1, num2) << endl; int num3 = 5; cout << factorial(num3) << endl; print(); }
print.cpp
#include <bits/stdc++.h> #include "function.h" using namespace std; void print() { cout < "makefile" << endl; }
factorial.cpp
#include <bits/stdc++.h> #include "function.h" using namespace std; //factorial program int factorial(int n){ if (n == 1) return 1; return n * factorial(n - 1); }
multiply.cpp
#include <bits/stdc++.h> #include "function.h" using namespace std; int multiply(int a, int b){ return a * b; }
functions.h
#ifndef FUNCTIONS_H #define FUNCTIONS_H void print(); int factorial(int); int multiply(int, int); #endif
輸出
2 120 makefile
廣告