Error with osdev.org Makefile

Programming, for all ages and all languages.
Post Reply
BootComp
Posts: 2
Joined: Thu Oct 18, 2012 12:46 pm

Error with osdev.org Makefile

Post 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
solano
Posts: 8
Joined: Mon Oct 01, 2012 12:29 pm
Location: Brazil

Re: Error with osdev.org Makefile

Post 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).
Caspian Project developer
Brazilian OSDev'er. Sorry for the terrible English!
BootComp
Posts: 2
Joined: Thu Oct 18, 2012 12:46 pm

Re: Error with osdev.org Makefile

Post by BootComp »

Thanks. That fixed it and all working now. :)
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Error with osdev.org Makefile

Post 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)
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
solano
Posts: 8
Joined: Mon Oct 01, 2012 12:29 pm
Location: Brazil

Re: Error with osdev.org Makefile

Post by solano »

Sorry, I confused them. What I don't know now is how it worked. :mrgreen:
Caspian Project developer
Brazilian OSDev'er. Sorry for the terrible English!
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Error with osdev.org Makefile

Post 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
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
Post Reply