You are on page 1of 15

Makefile

Nguyn Vn Khit

Ni dung
Bin dch chng trnh C++ Cch thc to Makefile V d Lm vic vi C#

Bin dch chng trnh C++


S dng g++. Phi thc hin ng 2 qu trnh. V d : mypoint.cpp, mypoint.h, main.cpp.

Example
//mypoint.h #ifndef __MYPOINT_H__ #define __MYPOINT_H__ class MyPoint { private: int x,y; public: MyPoint(int x,int y); void Move(int xoffset,int yoffset); void CurPos(); }; #endif

Example
//mypoint.cpp #include <iostream.h> #include "mypoint.h" MyPoint::MyPoint(int x,int y) { this->x = x; this->y = y; } void MyPoint::Move(int xoffset,int yoffset) { x+= xoffset; y+= yoffset; } void MyPoint::CurPos() { cout<<"I'm at ("<<x<<","<<y<<")"<<endl; }

Example
//main.cpp #include <iostream.h> #include "mypoint.h" void main() { cout<<"Hello, there!"<<endl; MyPoint p(3,4); p.CurPos(); p.Move(2,3); p.CurPos(); cout<<"Bye!"<<endl; }

Example
Linux: g++ -c main.cpp mypoint.cpp g++ main.o mypoint.o o point Windows (VC++ 6.0): cl /c main.cpp mypoint.cpp link main.obj mypoint.obj /out:point.exe

T ng ha qu trnh bin dch


Dng tp tin Makefile m t qu trnh bin dch. Dng cc cng c make trn tng mi trng thc thi Makefile. Ch bin dch li khi c s thay i trong m ngun. C th truyn tham s cho qu trnh bin dch.

Cch thc to Makefile


Gm nhiu khi thc hin. Mi khi gm:
Dependencies : Lit k cc thnh phn ph thuc (cn c trc). Rule: Cch thc thc hin thnh phn.

Khi all : cha thnh phn to ra thnh phn chnh. Khi clean : thnh phn cui cng

Makefile
Linux: //Makefile all: point point: main.o mypoint.o g++ main.o mypoint.o o point main.o: main.cpp mypoint.h g++ -c main.cpp mypoint.o: mypoint.cpp mypoint.h g++ -c mypoint.cpp

Makefile
Windows: //Makefile all: point point: main.obj mypoint.obj link main.obj mypoint.obj /out:point.exe main.obj: main.cpp mypoint.h cl /c main.cpp mypoint.obj: mypoint.cpp mypoint.h cl /c mypoint.cpp

Makefile
//Makefile all: point point: main.o mypoint.o g++ $(CFLAGS) main.o mypoint.o o point main.o: main.cpp mypoint.h g++ $(CFLAGS) -c main.cpp mypoint.o: mypoint.cpp mypoint.h g++ $(CFLAGS) -c mypoint.cpp

Makefile
make make CFLAGS=-O2 make CFLAGS=-g

Lm vic vi C#
Tng t nh vi C++. Lnh command line ca C# : csc V d csc /out:MyProg.exe Form1.cs Form1.Designer.cs Program.cs

Lm vic vi C#
Cc tham s khc ca csc:
/target : loi assembly s c sinh ra /reference (/r): cc tham chiu trong qu trnh dch /lib : xc nh cc th mc tm th vin

You might also like