make is a program that controls the building of other things, usually programs.
Why?
Most programs are split into many source files. The program needs to be recompiled if something changes, but not the whole program, just the affected parts. That's what
make deals with.
There is more than one
make program, but in the Linux world
GNU Make is the most common one. It has lots of built-in rules that make it a little easier to use.
What to do
If your program has only one file, and it's called
prog1.cpp, you don't need to do anything, the built-in rules do all the work. You just type:
If your program has two files,
main.cpp and
work.cpp, and the program name is
prog1, you need a Makefile. The name of the file is
Makefile and it contains:
1 2
|
prog1: main.o work.o
g++ -o $@ $^
|
What does it all mean?
prog1 needs to be updated when
main.o or
work.o change and it's updated by running:
|
g++ -o prog1 main.o work.o
|
$@ is the target to be built.
$^ is the list of dependencies
GNU Make knows that the .o can be built from a .cpp and how to do it.
In the case where you have just one source file,
GNU Make also knows how to build a program from a .o file. That's why you don't need a makefile.
Finally, in the makefile, there is a tab on line 2, not spaces.
It will not work with spaces.