|
From: | John Graham-Cumming |
Subject: | Re: What is wrong with this simple rule in makefile? |
Date: | Thu, 27 Apr 2006 15:57:58 +0200 |
User-agent: | Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040208 Thunderbird/0.5 Mnenhy/0.6.0.104 |
Lin George wrote:
I want to use g++ $(CFLAGS) to compile any .cpp files into .o files. For example, using g++ $(CFLAGS) foo.cpp to generate foo.o, using g++ $(CFLAGS) goo.cpp to generate goo.o. CFLAGS is a variale which I defined before. I write a rule in Makefile as, %.o : g++ $(CFLAGS) %.cpp But when executing the Makefile, it reports error message "g++: %.cpp: No such file or directory".
There are two important things wrong with this rule:1. You've specified no prerequisite so this rule means "in order to build any file ending in .o do this". That could be incorrect if there were .o's that were not built from .cpp files as you intend.
2. You can't say %.cpp in the rule body. That's the wrong syntax. What you need to do is use the automatic variable $<.
Here's the correct rule: %.o : %.cpp g++ $(CFLAGS) $<You probably also want to set a bunch of other g++ options (such as -o), but that's a bit out of the range of what you asked.
The other question I would have is why you need to define this rule at all. Don't GNU Make's built-in rules do what you need? GNU Make has the following as a built-in:
%.o: %.cpp $(COMPILE.cpp) $(OUTPUT_OPTION) $<where COMPILE.cpp = $(COMPILE.cc) and COMPILE.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c and CXX = g++.
So you could use GNU Make's built-in rule and just do CXXFLAGS += $(CFLAGS) somewhere in your Makefile and you'd be golden.
John.
[Prev in Thread] | Current Thread | [Next in Thread] |