GNU Linker和架构i386

时间:2013-03-19 09:57:52

标签: c macos assembly ld i386

所以我在OS X上运行,我想链接两个Mach-O对象i386。

第一个是从NASM生成的(它是一个汇编文件)

nasm -f macho -o kernel.o kernel.asm

第二个是从GCC生成的

gcc -c -arch i386 screen.c

但是当我尝试链接它们时......

ld -o myprogram screen.o kernel.o

...我收到此错误:

Undefined symbols for architecture i386:
  "print", referenced from:
      start in kernel.o
ld: symbol(s) not found for inferred architecture i386

我不明白为什么,因为我的两个文件是Mach-O object i386

$ file screen.o
screen.o: Mach-O object i386
$ file kernel.o
kernel.o: Mach-O object i386

如果您需要,请点击 kernel.asm

[BITS 32]
EXTERN print
GLOBAL start

start:
    mov  eax, msg
    push eax
    call print
    pop  eax

end:
    jmp end

msg  db 'Hello world!', 10, 0

这是 screen.c

void putcar(uchar c)
{
    /* Some code here */
}

void print(char *string)
{
        while(*string != 0){
                putcar(*string);
                string++;
        }
}

1 个答案:

答案 0 :(得分:3)

您需要在asm文件中使用符号_print,即

start:
    mov  eax, msg
    push eax
    call _print
    pop  eax

这是因为C函数名称在编译时会得到一个前导下划线。