首页 > 解决方案 > /usr/bin/ld: 输入文件 `func.o' 的 i386 架构与 i386:x86-64 输出不兼容

问题描述

您好,我一直在努力尝试从我的 C++ 代码中调用程序集一个函数函数,我在运行链接命令时收到此错误:

g++ func.o test.o -o program
/usr/bin/ld: func.o: in function `_start':
func.asm:(.text+0x0): multiple definition of `_start'; /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o:(.text+0x0): first defined here
/usr/bin/ld: i386 architecture of input file `func.o' is incompatible with i386:x86-64 output
/usr/bin/ld: test.o: in function `main':
test.cpp:(.text+0x32): undefined reference to `calcsum'
collect2: error: ld returned 1 exit status
#include<stdio.h>
  2 
  3 
  4 extern "C" int calcsum(int a, int b, int c);
  5 
  6 int main(int argc,char* argv[])
  7 {
  8     int a = 10;
  9     int b = 20;
 10     int c = 30;
 11     int sum = calcsum(a,b,c);
 12 
 13     printf("a: %d", a);
 14     printf("b: %d", b);
 15     printf("c: %d", c);
 16     printf("sum: %d", sum);
 17     return 0;
 18 }
 19 

装配功能

 1 section .text
  2     global _start
  3 _start:
  4 global _calcsum
  5 _calcsum:
  6 
  7     push ebp
  8     mov ebp, esp
  9 
 10     mov eax, [ebp+8]
 11     mov ecx, [ebp+12]
 12     mov edx, [ebp+16]
 13 
 14     add eax, ecx
 15     add eax, edx
 16 
 17     pop ebp
 18     ret

,,,,,,, 提前谢谢

标签: c++assembly

解决方案


  1. 您正在汇编文件中重新定义 _start 。
  2. 在 x86_64 汇编中,您不能将 32 位寄存器压入堆栈。如果您想保留 32 位架构,请添加 BITS 32 或添加 BITS 64 并将 ebp 替换为 rbp 等...
  3. 指定架构。如果您使用的是 x86_64 架构,请为 g++ 添加 -m64 标志。为 x86 架构添加 -m32。
  4. 您不使用 Windows,因此您不必将 _ 前缀添加到 calcsum。而不是 _calcsum,只使用 calcsum。

推荐阅读