List Info

Thread: CFLAGS dissappear during creation of objects




CFLAGS dissappear during creation of objects
country flaguser name
Germany
2008-04-09 06:40:44
Hi,

I have observed a strange behaviour of make as i tried to
create objects from 3 source files and bind them together to
an executable. My in-Makefile-defined CFLAGS variable
dissappear 
after the creation of first object. Here is  my Makefile:


CC=g++
NAME=processor
CFLAGS=-g -Wall
LIB=-lccrtp1
OBJECTS := $(patsubst %.cpp,%.o,$(wildcard *.cpp))

%.o : %.cpp %.h
        $(CC) -c $(CFLAGS) $< -o $

all: $(OBJECTS)
        $(CC)   -o processor $(CFLAGS) $(LIB) $(OBJECTS)  


as i do a make i get this:

g++ -c -g -Wall AudioRecorder.cpp -o AudioRecorder.o
g++    -c -o dmain.o dmain.cpp                              
               // -g -Wall dissappeared ??
g++    -c -o processor.o processor.cpp                      
              // -g -Wall dissappeared ??
g++   -o processor -g -Wall -lccrtp1 AudioRecorder.o dmain.o
processor.o 

compiler info:
g++ (GCC) 4.1.2 (Gentoo 4.1.2 p1.0.2)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying
conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.


What causes this behaviour? sth missing in my Makefile?

Thanx in advance,









_______________________________________________
Help-make mailing list
Help-makegnu.org
http:
//lists.gnu.org/mailman/listinfo/help-make

Re: CFLAGS dissappear during creation of objects
user name
2008-04-09 07:18:48
On Wed, 2008-04-09 at 13:40 +0200, j.s.bacho2online.de wrote:
> I have observed a strange behaviour of make as i tried
to create
> objects from 3 source files and bind them together to
an executable.
> My in-Makefile-defined CFLAGS variable dissappear 
> after the creation of first object. Here is  my
Makefile:

They didn't disappear.

> CC=g++
> NAME=processor
> CFLAGS=-g -Wall
> LIB=-lccrtp1
> OBJECTS := $(patsubst %.cpp,%.o,$(wildcard *.cpp))
> 
> %.o : %.cpp %.h
>         $(CC) -c $(CFLAGS) $< -o $

This rule tells make "if you have xyz.cpp AND xyz.h,
you can use this
recipe containing CFLAGS to build xyz.o".

So presumably when you build AudioRecorder.o, you do have
both
AudioRecorder.cpp and AudioRecorder.h and so make uses your
rule.

When you build dmain.o and processor.o, you don't have both
dmain.cpp
and dmain.h and processor.cpp and processor.h respectively,
and so make
cannot use your rule because it doesn't match.  Instead,
make uses the
default rule that it has built-in that knows how to build a
.o from
a .cpp.

However, THAT rule doesn't use CFLAGS, because CFLAGS is a
variable that
is supposed to contain compiler flags specific to C
compilers.  Make's
internal rules use CXXFLAGS to hold flags that are specific
to C++
compilers, so when make runs its own internal rule to
compile a C++ file
like dmain.cpp into dmain.o, it uses CXXFLAGS not CFLAGS.


So, you have a number of problems.  First, your pattern rule
should NOT
contain "%.h", unless you are committed to always
having a foo.h file
for every single foo.cpp file.

Second, you should be using the de facto standards for
variable naming,
which means that since you're compiling C++ files you should
be using
CXXFLAGS not CFLAGS.  If you check the GNU make manual
you'll find a
list of the commonly used built-in rules and variables.  Use
those.

If you do that you can actually remove your pattern rule
entirely and
depend on make's built-in rules instead.

-- 
------------------------------------------------------------
-------------------
 Paul D. Smith <psmithgnu.org>          Find
some GNU make tips at:
 http://www.gnu.org        
             http://make.mad-scientis
t.us
 "Please remain calm...I may be mad, but I am a
professional." --Mad Scientist


_______________________________________________
Help-make mailing list
Help-makegnu.org
http:
//lists.gnu.org/mailman/listinfo/help-make

Re: CFLAGS dissappear during creation of objects
country flaguser name
United States
2008-04-09 08:01:18
j.s.bacho2online.de wrote:

> %.o : %.cpp %.h
>         $(CC) -c $(CFLAGS) $< -o $
> 
> all: $(OBJECTS)
>         $(CC)   -o processor $(CFLAGS) $(LIB)
$(OBJECTS)
> 
> as i do a make i get this:
> 
> g++ -c -g -Wall AudioRecorder.cpp -o AudioRecorder.o
> g++    -c -o dmain.o dmain.cpp                         
                    // -g -Wall dissappeared ??
> g++    -c -o processor.o processor.cpp                 
                   // -g -Wall dissappeared ??
> g++   -o processor -g -Wall -lccrtp1 AudioRecorder.o
dmain.o processor.o

By any chance, is it the case that you have a file
AudioRecorder.h but
no dmain.h nor processor.h?  If so, that is why.

In other words, when you write "%.o : %.cpp %.h",
the rule can only
apply if all prerequisites exist or can be made.  If %.cpp
exists but
%.h does not, this rule won't match and instead an implicit
rule from
the built-in database will match.  This explains also why
CFLAGS isn't
used -- the built-in rules use the convention of CXXFLAGS
when compiling
C++ files and CFLAGS when compiling C files.  See the manual
for
details.

Those built-in rules are there to help you, not something
you have to
work around.  They are meant to avoid you having to give the
boring
compile/link recipies in every Makefile.  Using them you
should be able
to write your Makefile as just:

CXX=g++
CXXFLAGS=-g -Wall
LDLIBS=-lccrtp1
sources := $(wildcard *.cpp)

processor: $(sources:.cpp=.o)
	$(LINK.cpp) $^ $(LDLIBS) -o $

AudioRecorder.cpp: AudioRecorder.h

Brian


_______________________________________________
Help-make mailing list
Help-makegnu.org
http:
//lists.gnu.org/mailman/listinfo/help-make

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )