Board index » cppbuilder » Header file dependencies and gnu make

Header file dependencies and gnu make


2004-01-29 02:13:35 AM
cppbuilder76
Hi,
I'm trying to figure out an easy way to list the header file
dependencies for my source files so I can list them in my makefile. I am
using gnu make so I can't use .autodepend as with borland make. Anyone
any ideas?
Is there a witch that can generate this info for me? I couldn't find one...
As an eaxmple, I have a file called MainUnit.cpp, it depends on
MainUnit.h, plus a host of other header files. I want to be able to
determine this automatically so I can then list the headers as
dependencies for my MainUnit.obj.
Any help would be greatly appreciated.
Jamie
 
 

Re:Header file dependencies and gnu make

Hallo, Jamie.
Jamie Allsop wrote:
Quote
Hi,

I'm trying to figure out an easy way to list the header file
dependencies for my source files so I can list them in my makefile. I am
using gnu make so I can't use .autodepend as with borland make. Anyone
any ideas?

Is there a witch that can generate this info for me? I couldn't find one...
gcc itself. It should be something like -M or -MM. If you browse around
the gnu.org site you can find downloadable HTML documentation.
Cheers,
--
Nicola Musatti
Team Thai Kingdom
 

Re:Header file dependencies and gnu make

Hi Nicola,
Long time no see :)
Yeh I saw I could use GCC with -M or -MM (forget the difference, but I
remember reading it in the make manual). I also found this link which
was good:
make.paulandlesley.org/autodep.html
What I have done though is to write a small commandline program that
reads the dependencies listed at the start of each object file. Haven't
quite finished but it looks promising. Its only a few lines of code. THe
idea is to generate something similar to that described in the above link.
Thanks for the reply :) I think I will try using GCC also to see which
way is easier (probably GCC).
Thanks,
Jamie
Nicola Musatti wrote:
Quote
Hallo, Jamie.

Jamie Allsop wrote:

>Hi,
>
>I'm trying to figure out an easy way to list the header file
>dependencies for my source files so I can list them in my makefile. I
>am using gnu make so I can't use .autodepend as with borland make.
>Anyone any ideas?
>
>Is there a witch that can generate this info for me? I couldn't find
>one...


gcc itself. It should be something like -M or -MM. If you browse around
the gnu.org site you can find downloadable HTML documentation.

Cheers,
 

{smallsort}

Re:Header file dependencies and gnu make

A quick update:
Quote
What I have done though is to write a small commandline program that
reads the dependencies listed at the start of each object file. Haven't
quite finished but it looks promising. Its only a few lines of code. THe
idea is to generate something similar to that described in the above link.
I used a small program called DependenciesFromOMF to read each object
file and create a makefile *.d containing the dependencies. I then
-include'ed the files and now changing a dependency forces an update as
expected. I am sure there might be something I have not done exactly
right but all appears to work as it should now.
I also tried using GCC very quickly but was not very successful. I think
I need to spend more time with that solution so for now I'm going with
reading the dependencies directly from the .obj files after the compiler
has done its work :)
Just thought I'd post in case anyone else is trying to use GNU make and
is facing similar problems...
Jamie
 

Re:Header file dependencies and gnu make

On Fri, 30 Jan 2004 16:05:10 +0000, Jamie Allsop
< XXXX@XXXXX.COM >wrote:
Quote
A quick update:

>What I have done though is to write a small commandline program that
>reads the dependencies listed at the start of each object file. Haven't
>quite finished but it looks promising. Its only a few lines of code.
>THe idea is to generate something similar to that described in the
>above link.

I used a small program called DependenciesFromOMF to read each object
file and create a makefile *.d containing the dependencies. I then
-include'ed the files and now changing a dependency forces an update as
expected. I am sure there might be something I have not done exactly
right but all appears to work as it should now.

I also tried using GCC very quickly but was not very successful. I think
I need to spend more time with that solution so for now I'm going with
reading the dependencies directly from the .obj files after the compiler
has done its work :)

Just thought I'd post in case anyone else is trying to use GNU make and
is facing similar problems...

Jamie
I've also tinkered with the GCC way a little. If you copy/paste the
make-code from the documentation, it will work only in specific
circumstances, i.e. all your source files must reside in the same
directory. If your source files are spread out over several directories,
it will fail miserably. That was simply not good enough for me.
I analyzed the make-code (quite a tedious task, considering it's extreme
terseness) to figure out what could be done, and finally ended up with
this (OBJS should be a list of all relevant .o-files):
# Automatic dependency generation
#
%.d: %.cpp
@echo \
Remaking dependencies for $<
@set -e; $(CXX) -MM $(CXXFLAGS) $< \
| sed 's/\($(subst /,\/,$(*F))\)\.o[ :]*/\$(subst /,\/,$*).o $(subst
/,\/,$@) : /g'>$@; \
[ -s $@ ] || rm -f $@
#include $(OBJS:.o=.d)
It seems to work just fine, though I'm sure someone can come up with a
simple way to break it. It was enough for me at the time, so perhaps it
might be of some use to someone else.
 

Re:Header file dependencies and gnu make

Quote
I used a small program called DependenciesFromOMF to read each object
file and create a makefile *.d containing the dependencies. I then
-include'ed the files and now changing a dependency forces an update
as expected. I am sure there might be something I have not done
exactly right but all appears to work as it should now.
bcc will geneate those directly now, with -md and -mmd, which are
equivalent to gcc's -MMD and -MD, respectively. [yes, that's right,
borland -md == gcc -MMD ...]
Quote
I also tried using GCC very quickly but was not very successful. I
think I need to spend more time with that solution so for now I'm
going with reading the dependencies directly from the .obj files after
the compiler has done its work :)
there's no good reason to use -M or -MM most of the time, add the D to
get the dependency generated at compile time and avoid a second pass.
unfortunately, older gcc's would{*word*222}up the dependecy and leave off
the output directory. around were, we tweak it with a perl snippet:
DEPGEN = $(PERL) -pi -e 'if ($$. == 1) { s,^.*:,$@:, }' $(@:$(OBJEXT)=.d)
%.o: %.c
$(CC) blah blah
$(DEPGEN)
newer gcc has a new flag to do the right thing, i'm not sure what it is.
Quote

Just thought I'd post in case anyone else is trying to use GNU make
and is facing similar problems...
that leaves one final problem that borland's autodeps don't have: if
you rename or delete a header file, the next build will break because
make will be looking for a file that no longer exists. be sure to
delete the dependencies in your clean rule, and on unix consider using
ccache to speed up compiles. ccache.samba.org