c++ - /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
,,,,,,, 提前谢谢
解决方案
- 您正在汇编文件中重新定义 _start 。
- 在 x86_64 汇编中,您不能将 32 位寄存器压入堆栈。如果您想保留 32 位架构,请添加 BITS 32 或添加 BITS 64 并将 ebp 替换为 rbp 等...
- 指定架构。如果您使用的是 x86_64 架构,请为 g++ 添加 -m64 标志。为 x86 架构添加 -m32。
- 您不使用 Windows,因此您不必将 _ 前缀添加到 calcsum。而不是 _calcsum,只使用 calcsum。
推荐阅读
- recaptcha - 如何连接 reCaptcha v3 和图像验证网格
- javascript - 实现@keyframes CSS 动画的问题
- python - 在多列中按类别计算数据帧值
- r - Tidyverse 没有安装
- python-2.7 - 如何提示用户输入并只允许数字响应(在 Python 2.7 中)
- python - 嗨,我正在编写一个机器人来填写 HTML 表单。对值和有效负载有一些疑问
- go - 使用 Go 后端的 net::ERR_CERT_INVALID
- android - 在 kotlin 中向表格布局添加行
- python - JointDistributionSequential 样本的对数概率上的 Tensorflow 概率不兼容形状错误
- python - 如何将 for 循环中的值添加到数据框列中?