简体   繁体   English

如何使用 makefile 编译 32 位程序

[英]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.

相关问题 如何将混合(asm,C ++)源代码编译为32位程序? - How can I compile a hybrid (asm, C++) source code into a 32-bit program? 如何在 32 位操作系统中使用 g++ 进行编译 - How can I compile using g++ in a 32 bit operating system 如何将 c++ 程序编译为 32 位可执行文件而不是 64 位 - How do I compile a c++ program into a 32-bit executable instead of 64-bit 如何测量 32 位程序中 64 位进程的内存使用情况? - How can I measure memory usage of 64-bit process in 32-bit program ? CodeBlocks 为 32 位计算机编译程序 - CodeBlocks compile program for 32bit computer 我该如何使用64位而不是32位解决按位seive? 两者有什么区别? - How can I solve bitwise seive using 64 bit instead of 32 bit? What is the difference between these two? 如何将OpenSSL从32位配置为64位? - how I can configure OpenSSL to 64 bit from 32 bit? 如何使用自定义Makefile编译Android版STL C ++? - How can I compile STL C++ for Android using custom Makefile? 如何编译程序使其能够在32位Linux上使用&gt; 4GB内存? - How to compile a program to make it capable to use >4GB memory on 32-bit Linux? 如何使用Eclipse编译32位 - How to compile for 32bit with Eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM