1

I have a gnu makefile template that has served me well, but when I try to specify a compiler other than the first g++ in my path, it fails.

Here's the template.

CXX = g++
CXXFLAGS = $(INC) $(LIB) -Wall
INC = -I./ -I/usr/local/include
LIB = -L/usr/local/lib
SRCS = \
blah1.cpp
blah2.cpp

OBJS = $(SRCS:.cpp=.o)
DEPS = $(SRCS:.cpp=.d)
PROG = myprog

$(PROG):    $(OBJS)
    $(CXX) $(CXXFLAGS) -o $@ $(OBJS)

%.d: %.cpp
    @set -e; rm -f $@; \
    $(CXX) -MM $(CXXFLAGS) $< > $@.$$$$; \
    sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
    rm -f $@.$$$$

debug: CXXFLAGS += -O0 -DDEBUG -ggdb
debug: $(PROG)

-include $(DEPS)

.PHONY: clean
clean:
    rm -f $(DEPS) $(OBJS) $(PROG)

When I change the compiler from g++ to something like /usr/local/bin/g++46, it still compiles with g++ (/usr/bin/g++ to be exact). Why?

P.S. Any criticisms with the template are welcome. I'm not very comfortable with gnu make; I just crammed and searched the web for a day to come up with this.

4

1 回答 1

5

You haven't specified your own rule for how to build object files, so Make uses the default implicit rule, which is:

$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c

Note $(CXX), not $(CC), which specifies the default C compiler.

于 2012-06-26T17:17:33.120 回答