Page 1 of 1

ld:i386 architecture of input file is incompatible with i386

Posted: Tue Jun 01, 2010 10:03 pm
by TylerH
Whole error and offending command:

Code: Select all

ld --oformat=elf32-i386 main.o start.o -o init.o
ld: i386 architecture of input file `main.o' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `start.o' is incompatible with i386:x86-64 output
'start.o' is a standard elf32 object file compiled with Fasm 1.69.14 and 'main.o' was compiled with gcc with "-c -ffreestanding" flags. I'm running Fedora Core 13, gcc ver 4.4.4 if it matters.

I'm not sure the whole thing is relevant, but my makefile is simple so I'll post to save you from asking later if you think it will help.
main makefile:

Code: Select all

AS=fasm
ASFLAGS=
CC=gcc
CFLAGS=-c -ffreestanding
CPP=gcc
CPPFLAGS=
LDFLAGS=-nostdlib --oformat=elf32-i386

all : Image

Image : init/init.o

	ld --script link.ld init/init.o

init/init.o :

	(cd init; make)

clean :
	(cd init; make clean)
init makefile:

Code: Select all

AS=fasm
ASFLAGS=
CC=gcc
CFLAGS=-ffreestanding -Wl,--oformat=elf32-i386
CPP=gcc
CPPFLAGS=
LDFLAGS=--oformat=elf32-i386

all : init.o

init.o : main.o start.o

	ld $(LDFLAGS) main.o start.o -o init.o

main.o : main.c

	$(CC) $(CFLAGS) main.c

start.o : start.asm

	$(AS) $(ASFLAGS) start.asm start.o

clean :
	rm init.o main.o start.o
It's by no means perfect. I'm trying to get a good setup for maintaining my project, I'm going to restart my OS after I get my makefiles planned out.

Re: ld:i386 architecture of input file is incompatible with

Posted: Tue Jun 01, 2010 11:02 pm
by TylerH
I finally got it. I've been learning make, ld, and gcc for the past week.
main:

Code: Select all

AS=fasm
ASFLAGS=
CC=gcc
CFLAGS=-ansi -c -ffreestanding -Wall
CPP=gcc
CPPFLAGS=
LDFLAGS=-melf_i386 --oformat=elf32-i386

all : Image

Image : init/init.o

	ld $(LDFLAGS) --script link.ld init/init.o

init/init.o :

	(cd init; make)

clean :
	(cd init; make clean)
init:

Code: Select all

AS=fasm
ASFLAGS=
CC=gcc
CFLAGS=-ansi -c -ffreestanding -m32 -Wall
CPP=gcc
CPPFLAGS=
LDFLAGS=-melf_i386 --oformat=elf32-i386 

all : init.o

init.o : main.o start.o

	ld $(LDFLAGS) main.o start.o -o init.o


main.o : main.c

	$(CC) $(CFLAGS) main.c

start.o : start.asm

	$(AS) $(ASFLAGS) start.asm start.o

clean :
	rm init.o main.o start.o
Any of you OSdev veterans have anything to recommend about my makefiles, constructive criticism is encouraged?

I saw this in Linux 0.0.1's Makefile, what's it called(so I can research it for myself)?

Code: Select all

.c.s:
	$(CC) $(CFLAGS) \
	-nostdinc -Iinclude -S -o $*.s $<
I think I get what it's supposed to do, it provides a common rule for all files ending in .c and turns them into .s's right?

Also, is there a way to pass variables between make invocations, like when I "cd init; make", is there a way to pass the values of my variables to that makefile?

Re: ld:i386 architecture of input file is incompatible with

Posted: Wed Jun 02, 2010 1:42 am
by qw
TylerAnon wrote:I think I get what it's supposed to do, it provides a common rule for all files ending in .c and turns them into .s's right?
Yes, it is called an implicit rule.
TylerAnon wrote:Also, is there a way to pass variables between make invocations, like when I "cd init; make", is there a way to pass the values of my variables to that makefile?
The EXPORT directive adds a variable to the environment, making it available to all child processes.

P.S. You can find all of this in the GNU MAKE Manual. IMO it is a very good manual.

Re: ld:i386 architecture of input file is incompatible with

Posted: Wed Jun 02, 2010 2:55 am
by gerryg400
This is truly time well spent. It took a few weeks to get my makefile system working with some help from the real engineers I work with. In my opinion the best solution will have a makefile in every directory with every makefile including a default.mk file that contains all the rules. The individual makefiles are quite short and easy to maintain. My default.mk knows how to build c and S files but also knows how to build libraries, binaries and how to walk subdirectories. It also knows how to clean.

After a lot of investigation I found how to automatically generate dependencies. All of this stuff is in the make manual. Here's how I build an o file from a c file. If I modify a header, everything that includes it is rebuilt.

Code: Select all

%.o : %.c
	@echo "        Compiling $<"
	@ if $(CC) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) \
		-MT $@ -MD -MP -MF "$*.d_" -c -o $@ $<; \
	then \
		mv -f "$*.d_" "$*.d"; \
	else \
		rm -f "$*.d_"; \
		exit 1; \
	fi ; \

Re: ld:i386 architecture of input file is incompatible with

Posted: Wed Jun 02, 2010 7:43 pm
by TylerH
As for it all being in the manual, that's to be expected. It's just that I suck at finding the things I'm looking for in huge documents. It helps to know the name of things so I know where to start.