Page 1 of 1

JamesMolloy tutorial fails to compile

Posted: Thu Mar 03, 2022 11:15 am
by optimisticnugget
Trying to compile this http://www.jamesmolloy.co.uk/tutorial_h ... nesis.html

I get:

Code: Select all

ld -Tlink.ld -o kernel boot.o main.o
ld: i386 architecture of input file `boot.o' is incompatible with i386:x86-64 output
make: *** [Makefile:18: link] Error 1
The makefile is:

Code: Select all

SOURCES=boot.o main.o

CFLAGS=-nostdlib -nostdinc -fno-builtin -fno-stack-protector
LDFLAGS=-Tlink.ld
ASFLAGS=-felf

all: $(SOURCES) link

clean:
        -rm *.o kernel

link:
        ld $(LDFLAGS) -o kernel $(SOURCES)

.s.o:
        nasm $(ASFLAGS) $<
I have gcc: gcc (GCC) 11.2.0
nasm: 2.15.05
and ld: 2.38

What is the problem? How do I tell ld to output in the correct format?

Re: JamesMolloy tutorial fails to compile

Posted: Thu Mar 03, 2022 9:10 pm
by klange
This tutorial is very old (around fifteen years now) and has enough issues we have an entire wiki article about them.

The specific problem you are facing here is the lack of a cross-compiler, combined with insufficient command line flags to the compiler and linker to workaround that fact.

Re: JamesMolloy tutorial fails to compile

Posted: Sat Apr 02, 2022 11:51 am
by optimisticnugget
klange wrote:This tutorial is very old (around fifteen years now) and has enough issues we have an entire wiki article about them.

The specific problem you are facing here is the lack of a cross-compiler, combined with insufficient command line flags to the compiler and linker to workaround that fact.
I figured out a simple fix, not sure if it'll work for everyone, but it works for me.

Add -m32 and -m elf_i386 to CFLAGS and LDFLAGS, respectively:

Code: Select all

SOURCES=boot.o main.o monitor.o common.o descriptor_tables.o isr.o interrupt.o gdt.o timer.o \
        kheap.o paging.o

CFLAGS=-nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32
LDFLAGS=-Tlink.ld -m elf_i386
ASFLAGS=-felf

all: $(SOURCES) link

clean:
    -rm *.o kernel

link:
    ld $(LDFLAGS) -o kernel $(SOURCES)

.s.o:
    nasm $(ASFLAGS) $<