Makefile steps

Could anyone please write to me the simple steps in simple English involved in making Makefile in Linux for C++?

Many Thanks
No need for that.
If you have a look at some examples, you'll get it.

Below is an example
1
2
3
4
5
6
7
8
9
10
11
12
PROG=a.out

OBJS=main.o func1.o func2.o

CFLAGS=-g -Wall #this is optional

all:		$(OBJS) #specify dependency on object files
	g++ $(CFLAGS) $(OBJS) -o $(PROG)

clean: #clean executables and object files
	rm $(OBJS) $(PROG)
Google search is your friend. Just enter "Makefile tutorial" and you should see a few examples to learn.

Please note the latter Make version support even more functionalities than what we usually know. I don't know if it support conditional and branching logic inside a Makefile ?
Thanks h9uest and sohguanh

I need to understand the meaning of each line. I have looked for tutorials but I could understand. Could you please write to me the meaning of each line in a simple working makefile.

Thanks
closed account (z05DSL3A)
GulHK,

This may help you:
Managing Projects with GNU Make
http://www.cplusplus.com/forum/articles/32186/
Thank you Grey Wolf :-). Let me go through the book.
This is a simple makefile I have just created but it is giving me a error

make: Nothing to be done for `run-64bit'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# top-level rule to compile the whole program.
all: run-64bit

# program is made of several source files.
run-64bit: run.o monitor.h formatter.h logger.h
	g++ run.o monitor.o formatter.o logger.o -o run-64bit

# rule for file "main.o".
run.o: run.cpp monitor.h formatter.h logger.h
	g++ -g -Wall -c run.cpp

# rule for file "file1.o".
monitor.o: monitor.h monitor.cpp formatter.h logger.h 
	g++ -g -Wall -c monitor.cpp

# rule for file "file2.o".
formatter.o: formatter.h formatter.cpp
	g++ -g -Wall -c formatter.cpp
        
# rule for file "file2.o".
logger.o: logger.h logger.cpp
	g++ -g -Wall -c logger.cpp

# rule for cleaning files generated during compilations.
clean:
	/bin/rm -f run-64bit run.o monitor.o formatter.o logger.o
closed account (z05DSL3A)
I have not done much with makefiles for a while (I'm lazy and use IDEs).

Try:
CC=g++
CFLAGS=-c -Wall

all: run-64bit

run-64bit: run.o monitor.o formatter.o logger.o
	$(CC) run.o monitor.o formatter.o logger.o -o run-64bit 

run.o: run.cpp
	$(CC) $(CFLAGS) run.cpp

monitor.o: monitor.cpp
	$(CC) $(CFLAGS) monitor.cpp

formatter.o: formatter.cpp
	$(CC) $(CFLAGS) formatter.cpp

logger.o: logger.cpp
	$(CC) $(CFLAGS) logger.cpp

clean:
	rm -rf *o run-64bit


Edit:
This should also work but you will have to read up on how it works (I can't remember)
CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=run.cpp monitor.cpp formatter.cpp logger.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=run-64bit

all: $(SOURCES) $(EXECUTABLE)
	
$(EXECUTABLE): $(OBJECTS) 
	$(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
	$(CC) $(CFLAGS) $< -o $@
Last edited on
Thank you so much Grey Wolf :-). Did you just add the top two lines and replaced g++ with CC?
closed account (z05DSL3A)
CC=g++
CFLAGS=-c -Wall

These are variables so that you can easily change the compiler or compiler flags in one place for all the rules, so the line, $(CC) $(CFLAGS) run.cpp gets translated into g++ -c -Wall run.cpp.

run-64bit: run.o monitor.o formatter.o logger.o

Says run-64bit depends on the following: run.o, monitor.o, formatter.o, and logger.o.
the next set of rules tells it how to build those dependencies:
run.o: run.cpp
	$(CC) $(CFLAGS) run.cpp
...


Your file says that run-64bit depends on one object file and three header files:
run-64bit: run.o monitor.h formatter.h logger.h


You should be able to put the two side by side to see any other differences.
Topic archived. No new replies allowed.