无法将 UNIX/Linux 程序编译为 32 位程序

发布于 2024-12-11 13:03:24 字数 4104 浏览 0 评论 0原文

我编写了一个使用一些低级 I/O 的基本 UNIX 程序。没什么特别的,如果您想查看的话,这里是代码:

#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define BUFFSIZE 1024

int main(int argc, char *argv[])
{
    // Character buffer
    char buffer[BUFFSIZE];
    // File 1 descriptor
    int file1Desc = 0;
    // File 2 descriptor
    int file2Desc = 0;
    // Output file descriptor
    int outfileDesc = 0;
    // Count # of chars read in
    int n = 0;
    // Boolean: no stdin reads?
    unsigned char zeroSTDIN = 1;

    // Need at least two arguments, no more than 3
    if((argc < 3) || (argc > 4)){
        printf("ERROR!!!\nUsage: kitty file1 file2 <outfile>\n");
        return EXIT_FAILURE;
    }

    /** TRY TO OPEN FILES 1 AND 2 FOR READING **/
    // Check for stdin read flag
    if(argv[1][0] == '-' && argv[1][1] == '\0'){
        file1Desc = STDIN_FILENO;
        zeroSTDIN = 0;
    }
    // Otherwise just open file 1 for reading
    else{
        if((file1Desc = open(argv[1],O_RDONLY)) < 0){
            perror(argv[1]);
            printf("ERROR!!!\nUnable to open %s for reading\n",argv[1]);
            return EXIT_FAILURE;
        }
    }
    // Check for stdin read flag
    if(argv[2][0] == '-' && argv[2][1] == '\0'){
        // Only one read from stdin
        if(!zeroSTDIN){
            perror(argv[2]);
            printf("ERROR!!!\nOnly one read from stdin\n",argv[1]);
        }
        file2Desc = STDIN_FILENO;
    }
    // Otherwise just open file2 for reading
    else{
        if((file2Desc = open(argv[2],O_RDONLY)) < 0){
            perror(argv[2]);
            printf("ERROR!!!\nUnable to open %s for reading\n",argv[2]);
            if(file1Desc != STDIN_FILENO)
                close(file1Desc);
            return EXIT_FAILURE;
        }
    }

    // If argument specified, try to open output file for writing
    if(argc == 4){
        if((outfileDesc = open(argv[3],O_WRONLY|O_CREAT|O_TRUNC)) < 0){
            perror(argv[2]);
            printf("ERROR!!!\nUnable to open %s for writing\n",argv[3]);
            // Can't forget these
            if(file1Desc != STDIN_FILENO)
                close(file1Desc);
            if(file2Desc != STDIN_FILENO);
                close(file2Desc);
            return EXIT_FAILURE;
        }
    }
    // stdout otherwise
    else{
        outfileDesc = STDOUT_FILENO;
    }

    // While there is anything left to read from file 1
    while((n = read(file1Desc, buffer, BUFFSIZE)) > 0){
        // Write n chars to output file
        if(write(outfileDesc, buffer, n) != n){
            printf("Error writing to output file from file 1\n");
            return EXIT_FAILURE;
        }
    }
    // While there is anything left to read from file 2
    while((n = read(file2Desc, buffer, BUFFSIZE)) > 0){
        // Write n chars to output file
        if(write(outfileDesc, buffer, n) != n){
            printf("Error writing to output file from file 1\n");
            return EXIT_FAILURE;
        }
    }

    // Exit program
    if(file1Desc != STDIN_FILENO)
        close(file1Desc);
    if(file2Desc != STDIN_FILENO);
        close(file2Desc);
    if(argc == 4)
        close(outfileDesc);
    return 0;
}

它编译时没有任何特殊选项。但是,我想在 32 位模式下编译。我运行的是 Fedora 15 64 位,它默认为 64 位编译。

我正在尝试使用以下选项:

gcc -S -m32 -O3 -o llio.asm llio.c

所以我想可以查看正在生成的汇编代码,但出现以下编译错误:

In file included from /usr/include/features.h:387:0,
                 from /usr/include/fcntl.h:27,
                 from kitty.c:1:
/usr/include/gnu/stubs.h:7:27: fatal error: gnu/stubs-32.h: No such file or directory
compilation terminated.

此站点似乎知道我的问题: http://www.cyberciti.biz/faq/x86_64-linux-error-gnustub-32h-missing-error-and-solution/

但是,我看了一下,这些文件似乎已经安装了。我缺少什么?

安装的文件

I wrote a basic UNIX program that uses some low-level I/O. Nothing special, here's the code if you want to look at it:

#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define BUFFSIZE 1024

int main(int argc, char *argv[])
{
    // Character buffer
    char buffer[BUFFSIZE];
    // File 1 descriptor
    int file1Desc = 0;
    // File 2 descriptor
    int file2Desc = 0;
    // Output file descriptor
    int outfileDesc = 0;
    // Count # of chars read in
    int n = 0;
    // Boolean: no stdin reads?
    unsigned char zeroSTDIN = 1;

    // Need at least two arguments, no more than 3
    if((argc < 3) || (argc > 4)){
        printf("ERROR!!!\nUsage: kitty file1 file2 <outfile>\n");
        return EXIT_FAILURE;
    }

    /** TRY TO OPEN FILES 1 AND 2 FOR READING **/
    // Check for stdin read flag
    if(argv[1][0] == '-' && argv[1][1] == '\0'){
        file1Desc = STDIN_FILENO;
        zeroSTDIN = 0;
    }
    // Otherwise just open file 1 for reading
    else{
        if((file1Desc = open(argv[1],O_RDONLY)) < 0){
            perror(argv[1]);
            printf("ERROR!!!\nUnable to open %s for reading\n",argv[1]);
            return EXIT_FAILURE;
        }
    }
    // Check for stdin read flag
    if(argv[2][0] == '-' && argv[2][1] == '\0'){
        // Only one read from stdin
        if(!zeroSTDIN){
            perror(argv[2]);
            printf("ERROR!!!\nOnly one read from stdin\n",argv[1]);
        }
        file2Desc = STDIN_FILENO;
    }
    // Otherwise just open file2 for reading
    else{
        if((file2Desc = open(argv[2],O_RDONLY)) < 0){
            perror(argv[2]);
            printf("ERROR!!!\nUnable to open %s for reading\n",argv[2]);
            if(file1Desc != STDIN_FILENO)
                close(file1Desc);
            return EXIT_FAILURE;
        }
    }

    // If argument specified, try to open output file for writing
    if(argc == 4){
        if((outfileDesc = open(argv[3],O_WRONLY|O_CREAT|O_TRUNC)) < 0){
            perror(argv[2]);
            printf("ERROR!!!\nUnable to open %s for writing\n",argv[3]);
            // Can't forget these
            if(file1Desc != STDIN_FILENO)
                close(file1Desc);
            if(file2Desc != STDIN_FILENO);
                close(file2Desc);
            return EXIT_FAILURE;
        }
    }
    // stdout otherwise
    else{
        outfileDesc = STDOUT_FILENO;
    }

    // While there is anything left to read from file 1
    while((n = read(file1Desc, buffer, BUFFSIZE)) > 0){
        // Write n chars to output file
        if(write(outfileDesc, buffer, n) != n){
            printf("Error writing to output file from file 1\n");
            return EXIT_FAILURE;
        }
    }
    // While there is anything left to read from file 2
    while((n = read(file2Desc, buffer, BUFFSIZE)) > 0){
        // Write n chars to output file
        if(write(outfileDesc, buffer, n) != n){
            printf("Error writing to output file from file 1\n");
            return EXIT_FAILURE;
        }
    }

    // Exit program
    if(file1Desc != STDIN_FILENO)
        close(file1Desc);
    if(file2Desc != STDIN_FILENO);
        close(file2Desc);
    if(argc == 4)
        close(outfileDesc);
    return 0;
}

It compiles without any special options. However, I want to compile in 32-bit mode. I'm running Fedora 15 64-bit and it defaults to a 64-bit compilation.

I'm trying to use the following options:

gcc -S -m32 -O3 -o llio.asm llio.c

So I want can look at the assembly code being produced, but I get the following compilation errors:

In file included from /usr/include/features.h:387:0,
                 from /usr/include/fcntl.h:27,
                 from kitty.c:1:
/usr/include/gnu/stubs.h:7:27: fatal error: gnu/stubs-32.h: No such file or directory
compilation terminated.

This site seems to know my issue: http://www.cyberciti.biz/faq/x86_64-linux-error-gnustub-32h-missing-error-and-solution/

However, I looked and these files seem to already be installed. What am I missing?

Installed Files

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不奢求什么 2024-12-18 13:03:24

我曾经和你在同一条船上,这是我在我的一个笔记文件中写下的内容:

yum install glibc-devel.i686

注意:i 6 86 而不是 i 3 86

然后就可以了正如您所拥有的:gcc -m32...

I was in the same boat as you once, this is what I have written down in one of my notes files:

yum install glibc-devel.i686

NOTE: i 6 86 rather than i 3 86

Then just as you had: gcc -m32...

怎樣才叫好 2024-12-18 13:03:24

在大多数 Linux 发行版的 64 位安装中,未安装 32 位开发库...例如,在 Ubuntu 上,您需要安装 libc6-i386-dev (和 gcc-multilib,但我认为安装为无论如何都是依赖)

When on a 64bit install of most linux distributions, the 32 bit development libraries aren't installed... on Ubuntu for example you'll want to install libc6-i386-dev (and gcc-multilib, but I think that is installed as a dependency anyway)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文