How to compile 32-bit programs under Linux 64-bit system?


With the continuous development of computers, 64-bit systems have gradually become mainstream, but when we write programs, we still need to compile 32-bit programs to be compatible with old systems or other 32-bit program libraries. So in Linux 64 How to compile 32-bit programs under a bit system?

1. Preliminary knowledge

We need to understand some basic preparatory knowledge:
1. Cross-compilation: refers to writing, compiling and running target files of another machine under the operating system of one machine (i.e. cross-platform compilation for different platforms).
2. gcc-multilib: It is a multi-architecture support extension of the gcc compiler, so it can be used to generate code adapted to different architectures without specifying architecture information at compile time.
3. lib32z1-dev: It is a library file that provides support for 32-bit programs and can be installed and used under 64-bit Linux systems.
The above preparatory knowledge will be used in subsequent operations.

2. Configuration environment

Before we start compiling 32-bit programs, we need to make some environment configurations.

1. Install the support library

We need to install some support libraries. Enter the following command in the terminal:
sudo apt-get install libc6:i386 libstdc++6:i386 lib32z1-dev

2. Install gcc-multilib

After installing the support library, we need to install the gcc-multilib extension to support compiling 32-bit programs. Enter the following command in the terminal:
sudo apt-get install gcc-multilib
After the above two steps are completed, our environment is ready.

3. Compiler program

After the above configuration, we can start compiling 32-bit programs.

1. Use gcc compiler

We can use the gcc compiler to compile and generate 32-bit programs. Enter the following command in the terminal:
gcc -m32 -o sample sample.c
Among them, sample is the name of the generated executable file, and sample.c is our The source file to compile.
The -m32 parameter is used to specify that the compiled program is 32-bit.

2. Use makefiles

If the program we want to compile involves multiple source files, we can compile it by writing a makefile. The following is a simple makefile code:

CC=gcc
CFLAGS=-g -m32
LDFLAGS=-L/usr/lib32
all: sample
sample: sample.o func.o
$$(CC) $$(CFLAGS) $(LDFLAGS) -o sample sample.o func.o
sample.o: sample.c
$(CC) $(CFLAGS) -c sample.c -o sample.o
func.o: func.c
$(CC) $(CFLAGS) -c func.c -o func.o
clean:
rm -rf *.o sample

Enter the make command in the terminal to compile the makefile.

4. Test procedures

After compilation is completed, we need to test to ensure that the program can run normally. Enter the following command in the terminal:
./sample
If the output result is correct, it proves that the program has been successfully compiled into a 32-bit program.

Testing process

Compiling a 32-bit program under a Linux 64-bit system requires some configuration and specific command input, but as long as you follow the above steps, you can easily complete the compilation. At the same time, we also need to pay attention to some details, such as parameter settings in the makefile and the steps of testing the program. Hope this article helps you!
Test code: sizeof_test.c

#include <stdio.h>

typedef union {
    
    
    long i;
    int k[5];
    char c;
}DATE;

struct data {
    
    
    int cat;
    DATE cow;
    double dog;
} too;


int main(void)
{
    
    
    DATE max;
    printf("%d\n", sizeof(struct data)+sizeof(max));
    return 0;
}

Test Results

Test Results

Guess you like

Origin blog.csdn.net/m0_66338176/article/details/134022751
Recommended