Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

c++ - How can I compile a 32 bit program using a makefile

I'm attempting to make 3 separate programs mem_1.exe, mem_2.exe and mem_3.exe. I need to make them 32 bit when I compile them and the error message does not seem to be reflecting what I am writing. Below is my makefile.

mem_1: memlayout.o mem_1.o
    gcc -o mem_1 memlayout.c mem_1.c  -ldl -m32

mem_2: memlayout.o mem_2.o
    gcc -o mem_2 memlayout.c mem_2.c -m32 -ldl

mem_3: memlayout.o mem_3.o
    gcc -o mem_3 memlayout.c mem_3.c -m32 -ldl


mem_1.o: mem_1.c 
    gcc -c -o mem_1  mem_1.c -m32

mem_2.o: mem_2.c 
    gcc -c -o mem_2  mem_2.c -m32

mem_3.o: mem_3.c 
    gcc -c -o mem_3  mem_3.c -m32
memlayout.o: memlayout.c
    gcc -c -o memlayout memlayout.c -m32
clean:
    rm -f mem_1.o mem_2.o mem_3.o memlayout.o *~

Everytime I attempt to run this makefile I get this error message

cc    -c -o memlayout.o memlayout.c
cc    -c -o mem_1.o mem_1.c
gcc -o mem_1.exe mem_1.o memlayout.o -m32 -ldl
/usr/bin/ld: i386:x86-64 architecture of input file `mem_1.o' is incompatible with i386 output
/usr/bin/ld: i386:x86-64 architecture of input file `memlayout.o' is incompatible with i386 output

Which doesn't seem to make sense since I am using the -m32 flag to make it a 32 bit. Can anyone explain what I'm doing wrong?

question from:https://stackoverflow.com/questions/66057566/how-can-i-compile-a-32-bit-program-using-a-makefile

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'd advise thinking of your makefile as code (since it is) and follow the standard advice to avoid repeating yourself.

It looks like you also have the flags a bit wrong. -m32 is a compiler flag. -ldl is a linker flag. If you're going to build for 32 bits, you need to tell both the compiler and the linker to built 32-bit files. No guarantee, but I think you want something on this general order:

CFLAGS = -m32
LFLAGS = -melf_i386 -ldl

mem_1: mem_1.o memlayout.o
    $(LD) $(LFLAGS) -o mem_1 mem_1.o memlayout.o

mem_2: mem_2.o memlayout.o
    $(LD) $(LFLAGS) -o mem_2 mem_2.o memlayout.o

mem_3: mem_3.o memlayout.o
    $(LD) $(LFLAGS) -o mem_3 mem_3.o memlayout.o

# You probably don't really need this--`make` will usually have it built-in:
.c.o:
    $(CC) -c $(CFLAGS) $<

Note: I'm old, so this is a sort of old-fashioned Makefile. Gnu recommends doing things a bit differently, but this should still work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...