Sporatically when attempting to compile a C++ program I've been writing on GCC 4.1.1 I recieve an undefined reference to 'main' which seems to have nothing to do with my source code. The following two compiles were completed sequentially (the source code wasn't touched, no other programs were used). stephen@sb03li /mnt/sandcastle/COSC5P71 $ make clean rm *.o stephen@sb03li /mnt/sandcastle/COSC5P71 $ make "CFLAGS= -Wall -O0 -g" g++ -Wall -O0 -g -c Chromosome.cpp g++ -Wall -O0 -g -c Random.cpp g++ -Wall -O0 -g Chromosome.o Random.o -o kakuro /usr/lib/gcc/i686-pc-linux-gnu/4.1.1/../../../crt1.o: In function `_start': init.c:(.text+0x18): undefined reference to `main' collect2: ld returned 1 exit status make: *** [kakuro] Error 1 stephen@sb03li /mnt/sandcastle/COSC5P71 $ make clean rm *.o stephen@sb03li /mnt/sandcastle/COSC5P71 $ make "CFLAGS= -Wall -O0 -g" g++ -Wall -O0 -g -c Chromosome.cpp g++ -Wall -O0 -g -c Random.cpp g++ -Wall -O0 -g Kakuro.cpp Chromosome.o Random.o -o kakuro stephen@sb03li /mnt/sandcastle/COSC5P71 $ If only the executeable is deleted the compile seems to always succeed regardless of wether the object files were from a failed or successful compile, and optimization flags don't appear to make a difference. My make file: CC=g++ CFLAGS= -O2 all: kakuro kakuro: Kakuro.cpp Chromosome.o Random.o $(CC) $(CFLAGS) $? -o $@ Chromosome.o: Chromosome.cpp $(CC) $(CFLAGS) -c $? Random.o: Random.cpp $(CC) $(CFLAGS) -c $? clean: rm *.o This is on a Pentium 4 machine.
Looking more carefully, every other time Kakuro.cpp is not included in the $?. This only happens when the binary already exists, so the problem can be worked around by adding rm kakuro to make clean. However, when the binary exists it happens every other time, which seems like unusual and undesirable behavior.
http://www.gnu.org/software/make/manual/make.html#Automatic-Variables $? The names of all the prerequisites that are newer than the target, with spaces between them. For prerequisites which are archive members, only the member named is used (see Archives).