How to create "universal" makefile? 

Newsgroups: gnu.gcc.help,comp.os.linux.misc
Date: Thu, 13 Mar 2003 18:58:36 -0500
> On my UNIX box I create simple and not so simple c++ programs which use only
> standard library. Each program has only one source file called main.cpp
> which includes standard library headers and my own headers.
>
> Can I create an universal makefile which would work with all my programs and
> which would automatically generate list of included header files and check
> them (so I'll be able no to add each new included header file manually)?

Yes. There's an example somewhere in the texinfo docs for GNU make.

How to create "universal" makefile? 

The "makedepend" program can figure out which header files your program needs. See "man makedepend". The following should come pretty close…

PROGRAM=sample

all: depend $(PROGRAM)

depend:
        makedepend main.cpp

$(PROGRAM): main.o

clean:
        rm main.o

Steve Kirkendall

How to create "universal" makefile? 

> Can I create an universal makefile which would work with all my

You don't even need a makefile. A file named main.cpp can be turned into an executable called main using the command `make main`, so when I do Computer Science projects, my makefiles usually consist of setting the compiler flags, telling make what programs are part of "all", and creating a cleanup rule.

[bloom@kabloom lab1]% ls -l
total 160
-rwxrwxr-x    1 bloom    bloom       15958 Jan 17 14:30 execer*
-rw-rw-r--    1 bloom    bloom         153 Jan 14 17:29 execer.cpp
-rw-rw-r--    1 bloom    bloom         132 Jan 22 12:08 makefile
-rwxrwxr-x    1 bloom    bloom       31697 Jan 21 17:18 myls*
-rw-rw-r--    1 bloom    bloom        2097 Jan 21 17:18 myls.cpp
-rwxrwxr-x    1 bloom    bloom       40554 Jan 21 15:34 mysh*
-rw-rw-r--    1 bloom    bloom        2149 Jan 21 15:34 mysh.cpp
-rwxrwxr-x    1 bloom    bloom       26029 Jan 17 14:30 mysplit*
-rw-rw-r--    1 bloom    bloom         920 Jan 13 12:28 mysplit.cpp
-rwxrwxr-x    1 bloom    bloom       18466 Jan 17 14:30 tester*
-rw-rw-r--    1 bloom    bloom         942 Jan 22 12:08 tester.cpp

[bloom@kabloom lab1]% cat makefile
CFLAGS=-g
CXXFLAGS=$(CFLAGS)

all: execer myls mysh mysplit tester

distclean:
        rm -f execer myls mysh mysplit tester core core.*

More information on this cool feature is available in the GNU Make texinfo manual (`info make`)

Ken Bloom