Bạn thử làm ví dụ này xem .
1. Tạo file helloworld.h
PHP Code:
#ifndef __HELLOWORLD__
#define __HELLOWORLD__
#include <iostream>
using namespace std;
void print();
#endif
2. File helloworld.cpp
#include "helloworld.h"
PHP Code:
void print()
{
cout << "Hello world" << endl;
}
3. File main.cpp
PHP Code:
#include "helloworld.h"
int main()
{
print();
}
4. File makefile
PHP Code:
all: helloworld.exe
CC = cl
LD = link
LDFLAGS = /RELEASE /MANIFEST:NO /NOLOGO /out:helloworld.exe
CCFLAGS = /EHsc /c
objs = main.obj helloworld.obj
includes = helloworld.h
helloworld.exe : $(objs)
$(LD) $(LDFLAGS) $(objs)
$(objs) : $(includes)
$(CC) $(CCFLAGS) $*.cpp
5. Chạy dòng lệnh:
PHP Code:
nmake -f makefile
Lưu ý :
- Mình dùng cl và link của VC++ để dịch và tạo file exe.
- CCFLAGS và LDFLAGS là các chỉ thị ( options ) dùng để dịch và chạy.
Bạn có thể vào msdn tìm hiểu ý nghĩa từng options .
- objs và includes :
makefile quy định các ràng buộc khi tạo file mới. Trong trường hợp này:
Để link tạo thành helloworld.exe , thì cần các file objects ( $(objs) )
Tương tự, để dịch các file .cpp cần các file .h ( $(includes )).
Bạn có thể tham khảo file makefile ở trên để hiểu rõ hơn.