Makefile in action
สร้างไฟล์โปรแกรมจำนวน 3 ไฟล์ ได้แก่
hello.cpp
function.h
main.cpp
// hello.cpp
#include <iostream>
#include "function.h"
using namespace std;
void print_hello()
{
cout << "Hello World" << endl;
}
// function.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
void print_hello();
#endif
// main.cpp
#include <iostream>
#include "function.h"
using namespace std;
// main() is where program execution begins.
int main() {
print_hello();
return 0;
}
หลังจากนั้นให้สร้างไฟล์ Makefile
ดังนี้
all: hello
hello: main.o hello.o
g++ main.o hello.o -o hello
main.o: main.cpp
g++ -c main.cpp
hello.o: hello.cpp
g++ -c hello.cpp
clean:
rm -rf *o hello
หลังจากนั้นให้ทำการรันด้วยคำสั่งดังนี้
$ make
g++ -c main.cpp
g++ -c hello.cpp
g++ main.o hello.o -o hello
$ ./hello
Hello World
Last updated
Was this helpful?