Makefile questions

Trying makefiles for the first time (used to VS). I have a few questions:

1. All files (src, include [for entry points], bin and intermediate) are in the same directory. How can I specify paths for each one?

2. Is there a way to automatically make header dependencies for .o targets?
If I add #include "dictionary.hpp" in synthesizer.hpp, do I have to go back and change my makefile?

3. I've been reading about implicit rules here: http://www.gnu.org/software/make/manual/make.html#Implicit-Rules. I like the -Wall option, how do I include that in an implicit rule so that I can replace
1
2
synthesizer.o : synthesizer.cpp synthesizer.hpp
	$(CC) $(CFLAGS) synthesizer.cpp

with
1
2
synthesizer.o : synthesizer.hpp


This is my current makefile:
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
CC = g++
CFLAGS = -c -Wall
objects = synthesizer.o \
          dictionary.o \
          vtest.o

.DEFAULT : all

all : vtest

vtest : $(objects) 
	$(CC) $(objects) -o vtest

synthesizer.o : synthesizer.cpp synthesizer.hpp
	$(CC) $(CFLAGS) synthesizer.cpp

dictionary.o : dictionary.cpp dictionary.hpp
	$(CC) $(CFLAGS) dictionary.cpp

vtest.o : vtest.cpp dictionary.hpp
	$(CC) $(CFLAGS) vtest.cpp

.PHONY : clean
clean:
	-rm vtest *.o
Last edited on
http://users.softlab.ece.ntua.gr/~ttsiod/makefile.html

> How can I specify paths for each one?
For headers, use the INCLUDEFLAGS, by instance -Iheader
note how it has different directories for binary, object files, dependencies, in debug and release configuration.

> Is there a way to automatically make header dependencies for .o targets?
The header dependencies are generated by invoking g++ with the -MM -MP flags

> I like the -Wall option, how do I include that in an implicit rule
CFLAGS are flags for compiling C code
CXXFLAGS are flags for compiling C++ code
Ah, so CFLAGS is actually a reserved keyword in make that is implicitly added to the compiler options?

I'm trying to find the INCLUDEFLAGS for the object files, but $man g++ is massive!

I found -MM in there. That looks perfect for what you mentioned.
Last edited on
Hmm after adding -MM, the build seems to fail. Before it failed, figured out how to get the output directory in there though. (make is still built from the src folder though).
Here is the makefile/Output
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
27
28
29
30
31
32
33
34
35
36
# objects
objects = synthesizer.o \
          dictionary.o \
          vtest.o

# paths (to integrate)
#SRC = src
OUT = ../bin
#INC = src
#INT = int

# compile options
CXX = g++
CXXFLAGS = -c -Wall -MM 

.DEFAULT : all

all : vtest

vtest : $(objects)
	$(CXX) $(objects) -o $(OUT)/vtest

# intermediate files
synthesizer.o : synthesizer.cpp
	$(CXX) $(CXXFLAGS) synthesizer.cpp

dictionary.o : dictionary.cpp
	$(CXX) $(CXXFLAGS) dictionary.cpp

vtest.o : vtest.cpp
	$(CXX) $(CXXFLAGS) vtest.cpp

#cleanup
.PHONY : clean
clean:
	-rm $(OUT)/vtest *.o
stew@Romulus ~/SVN/vsynth/trunk/src $ make
g++ -c -Wall -MM  synthesizer.cpp
synthesizer.o: synthesizer.cpp synthesizer.hpp vsynth.hpp dictionary.hpp
g++ -c -Wall -MM  dictionary.cpp
dictionary.o: dictionary.cpp dictionary.hpp vsynth.hpp
g++ -c -Wall -MM  vtest.cpp
vtest.o: vtest.cpp dictionary.hpp vsynth.hpp
g++ synthesizer.o dictionary.o vtest.o -o ../bin/vtest
g++: error: synthesizer.o: No such file or directory
g++: error: dictionary.o: No such file or directory
g++: error: vtest.o: No such file or directory
g++: fatal error: no input files
compilation terminated.
make: *** [vtest] Error 4

And the output:
Last edited on
> I'm trying to find the INCLUDEFLAGS for the object files
That's just the name given in the example.
CPPFLAGS are the flags for the preprocessor.


> Hmm after adding -MM, the build seems to fail
-MM would not build, it generates dependencies.
$ g++ -c -Wall -MM  synthesizer.cpp
synthesizer.o: synthesizer.cpp synthesizer.hpp vsynth.hpp dictionary.hpp
The idea is to put those dependencies in your makefile
By instance, sending that output to a file and then include it from your makefile.

Take a look at the link
That link actually really helped. I've got it working now with proper dependencies and intermediate directories. A lot of the syntax I'm still confused about, but I think I can worry about specific lines when I need to change them.

Thanks,
Last edited on
Topic archived. No new replies allowed.