tags:

views:

46

answers:

3

I'm trying to write a makefile using CC on Solaris 10. [Only the first bit of that really matters, I think]. I have the following rule for foo.o:

foo.o: foo.cc common_dependencies.h
    CC -c foo.cc -I../../common

Unfortunately, common_dependencies.h includes all sorts of idiosyncratic trash, in directories not named '.' or '../../common' . Is this just going to have to be a brute force makefile where I ferret out all of the dependency paths? All of the dependencies are somewhere under '../..', but sometimes 1-level down and sometimes 2-levels down.

-Thanks Neil

A: 

Typically, if header files are shared amongst source files in multiple directories, they are kept in an include/ tree under the project root, and then you use something like -I$(TOPDIR)/include to add the include tree to the include path and let your source files pull in include/foo/bar.h like so:

#include "foo/bar.h"
SamB
This may be typical, but it doesn't hold in the case David describes.
Beta
Actually, it is OK. We are, uhh, 'reusing' some legacy stuff which was horribly disorganized. We haven't baselined it, though, so I took the liberty to make a local copy [it's not very big when it is all said and done] and reorganize it. So, I'm just going to do a little more housework.
David
A: 

This problem is pretty mean in general. There are plenty of conventions for project organization plus tools to help (autoconf and similar), but if the author hasn't taken advantage of those things you're going to have to hunt everything down and install the -Is yourself.

You could use gcc -M or makedepends or one of the many similar tools to get a list of the directories that are needed. Presumably you could even script the extraction.

dmckee
I used CC -xM1, which is the idiomatic usage, but it dumps the cant-finds to STDERR (i.e. CC -xM1 foo.cc > dependencies) gets all the includes it can find, and doesn't get any of the things that can't be found. I could python it it up, which would be the most natural for me, but it might take an hour, and I just wanted to get a community check before I took off on a tangent.
David
Thanks, though, I appreciate the feedback.
David
A: 

It's really not a good idea to specify a path in an #include directive; it makes the header -- and anything that depends on it -- dependent on a particular directory structure. It's worth finding out whether you're allowed to clean up common_dependencies.h itself.

If the paths in common_dependencies.h are correct, then your Makefile will work as is (although I'd recommend using an absolute path, not "../.."), and you can use somthing like Advanced Auto-Dependency Generation to handle the dependencies for you.

If the paths in common_dependencies.h are not correct, then yes, you'll have to find every file that it #includes, and knowing where they are won't be enough. If it #includes a file with a false path, the compilation will crash even if the compiler has -Ithe_correct_path, so you'll have to either fix common_dependencies.h or modify the directory structure (by moving, copying or linking to the files).

Beta
This lump of code started life as a blob in a single directory, without so much as a whiff of unit testing, with revision histories that are not current [last change '94, except for //B.Lah's debug statement 12/01, for example]. Makes finding dependencies easy, they are all part of the lump, but it confounds most quality practices. I'm hoping to do make some organizational changes to affect the latter. [And this may include refactoring common_dependencies.h, because it is seriously hosed]. Thanks, advice taken and recommendation recognized.
David