Page 1 of 1

Error with osdev.org Makefile

Posted: Thu Oct 18, 2012 12:55 pm
by BootComp
Hi,

First off I would like to thank the author of the Makefile article in the wiki http://wiki.osdev.org/Makefile. I am using this as a reference for my own makefile for a little project I have just started (not OS development). Here is the makefile,

Code: Select all

CC := gcc
RM := rm
CFLAGS := -Iinclude/ -g -std=c99 -Wall
RMFLAGS := -rf
EXT_LIBS := -lc -lssl
PROJDIRS := dir1 dir2
SRCFILES := $(shell find $(PROJDIRS) -type f -name "*.c")
HDRFILES := $(shell find $(PROJDIRS) -type f -name "*.h")
OBJFILES := $(patsubst %.c,%.o,$(SRCFILES))
DEPFILES := $(patsubst %.c,%.d,$(SRCFILES))
EXECUTABLE := my_exec

.PHONY: all clean

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJFILES)
	@$(CC) -o $(EXECUTABLE) $? $(EXT_LIBS)

%.o: %.c Makefile
	@$(CC) $(CFLAGS) -MD -MP -c $< -o $@

-include $(DEPFILES)

clean:
	-@$(RM) $(RMFLAGS) $(OBJFILES) $(DEPFILES) $(EXECUTABLE)
However I noticed that if I execute make the second time (without a clean and just some minor file source code modifications) I get the following error (I need to do a clean and make again to get a proper compile),

Code: Select all

/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [my_exec] Error 1
I think this is most probably because of an error in my Makefile (constructed with my limited understanding of the wiki article), and would really appreciate any help from the folks here in figuring out what I am missing here.

TIA

Re: Error with osdev.org Makefile

Posted: Thu Oct 18, 2012 1:37 pm
by solano
%.c is not a single prerequisite. It expands to "abc.c file.c another.c whatever.c", so $< will expand to abc.c (or the first file of the list). To include all the prerequisites, use $^ instead. That will also include the prerequisite Makefile (then I sugest you remove it).

Re: Error with osdev.org Makefile

Posted: Thu Oct 18, 2012 4:12 pm
by BootComp
Thanks. That fixed it and all working now. :)

Re: Error with osdev.org Makefile

Posted: Thu Oct 18, 2012 7:12 pm
by thepowersgang
Actually, that's wrong. The %.o target is correct, what is incorrect is the use of '$?' in the $(EXECUTABLE) target. $(OBJFILES) or $^ should be used instead. ($? returns the changed prerequisites, while $^ returns all)

Re: Error with osdev.org Makefile

Posted: Fri Oct 19, 2012 9:06 am
by solano
Sorry, I confused them. What I don't know now is how it worked. :mrgreen:

Re: Error with osdev.org Makefile

Posted: Fri Oct 19, 2012 11:45 am
by gravaera
Yo:

I recommend you read the GNU Make manual, because it is very clear, and it explains everything you need to know about GNU Make. It is the only documentation I ever read on Make, and being as complete as it is, it is probably the only documentation you need.

--Peace out
gravaera