makefile help

Skip to first unread message

John Small

Jun 3, 2011, 8:29:16 AM6/3/11
I am trying to help someone transition the build of his software from
a large set of inter-connected CMD files to an NMAKE v4.00.001

I have succeeded in doing so as far as I can tell. But I'm hoping to
simplify the makefile.

There are 50 nearly identical description bloacks in the make file.
The code specifies an OBJ target file, the C source file and other
dependent (header) files and the commands to execute. All 50 sets of
code are the same in the following ways:
- the base names of the OBJ and C files match
- the directories of all the OBJ files are all the same
- the directories of all the C files are all the same
- the commands to be executed, because of my use of special macros,
are all identical.
IOW, the only difference between them are the dependent header files.

Since the commands are 11 lines long, I am hoping there are ways I can
condense this code.

I've considered
- moving the 11 lines into an !include file
- moving the 11 lines into a secondary makefile
- moving the 11 lines into a CMD file

What are the advantages and disadvantages of each? (I'm especially
interested in how values of macros would be "passed" to the code and
how return values, especially errors, are returned.) Are there better
ideas than these?

These ideas would only shorten the command portion of the 50
description blocks. Is there some way to reduce the number of
description blocks?

On a related note, the list of dependent header files in at least some
of these description blocks are out of date. What are good ways to
handle this?

A brute force way would be: If a header file changes, just do a
complete rebuild. Although crude, time-consuing and requiring
discipline on the part of the maintainer, it would have the advantage
of not requiring the header files in the dependents list. This would
mean, I think, that I could consense the 50 description blocks into 1!

Are there better approaches?

Are there any tools which will resursively build a list of "private"
header files used by a source file?

Thanks for any help on either issue!


John Small

Dave Yeo

Jun 3, 2011, 10:35:05 PM6/3/11
John Small wrote:
> Are there any tools which will resursively build a list of "private"
> header files used by a source file?

There's makedepend.exe which came with XFree86 and probably other
sources. From the man page,

The makedepend program reads each sourcefile in sequence
and parses it like a C-preprocessor, processing all
#include, #define, #undef, #ifdef, #ifndef, #endif, #if,
#elif and #else directives so that it can correctly tell
which #include, directives would be used in a compilation.
Any #include, directives can reference files having other
#include directives, and parsing will occur in these files
as well.
Every file that a sourcefile includes, directly or indi-
rectly, is what makedepend calls a dependency. These
dependencies are then written to a makefile in such a way
that make(1) will know which object files must be recom-
piled when a dependency has changed.

By default, makedepend places its output in the file named
makefile if it exists, otherwise Makefile. An alternate
makefile may be specified with the -f option. It first
searches the makefile for the line

# DO NOT DELETE THIS LINE -- make depend depends on

or one provided with the -s option, as a delimiter for the
dependency output. If it finds it, it will delete every-
thing following this to the end of the makefile and put
the output after this line. If it doesn't find it, the
program will append the string to the end of the makefile
and place the output following that. For each sourcefile
appearing on the command line, makedepend puts lines in
the makefile of the form
sourcefile.o: dfile ...

Where sourcefile.o is the name from the command line with
its suffix replaced with ``.o'', and dfile is a dependency
discovered in a #include directive while parsing source-
file or one of the files it included.
Normally, makedepend will be used in a makefile target so
that typing ``make depend'' will bring the dependencies up
to date for the makefile. For example,
SRCS = file1.c file2.c ...

This probably works best with Gnu make. The other stuff you mentioned
might also be easier to implement with Gnu make as well.

Paul Ratcliffe

Jun 4, 2011, 5:35:22 AM6/4/11
On Fri, 03 Jun 2011 07:29:16 -0500, John Small <> wrote:

> Are there any tools which will resursively build a list of "private"
> header files used by a source file?

XWP/WarpIN use a tool called Fastdep, written by Knut St. Osmundsen which
generates a .depend dependency file.
You just !include this in your main makefile having removed any .h
dependencies from the ".obj: .c" list.

It's available as fastdep.exe at the trunk root level of the XWPHELPERS
CVS repository.

Reply all
Reply to author
0 new messages