JamesMolloy tutorial fails to compile

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
optimisticnugget
Posts: 8
Joined: Wed Mar 02, 2022 4:21 pm

JamesMolloy tutorial fails to compile

Post 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?
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

Re: JamesMolloy tutorial fails to compile

Post 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.
optimisticnugget
Posts: 8
Joined: Wed Mar 02, 2022 4:21 pm

Re: JamesMolloy tutorial fails to compile

Post 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) $<
Post Reply