2

I have a folder containing several .cpp files, e.g. A.cpp, B.cpp and C.cpp. They are all independent from each other. I could compile A.cpp to A.o and then to binary file A. So it is with B.cpp and C.cpp.

I want to write a makefile that could compile all these files at once. And in the future, if there are some new files such as D.cpp is put into this folder, makefile can still handle it automatically.

My current file is

CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=$(wildcard *.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=$(SOURCES:.cpp=)

all: $(SOURCES) $(OBJECTS) $(EXECUTABLE)

$(OBJECTS): $(SOURCES)  
    $(CC) $(CFLAGS) $(@:.o=.cpp) -o $@

$(EXECUTABLE): $(SOURCES)
    $(CC) $(LDFLAGS) $(@:=.o) -o $@

The problem is that once one .cpp file is changed or added into the folder, all other files will be updated and re-compiled if I do make.

Please let me know how to solve this issue.

Thanks.

4 Answers 4

5

Your makefile lists every source file as a prerequisite of every executable and every object file.

It also doesn't like the object files as prerequisites of the executables (which means make can't automatically build them for you).

Those are the problems.

That all being said you don't need the rules in this makefile at all.

A makefile of just

CC=g++
CXXFLAGS=-c -Wall

will let you run make A and build A from A.cpp (though it won't build A.o to do that since it doesn't need to). You don't even need a makefile at all to build A from A.cpp if you don't need to set CC or CXXFLAGS (or don't mind setting them on the command line.

$ mkdir test
$ cd test
$ ls
$ touch A.cpp
$ make CC=g++ CXXFLAGS='-c -Wall' A
g++ -c -Wall    A.cpp   -o A

But if you ask it to make A.o it will.

$ make CC=g++ CXXFLAGS='-c -Wall' A.o
g++ -c -Wall   -c -o A.o A.cpp

At which point make will try to build A from A.o.

$ make CC=g++ CXXFLAGS='-c -Wall' A
g++   A.o   -o A

To get back support for make building all the executables you need a line like:

all: $(subst .cpp,,$(wildcard *.cpp))

If you do want to manually specify everything nonetheless (and force .o building) you want something more like this. (Which uses 10.5 Defining and Redefining Pattern Rules and 4.12 Static Pattern Rules.)

CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=$(wildcard *.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=$(SOURCES:.cpp=)

all: $(EXECUTABLE)

%.o: %.cpp
    $(CC) $(CFLAGS) $^ -o $@

$(EXECUTABLE): % : %.o
    $(CC) $(LDFLAGS) $^ -o $@

Which then lets you write make to build them all and make A to build specific ones.

1

Put .SUFFIXES: .o .cpp in above of all line.

CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=$(wildcard *.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=$(SOURCES:.cpp=)

.SUFFIXES: .o .cpp

all: $(SOURCES) $(OBJECTS) $(EXECUTABLE)

$(OBJECTS): $(SOURCES)  
    $(CC) $(CFLAGS) $(@:.o=.cpp) -o $@

$(EXECUTABLE): $(SOURCES)
    $(CC) $(LDFLAGS) $(@:=.o) -o $@

Additional

Use this

CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=$(wildcard *.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=$(SOURCES:.cpp=)

.SUFFIXES: .o .cpp

all: $(EXECUTABLE)

.cpp.o:
    $(CC) $(CFLAGS) $(@:.o=.cpp) -o $@

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

You don't need to put $(SOURCES) or $(OBJECTS) into all. you must write below

DESTINATION: SOURCE

For about .SUFFIXES and .cpp.o, it's an especially. it works as .SOURCE.DESTINATION:

4
  • No, that doesn't fix the problems here. Jan 20, 2015 at 3:11
  • do you mean header files?
    – mattn
    Jan 20, 2015 at 3:12
  • I mean that $(EXECUTABLE): $(SOURCES) is wrong on a number of levels. $(OBJECTS): $(SOURCES) is also wrong. And those are causing the problem. Jan 20, 2015 at 3:15
  • i'm reading the link gnu.org/software/make/manual/html_node/Suffix-Rules.html which gives me some ideas on the Suffix Rules. Thanks for your help.
    – stcheng
    Jan 20, 2015 at 3:34
1

You only need to change your all: line, and then remove all the other lines that follow it.

CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=$(wildcard *.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=$(SOURCES:.cpp=)

all: $(OBJECTS) $(EXECUTABLE)

If you don't need to create the .o files, then you can also remove $(OBJECTS) from all:, as well as the line that defines it. This removes the need to redefine $(CC) for the link line, but you need to add -Wall to $(CXXFLAGS).

CXXFLAGS=-Wall
LDFLAGS=
SOURCES=$(wildcard *.cpp)
EXECUTABLE=$(SOURCES:.cpp=)

all: $(EXECUTABLE)
3
  • this example only works if the 'std' make initialization file is available. And it does not properly handle changes in header files, nor if the user header files are in a different directory from the current directory. and without a .PHONY: all line, it will complain that it cannot create the 'all' file Jan 20, 2015 at 3:47
  • @user3629249: I grant this is not a general solution to makefiles. It is directed at this particular question.
    – jxh
    Jan 20, 2015 at 3:50
  • .PHONY: all is only necessary to keep make from doing nothing should an all file come into existence. make will not give an error about being unable to create an all file without it... at least in GNU Make 3.81 it is possible newer versions added that sanity check. Jan 20, 2015 at 13:19
1

The two lines giving you grief are:

$(OBJECTS): $(SOURCES)

$(EXECUTABLE): $(SOURCES)

The first says "every object file depends on every one of the source files; if any source file changes, every object file is out of date". The second says the same about executables.

make knows how to build an executable direct from a source file; you don't need the object files as intermediaries.

I'd remove $(OBJECTS) from the all line (since you don't really need the object files).

Then you can remove the lines I identified and the commands that follow them (remove the last four non-blank lines shown in the question).

If you still want to be able to create all the object files, add:

objects: $(OBJECTS)

and use make objects to make them all.

FWIW: The compilation rule to create an object should specify -c directly and not include it in the $(CFLAGS) macro. The compilation rule to build executables can (and should) then use $(CFLAGS) (as well as $(LDFLAGS) and maybe $(LDLIBS) too).

$(OBJECTS):
    $(CC) -c $(CFLAGS) $(@:.o=.cpp) -o $@

$(EXECUTABLE):
    $(CC) -o $@ $(CFLAGS) [email protected] $(LDFLAGS) $(LDLIBS)

The -o $@ in the object compilation line is largely unnecessary, at least in my experience. The C compiler generates $@ anyway. It might matter if the source is not in the current directory, but that isn't the scenario you've got anyway.

And it is generally better to simply use the built-in rules so you don't have to get the arcane incantations like ${@:.o=.cpp} correct. With most versions of make, the built-in rules will be very similar to what I showed (use make -p to find out what they are with your make). And GNU make has the modern rules using %.o: %.cpp notation whereas the code shown does not assume that.

4
  • Good note about the location for the -c flag and use of $(CFLAGS). Use of $* in a static rule always catches me off-guard though (especially since it depends on "recognized suffix"es). Jan 20, 2015 at 13:22
  • @EtanReisner: I think you're probably right; it would be better to use $@ here. Jan 20, 2015 at 15:39
  • [email protected] works for the executable but not for the object file. That needs $(@:.o:.cpp) or similar (since you can't use prerequisite variables as you don't have any). Static pattern rules would help with this. Jan 20, 2015 at 15:53
  • @EtanReisner: Under-caffeination — a deadly state for programming. Doubly so when the code isn't testable (or, at least, isn't tested). Jan 20, 2015 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.