What is a makefile ?

Hello everyone,

i need to make a makefile for my program since it was asked for by our teacher but he never showed us how and the descriptions ive read online dont really help on how to make them or what they are. can some one please explain what it is and how i can make the makefile. I'm using Xcode but i can also use the terminal on Ubuntu with a GCC compiler. say for example a basic program:
1
2
3
4
5
6
7
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World"<<endl;
return 0;
}


what would be the make file for this program?(not my actual program)
A makefile is used to tell the compiler which source files you want to compile. It'll also do things like name your excecutable and place it in a specific location.

Here's a link that could help:
http://www.network-theory.co.uk/docs/gccintro/gccintro_16.html
that didnt really help

i mostly just want what exactly i have to enter in the terminal
Last edited on
A makefile is a file containing instructions for how to atomically build your program. If you have a makefile named makefile you should only have to run the make command (without arguments) to build the program.

Example:
If the code posted above is put into a file named main.cpp a makefile could be as simple as
1
2
all:
	g++ main.cpp -o hello
(assuming g++ compiler).

It's possible to add much advanced stuff to it but I think you better use a search engine to find some good makefile tutorials to learn from.
Last edited on
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:
 
make prog1

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.
still not working for me can you please just tell me what exactly i have to enter in the terminal? assume i guess that the files are called main.cpp work.cpp since it makes sense that make is meant for more than one file.

still if this can be done on xcode even better
Your attitude frustrates the heck out of me, rro0035. A huge part of this field is learning things yourself and it doesn't seem you have any interest in that.

Why don't you just call a classmate and copy their solution? You don't seem to have the interest in actually learning so that would be the better choice ( and quicker since you wouldn't have to ask people on the internet for a solution. )
Last edited on
A makefile can be used on a single file to automate Terminal commands; for example, so you do not have to enter g++ main.cpp -o hello every single time you want to recompile. Its utility is not necessarily confined to "more than one file."

still not working for me can you please just tell me what exactly i have to enter in the terminal? assume i guess that the files are called main.cpp work.cpp since it makes sense that make is meant for more than one file.

still if this can be done on xcode even better


What you enter into the Terminal is dependent upon the contents of your makefile.

If your makefile looks like this (as it very well might in the case of a single hello world program):

1
2
 all:
	g++ main.cpp -o hello


You would go into the Terminal, cd to the directory containing the makefile and the hello world source code (main.cpp), and enter

make all

to compile your program using the makefile, and then

./hello

to run the "hello" program you just compiled.

Very good website explaining the makefile basics, and using a hello world project:

http://mrbook.org/tutorials/make/
Last edited on
+1 georgewashere
Your attitude frustrates the heck out of me, rro0035. A huge part of this field is learning things yourself and it doesn't seem you have any interest in that.

Why don't you just call a classmate and copy their solution? You don't seem to have the interest in actually learning so that would be the better choice ( and quicker since you wouldn't have to ask people on the Internet for a solution. )


i am trying to learn on my own since it appears teachers leave me out to hang. a makefile was never mentioned in class. no idea what it was or how to make them. and after trying several times i figured out that it is a separate file that needs to be made with a text editor containing the commands that other previous users posted.

it frustrates me that people like you get angry and dont even bother to help. if it was so trivial all that needed to be said was this.
Topic archived. No new replies allowed.