在 Linux 64 位上结合 C 和汇编(32 位代码)

标签 c linux gcc 64-bit gnu-assembler

我有一个 64 位的 Ubuntu 操作系统,我一直在学习 32 位的汇编。我正在尝试编译这两个文件:

正方形.s:

#square.s

.section .text
.globl sqr
.type sqr, @function
sqr:
    pushl %ebp
    movl %esp, %ebp
    movl 8(%ebp), %eax
    imull %eax, %eax
    popl %ebp
    ret

主.c:

//main.c
#include <stdio.h>
extern long sqr(long);
int main(int argc, char* argv[])
{
    long squared = sqr(10);
    printf("%lu\n", squared);
    return 0;
}

在我的 32 位虚拟机上,我用这个命令编译了它们

  gcc main.c square.s -o test

它奏效了。我遇到的问题是我想在我的 64 位机器上编译这些文件。我尝试了几种编译这些文件的方法,但都没有奏效。 谁能指出我正确的方向?有没有办法做到这一点?我试过 -m32 但没用。

当我这样做时:

  gcc -m32 -o test main.c square.s

我明白了:

  In file included from /usr/include/stdio.h:28:0,
             from main.c:1:
/usr/include/features.h:323:26: fatal error: bits/predefs.h: No such file or directory
compilation terminated.

最佳答案

在 64 位 ubuntu 上编译/链接 32 位程序需要 gcc-multilib,试试:

sudo apt-get install gcc-multilib libc6-i386 lib6-dev-i386

但是,当您尝试链接其他库时,这可能会出现其他问题。

使用 32-bit chroot environment 会更好(即在 64 位 ubuntu 上运行 32 位根目录)。

关于在 Linux 64 位上结合 C 和汇编(32 位代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8598902/

相关文章:

c - 隐藏终端上的密码输入

C 编程 - 运算符

linux - Libnet 缺少一些定义标志

linux - cmake include_directories 命令之后/之前

c - 什么是 C 中的 bogus_type

c - 如何将二进制 int 转换为字符串?

c - 如何在C中读取/解析输入?常见问题

linux - 如何为所有请求添加 ~username 前缀?

c - seccomp --- 如何退出_SUCCESS?

c++ - 在 GCC、Clang 和英特尔编译器中强制内联单个宏?