Makefile

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Makefile

Post by Nessphoro »

Okay guys this is killing me,

In my makefile, I tell it to use G++ along with some custom flags to compile, but those flags do not appear to work, they don't even appear in a command line.

Makefile:
CPP_FILES := $(wildcard source/*.cpp)
OBJ_FILES := $(patsubst %.cpp,%.o,$(CPP_FILES))
LD_FLAGS := -T linker.ld
CC_FLAGS := -ggdb -Wall -Wextra -nostdlib -fno-builtin -nostartfiles -nodefaultlibs -fno-exceptions -fno-rtti -fno-stack-protector -fleading-underscore

kernel.bin: $(OBJ_FILES) ; ld $(LD_FLAGS) -o kernel.bin boot.o $(OBJ_FILES)

source/%.o: source/%cpp ; g++ $(CC_FLAGS) -o $@ $<

P.S: LD_FLAGS work flawlessly
Last edited by JamesM on Thu May 26, 2011 7:23 am, edited 1 time in total.
Reason: Remove "Lulz" from the post title. This is not 4chan!
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Makefile Lulz

Post by bluemoon »

Code: Select all

source/%.o: source/%cpp ; g++ $(CC_FLAGS) -o $@ $<
Do you mean source/%.cpp?
if things does not match any rule, the default action would be made such as invoke g++ without your custom flags.

The easiest way is to replace or insert echo checkpoints to debug, which is essential skill for general programming.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Makefile Lulz

Post by Solar »

Code: Select all

source/%.o: source/%cpp ; g++ $(CC_FLAGS) -o $@ $<
What's the semicolon doing there?

Everything after the colon is the dependency. The command(s) to be executed should be on subsequent lines, starting with a tab. This is important!

Code: Select all

source/%.o: source/%.cpp
	$(CXX) $(CC_FLAGS) -o $@ $<
You might want to check out the Makefile tutorial for more things regarding 'make'.
Every good solution is obvious once you've found it.
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: Makefile Lulz

Post by Nessphoro »

Yeah thanks that worked, but how do I go on adding ASM code support, because this doesn't work

PROJECT_DIRS := source
CPP_FILES := $(wildcard source/*.cpp)
ASM_FILES :=$(wildcard source/*.asm)
OBJ_FILES := $(patsubst %.cpp,%.o,$(CPP_FILES))
ASM_OBJ :=$(patsubst %.asm,%.o,$(ASM_FILES))
LD_FLAGS := -T linker.ld
CC_FLAGS := -ggdb -Wall -Wextra -nostdlib -fno-builtin -nostartfiles -nodefaultlibs -fno-exceptions -fno-rtti -fno-stack-protector -fleading-underscore

kernel.bin: $(OBJ_FILES)
ld $(LD_FLAGS) -o kernel.bin $(OBJ_FILES) $(ASM_OBJ)

source/%.o: source/%.cpp
g++ $(CC_FLAGS) -o $@ $<
source/%.o: source/%.asm
nasm -f elf -o $@ $<
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Makefile Lulz

Post by bluemoon »

As a positive suggestion you should read Getting_Started, specifically Required Knowledge & Toolchain

To your question, it's quite obvious you didn't include assembly files in your definition of OBJ_FILES; or otherwise include ASM_OBJ in kernel.bin.
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: Makefile

Post by Nessphoro »

Gotcha,

Well sorry guys, I had my OS compiling but the problem was that it was a bash file and everytime I added a file, I needed to make changes myself, so I looked into makefile, even though with some fail :D
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Makefile

Post by Solar »

Make sure you really understand 'make'. Using a half-understood tool in a non-trivial project is an error source unto itself.

For example, your Makefile doesn't cater to changes made in header files. You change a constant in a header file, you might be in for a lengthy debug session before you realize that your object files didn't get recompiled. Might I recommend the Makefile tutorial a second time?
Every good solution is obvious once you've found it.
Post Reply