2
votes

I have been having troubles getting my makefiles to work the way I want. First off, I would like to say this is POSIX make, as in http://www.opengroup.org/onlinepubs/009695399/utilities/make.html I am needing my build system to work with both BSDs and GNUs(Linux).

What I am wanting is a zero maintenance makefile. I want it to just compile all .c and .asm files in src/ and place the object files in objs/ and then to link everything in objs/ to a binary file.

I can do a lot, but I can't get it to separate the source and obj files.

I am ok if this requires a little built-in shell scripting (using POSIX defined /bin/sh), but I can just not get the dependencies to work right. I want it to only build the object file if the source file is newer.

My closest is this:

${C_OBJS}: ${HDRS} ${*:objs/%=src/%}.c
    ${CC} ${CFLAGS} -c ${*:objs/%=src/%}.c -o $*.o

This has the problem that I must still specify C_OBJS=objs/foo.o and such and also it is just barely not POSIX and therefore, compiles with BSD make but not GNU make.

2

2 Answers

2
votes

The POSIX version of make does not explicitly support file names with slashes in them, nor does it make provision for separating source files in a different directory from the object files. And, as noted by @caskey, it does not support any notation using '%' characters, though it notes that such rules exist and recommends that they be reserved for use as metacharacters.

Consequently, you probably cannot do what you want with standard POSIX make.

In practice, you can often do what you seek with specific implementations of make, but the resulting makefile has limited portability.

Consider using a makefile generation systems of some sort - cmake or the auto-tools (autoconf, libtool, automake, etc). Or one of the many reworkings of the basic concepts of make:

  • scons
  • ant
  • cake
  • cook
  • bras
  • ...and a dozen I've forgotten or not heard of...
1
votes

POSIX make doesn't support constructs like?

  objs/%.o : src/%.c
    ${CC} ${CFLAGS} -c $< -o $@

Forgot the question mark at the end, hope that makes my comment more clear.