gpt4 book ai didi

从C编译错误gcc调用Assembly

转载 作者:行者123 更新时间:2023-11-30 20:21:06 25 4
gpt4 key购买 nike

尝试在 C 中运行汇编,但无法使用 gcc 进行编译

main.c

#include <stdio.h>
#include "assem.s"
void extern testing(int * a);

int main(){
int b = 8;
int * a = &b;
testing(a);
printf("%d",*a);

}

assembly .s

.globl testing

testing:
ret

海湾合作委员会

gcc main.c assem.s -o test.exe

错误

expected identifier or '(' before '.' token .globl testing

最佳答案

当您执行#include "assem.s"时,它会获取文件assem.s的内容并将其放入C在那时候。结果将使编译器尝试编译此内容:

#include <stdio.h>
.globl testing

testing:
ret

void extern testing(int * a);

int main(){
int b = 8;
int * a = &b;
testing(a);
printf("%d",*a);

}

当然这不是你想要的。编译器尝试编译 .globltesting 行,但失败了,因为它不是正确的 C 语法,并导致出现错误。您所要做的就是删除#include "assem.s"

assem.s 将被汇编,然后使用命令 gcc main.cassem.s -o test.exe 链接到可执行文件。这个GCC命令与以下命令等效,只是不会生成中间目标文件:

gcc -c main.c -o main.o
gcc -c assem.s -o assem.o
gcc main.o assem.o -o test.exe

您可以按照与 .c 文件相同的方式汇编/编译 .s.S 文件。 .s.S 文件不应与 C #include 指令一起使用。一些新的GCC用户存在一种误解,认为它们应该被包含在内,而不是单独组装/链接。

关于从C编译错误gcc调用Assembly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43796174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com