> For the complete documentation index, see [llms.txt](https://docs.aic-eec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aic-eec.com/c-c++-for-embedded-programming/c-programming-techniques/makefile-in-action.md).

# Makefile in action

สร้างไฟล์โปรแกรมจำนวน 3 ไฟล์ ได้แก่

* hello.cpp
* function.h
* main.cpp

```c
// hello.cpp

#include <iostream>
#include "function.h"
using namespace std;

void print_hello()
{
   cout << "Hello World" << endl;
}
```

```c
// function.h

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

void print_hello();

#endif
```

```c
// main.cpp

#include <iostream>
#include "function.h"
using namespace std;

// main() is where program execution begins.
int main() {

	print_hello();
	return 0;
}
```

หลังจากนั้นให้สร้างไฟล์ `Makefile` ดังนี้

```c
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
```

หลังจากนั้นให้ทำการรันด้วยคำสั่งดังนี้

```c
$ make
g++ -c main.cpp
g++ -c hello.cpp
g++ main.o hello.o -o hello
```

```c
$ ./hello
Hello World
```
