Discussion:
About g++ option -MM
Peng Yu
2006-01-26 02:10:39 UTC
Permalink
Suppose I have a file "main.cc", if I run the following command "g++
-MM main.cc, I end up with following rule "main.o: main.cc main.h".

What if I want something like the following contents? Basically, what
I want are two sets of objects code. One is with compiler option "-g",
the other is with option "-O3".

Is it possible to do it in g++, or I have to do some scripting like using perl.
####################
main-g.o: main.cc main.h
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -g -c -o $@ $<

main-o.o: main.cc main.h
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -O3 -c -o $@ $<
####################

Thanks,
Peng
Dima Sorkin
2006-01-26 08:26:40 UTC
Permalink
Post by Peng Yu
Suppose I have a file "main.cc", if I run the following command "g++
-MM main.cc, I end up with following rule "main.o: main.cc main.h".
What if I want something like the following contents?
####################
main-g.o: main.cc main.h
main-o.o: main.cc main.h
####################
Hi.
This is not GCC question, but MAKE question ( GNU-MAKE ).
You don't need to write these lines, MAKE performs the calls automatically.
You only change the CXXFLAGS (and etc.) variables, depending on what
compiling options you want.

Example for makefile:
--------------------------------------------- Makefile:
If ($(debug_mode),on)
CXXFLAGS := $(TARGET_ARCH) -g
else
....
end
include gcc_generated_dependencies.make

-------------------------------------------------
gcc_generated_dependencies.make:
# these lines generated with g++
main.o : main.c main.h
-------------------------------------------------

I don't sure I use the correct MAKE syntax. Please read it's docs.

Regards,
Dima.
Peng Yu
2006-01-27 23:37:58 UTC
Permalink
Post by Dima Sorkin
Post by Peng Yu
Suppose I have a file "main.cc", if I run the following command "g++
-MM main.cc, I end up with following rule "main.o: main.cc main.h".
What if I want something like the following contents?
####################
main-g.o: main.cc main.h
main-o.o: main.cc main.h
####################
Hi.
This is not GCC question, but MAKE question ( GNU-MAKE ).
You don't need to write these lines, MAKE performs the calls automatically.
You only change the CXXFLAGS (and etc.) variables, depending on what
compiling options you want.
If ($(debug_mode),on)
CXXFLAGS := $(TARGET_ARCH) -g
else
....
end
include gcc_generated_dependencies.make
-------------------------------------------------
# these lines generated with g++
main.o : main.c main.h
-------------------------------------------------
I don't sure I use the correct MAKE syntax. Please read it's docs.
The above method will replace old version ones with new version ones.
What if I want to keep both versions like main-g.o and main-o.o?

Thanks,
Peng
Dima Sorkin
2006-01-29 07:27:55 UTC
Permalink
Post by Peng Yu
What if I want to keep both versions like main-g.o and main-o.o?
This can be a litle tricky: implicit rules may be used, something like this:
%-g.o : %.cc
$(COMPILE.c) -o $@ -c $<
%-o.o : %.cc
....

Dima.
Dima Sorkin
2006-01-29 08:09:06 UTC
Permalink
Post by Peng Yu
The above method will replace old version ones with new version ones.
What if I want to keep both versions like main-g.o and main-o.o?
This can be a litle tricky: implicit rules may be used, something like this:
%-g.o : %.cc
$(COMPILE.c) -o $@ -c $<
%-o.o : %.cc
....

The sufficient enough scheme can be much more trickier. I simlify it a lot.
( Nevertheless, you may want to read:
http://make.paulandlesley.org/autodep.html and
http://aegis.sourceforge.net/auug97.pdf )

So, depending on whether you are in debug mode, you generate uniform
suffix to your objects, usually it will be .g.o - debug, .o - optimized:
SUFF :=

for dependency generation you define the following implicit rule:
%.d : %.cc
g++ -MM -MT $<$SUFF $(CPPFLAGS) $(CXXFLAGS) $< > $@

and you include all *.d files that are created:
include $(MY_SRC_LIST: .cc=.d)

Dima.

Loading...