无法在macOS Sierra上将程序集文件(.s)与GCC链接 [英] Can't link an assembly file (.s) with GCC on macOS Sierra
问题描述
我有2个文件可以编译. 第一个是main.c,第二个是在程序集(work.s)中进行求和和乘法的函数.
I have 2 files to compile. The first is main.c and the 2nd is a function that does sums and multiplications in assembly (work.s).
这是代码:
main.c文件:
#include <stdio.h>
short work();
int main() {
short z = work();
printf("work(); -> %hd\n", z);
return 0;
}
work.s文件:
.globl work;
work :
xorl %eax,%eax;
xorl %ecx,%ecx;
movw $20,%ax;
subw $2,%ax;
movw $7,%cx;
addw $3,%cx;
movw $10,%cx;
subw $3,%cx;
shl $1,%cx;
addw %cx,%ax;
ret;
从命令行使用gcc:gcc -m32 main.c work.s -o main
From command line using gcc : gcc -m32 main.c work.s -o main
这是输出:
i386体系结构的未定义符号:
Undefined symbols for architecture i386:
"_ work",引用自: _main in main-fbbcca.o
"_work", referenced from: _main in main-fbbcca.o
ld:未发现体系结构i386的符号
ld: symbol(s) not found for architecture i386
clang:错误:链接器命令失败,退出代码为1(使用-v查看 调用)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
在Linux上具有相同文件和命令的情况下,为什么以及如何解决该问题?
While on Linux with same files and commands it works, why and how I can fix it?
推荐答案
也许将您的全局名称更改为gcc正在寻找的名称?
Maybe change your global to the name gcc is looking for?
.globl _work;
_work :
...
还有其他方法:
https://montcs.bloomu.edu/~bobmon/Code/Asm.and.C/C-asm/Asm-C-example0.shtml
< =不同的编译器,相同的原理...
<= Different compiler, same principles...
这篇关于无法在macOS Sierra上将程序集文件(.s)与GCC链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!