C++ Makefile
C++ is object orient programming.Each object file can be created by its own .cpp and .h file.
For instance, If you have the following files
main.cpp -----------------> main.cpp uses a object, called person
person.cpp -----------------> person object source file
person.h. -------------------> person object header file
You need to compile these three files at once.
If you would like to compile using a terminal(linux, mac os), you can type it like following
g++ main.cpp person.cpp
It is pretty simple. right?
But how about this??
You have 100 object files that are need to be comfile.
it would be like
g++ main.cpp a.cpp b.cpp c.cpp d.cpp ...................
It would be really painful.. YOU need to type it every file for every compiling.
Fortunately, there is a better option, you can use Makefile
Makefile tells make, a UNIX command, to communicate with c++ compiler to link and compile a program.
If you do not know UNIX, it is ok. Think it as the father of all operacting system(window, linux, mac os and many more....). To be honest, I do not know it well either hahahaha
If you use Makefile, you need to type the compiling command just onece.
After you create Makefile, you just need to type "make" in the terminal.
Format
target : dependencysystem command
Actually, it is really simple. If you remember these two lines, you can make Makefile pretty much.
One thing you need to remember is that this file is space sensitive. make sure you tab for the second line
Example
output: main.o Person.o 3)g++ main.o Person.o -o output
main.o: main.cpp 1)
g++ -c main.cpp
Person.o: Person.cpp Person.h 2)
g++ -c Person.cpp
clean: 4)
rm *.o output
1) "g++ -c main.cpp" creates "main.o"
2) "g++ -c Person.cpp" create "Person.o". Person is an object, so you need to add .h and .cpp for depedency
3) after 1) and 2) are done, Makefile will work on making output. "-o output" makes the excutable's name "output"
4) it is optional feature. It is deletion of excutable and other .o file. After you compile, and want to delete all .o files and output. You can type "make clean". It will do the job for you.
*excutable = file that runs progream (it is like ".exe" for window and ".jar" for java)
---------------------------------------------------------------------------------------------------------------------------
The following is practice code. If you have suggestion or correction, please feel free to leave comment.
To compile
makeTo delete .o files and output(excutable)
make cleanThere are four files
1) MakeFile
output: main.o Person.o
g++ main.o Person.o -o output
main.o: main.cpp
g++ -c main.cpp
Person.o: Person.cpp Person.h
g++ -c Person.cpp
clean:
rm *.o output
2) main.cpp
#include <iostream>
#include <functional> // i am using mac so i need this to use namespace std
#include <string>
#include "Person.h"
using namespace std;
int main( int argc, char *argv[])
{
Person a("Tom", "Jang");
a.printInfo();
}
3) Person.h
#include <iostream>
#include <functional> // i am using mac so i need this to use namespace std
#include <string>
using namespace std;
class Person
{
private:
string firstName;
string lastName;
public:
Person();
Person( string fName, string lName );
void printInfo();
};
4) Person.cpp
#include <iostream>
#include <functional> // i am using mac so i need this to use namespace std
#include <string>
#include "Person.h"
using namespace std;
Person :: Person()
{
firstName = "empty";
lastName = "empty2";
}
Person :: Person ( string fName, string lName )
{
firstName = fName;
lastName = lName;
}
void Person :: printInfo()
{
cout << "firstName is " << firstName << endl;
cout << "lastName is " << lastName << endl;
}