Chinaunix

标题: gcc中调用nasm [打印本页]

作者: stuman    时间: 2014-04-25 21:01
标题: gcc中调用nasm
本帖最后由 stuman 于 2014-04-25 21:03 编辑

bar.c
  1. #include <stdio.h>
  2. extern int f0();
  3. main()
  4. {
  5. int i;
  6. i = f0();
  7. printf("%d",i);
  8. }
复制代码
foo.asm
  1. ;[section .text]  
  2. global _f0
  3. _f0:
  4.         mov eax,100
  5. ret
复制代码
编译命令如下:
gcc -c bar.c
nasm -f elf foo.asm
gcc -o foobar bar.o foo.o

出错信息:
bar.o: In function `main':
bar.c: ( .text+0x12): undefined reference to `f0'
collect2: ld returned 1 exit status

请问如何解决?
作者: stuman    时间: 2014-04-25 21:34
经过多次尝试,终于解决问题,要注意3点:
汇编中函数要用global导出,函数名不需要加下划线,链接目标文件时不能用ld要用gcc




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2