I’m working on my next cool project (subscribe to RTFMS.com to find out details) and got stuck with an interesting problem. Here is a fragment of my Makefile:
...
SOURCES := $(wildcard *.c)
...
%.o: %.c %.h
$(I_CC) $(I_CCFLAGS) -c $<
...
When I run make clean;make the output is following:
gcc -g -I. -I/usr/include -c 1.c
gcc -g -I. -I/usr/include -c 2.c
cc -c -o main.o main.c
gcc -g -I. -I/usr/include -c 3.c
gcc -g -I. -I/usr/include -c 4.c
gcc -g -I. -I/usr/include -c 5.c
gcc -g -I. -I/usr/include -o gr 1.o 2.o 3.o 4.o 5.o main.o -llib1 -llib2
See how for all the files this invokes compiler with correct flags except for main.c? Not a big deal on most linux systems, but I'm compiling on mac and what's worst I'm actually crosscompiling. This ugly feature ruins the entire compilation process, so I have to run
gcc -g -I. -I/usr/include -c main.c
before running make so everything gets compiled the same way.
This kept me puzzled for an hour or so until I got annoyed by this manual compilation step and found what's going on ...
Apparently the problem was in the
%.o: %.c %.h
part of the Makefile. Since I had no header file for main.c (who would need one?!) instead of invoking my compiler with all the right flags the default compiler got invoked. You can specify the default compiler by setting CC environment variable (export CC=/usr/bin/gcc), but you loose the flags anyway.
You can fix the Makefile, but I just did "touch main.h" and that solved the problem, now everything compiles right way. Again, subscribe to RTFMS.com to find out what's cooking in my video blog :)