Implicit rules are stored in the make database, so we can use the following expression to get the implicit rule for .c targets:
make --print-data-base -f /dev/null 2>/dev/null |grep -A 6 -e '^\.c:'|grep -v -e '^#'
Read the command above like this:
"Print the database, use /dev/null as (dummy) Makefile, ignore errors.
Grep the line starting with .c (the .c implicit rule), and the following 6 lines.
Ignore lines which are comments."
This will display:
.c:
$(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@
In a similar manner, you can get the default variable definitions from the make database:
make --print-data-base -f /dev/null 2>/dev/null |grep -e '^LINK.c '
Get the definition (if any) for each variable in the implicit rule:
make -p -f /dev/null|grep -E '^CC|^CFLAGS|^LDFLAGS|^.IMPSRC|^LDLIBS|^.TARGET'
As we can see, not all variables are defined in the database...
LDFLAGS =
CFLAGS = -O2 -fno-strict-aliasing -pipe ${_CPUCFLAGS}
CC = cc
.TARGETS =
make -p -f /dev/null|grep -E '^_CPUCFLAGS'
... and other variables are empty by default:
_CPUCFLAGS =
Note: On FreeBSD, if you prefer GNU make, it's is available as the gmake command.
Using AIX maketop AIX make has some quirks to be aware of. AIX make prints its database to stderr by default, and grep on AIX does not support the -B (Before) and -A (After) flags, so to display the implicit rule for .c targets we have to use the following command (tested on AIX 5.2):
Here we see that the rule definitions also differ from GNU make:
.c :
$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
Exercises:
Get the value for each variable in the implicit .c rule using commands similar to the one above, using GNU make.
Create an empty directory. Create a simple C source file with the .c extension. Play around with variables from the command line to understand how the implicit rules and associated variables work, for example:
make CFLAGS='-g -O2' LDFLAGS=-static your-program-without-c-extension