Last updated 4 years ago
Was this helpful?
สร้างไฟล์โปรแกรมจำนวน 3 ไฟล์ ได้แก่
hello.cpp
function.h
main.cpp
หลังจากนั้นให้สร้างไฟล์ Makefile ดังนี้
Makefile
หลังจากนั้นให้ทำการรันด้วยคำสั่งดังนี้
// 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; }
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