[英]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.我正在尝试制作 3 个单独的程序 mem_1.exe、mem_2.exe 和 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.当我编译它们时,我需要将它们设为 32 位,并且错误消息似乎没有反映我正在写的内容。 Below is my makefile.下面是我的 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每次我尝试运行此 makefile 时,我都会收到此错误消息
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.这似乎没有意义,因为我使用 -m32 标志使其成为 32 位。 Can anyone explain what I'm doing wrong?谁能解释我做错了什么?
I'd advise thinking of your makefile as code (since it is) and follow the standard advice to avoid repeating yourself.我建议您将 makefile 视为代码(因为它是)并遵循标准建议以避免重复自己。
It looks like you also have the flags a bit wrong.看起来你的标志也有点错误。 -m32
is a compiler flag. -m32
是编译器标志。 -ldl
is a linker flag. -ldl
是 linker 标志。 If you're going to build for 32 bits, you need to tell both the compiler and the linker to built 32-bit files.如果要构建 32 位文件,则需要告诉编译器和 linker 构建 32 位文件。 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.注意:我老了,所以这是一种老式的 Makefile。 Gnu recommends doing things a bit differently, but this should still work. Gnu 建议做一些不同的事情,但这应该仍然有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.