Linker: Multiple Definition Error
Posted: Sun Jul 20, 2008 5:29 pm
I've been following Bran's tutorial. At first I grouped all of my source files together in one directory (along with my make file and linker script) apart from a header file which is in the subdirectory "include". I used the following make file to build it, and it works:
Then I decided to start organizing my source files a little bit better. So, I placed each source file in it's own sub directory (makefile and linker script remain where they were). Then I tried using the following make file (based on the makefile tutorial in the wiki):
BUT I get a "multiple definition error" from the linker despite not actually having changed my source code!
The error:
Obviously I'm doing something wrong, but I have no idea what, any ideas?
Code: Select all
all: floppy.img
loader.o: loader.asm
nasm -f aout -o loader.o loader.asm
main.o: main.c
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o main.o main.c
memory.o: memory.c
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o memory.o memory.c
string.o: string.c
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o string.o string.c
kernel.bin: loader.o main.o memory.o string.o
ld -T link.ld -o kernel.bin loader.o main.o memory.o string.o
floppy.img: kernel.bin
cat stage1 stage2 pad kernel.bin > floppy.img
Code: Select all
COMPILER := gcc
ASSEMBLER := nasm
PRJDIRS := loader memory string
NASM_SOURCE_FILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.asm")
C_SOURCE_FILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.c")
C_HEADER_FILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.h")
ALL_SOURCE_FILES := $(NASM_SOURCE_FILES) $(C_SOURCE_FILES) $(C_HEADER_FILES)
OBJECT_FILES := $(patsubst %.c,%.o,$(C_SOURCE_FILES)) $(patsubst %.asm,%.o,$(NASM_SOURCE_FILES))
COMPILER_FLAGS := -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin
all: kernel.bin
kernel.bin: $(OBJECT_FILES)
ld -T link.ld -o kernel.bin $(OBJECT_FILES)
%.o: %.c
$(COMPILER) $(COMPILER_FLAGS) -I./include -c -o $@ $<
%.o: %.asm
$(ASSEMBLER) -f aout -o $@ $<
The error:
Code: Select all
make -k all
ld -T link.ld -o kernel.bin ./string/string.o ./main.o ./memory/memory.o ./loader/loader.o
./main.o: In function `kmain':
main.c:(.text+0x0): multiple definition of `kmain'
./string/string.o:(.text+0x0): first defined here
./memory/memory.o: In function `memcpy':
memory.c:(.text+0x0): multiple definition of `memcpy'
./string/string.o:(.text+0x10): first defined here
./memory/memory.o: In function `memset':
memory.c:(.text+0x29): multiple definition of `memset'
./string/string.o:(.text+0x39): first defined here
./memory/memory.o: In function `memsetw':
memory.c:(.text+0x4b): multiple definition of `memsetw'
./string/string.o:(.text+0x5b): first defined here
./loader/loader.o:./loader/loader.o:(.text+0x20): multiple definition of `_loader'
./string/string.o:(.text+0xa0): first defined here
make: *** [kernel.bin] Error 1
make: Target `all' not remade because of errors.