1

我正在尝试为汇编程序编写一个静态库。但是,它不起作用。该库构建良好,但是当我尝试构建程序时,会发生这种情况:

$ ld -o hello -L../myasm -lmyasm hello.o
hello.o: In function `_start':
(.text+0x18): undefined reference to `exit'

我四处张望,这让我更加困惑。

$ nm ../myasm/libmyasm.a
myasm.o:
00000000 T exit
$ nm hello.o
00000000 T _start
         U exit
00000000 d message

知道发生了什么吗?

我的代码:

你好.s

#; This is a hello world program, in assembler.

.extern exit

.data
message:
    .byte 14
    .ascii "Hello, World!\n"

.text
.global _start

_start:
    #; First, write the message.
    mov $4, %eax            #; write syscall number
    mov $1, %ebx            #; stdout file descriptor
    mov $message+1, %ecx    #; message address
    mov message, %dl        #; we only want one byte, so %dl
    int $0x80

    #; Now, we need to exit.
    call exit

你好/Makefile

hello: hello.o
        ld -o hello -L../myasm -lmyasm hello.o
hello.o: hello.s
        as -o hello.o hello.s
run: hello
        ./hello
clean:
        rm hello.o hello

肌瘤

#; lib.s

.text
.global exit

exit:
    mov $1, %eax #; exit syscall #
    mov $0, %ebx #; success
    int $0x80

myasm/Makefile

libmyasm.a: myasm.o
        ar cr libmyasm.a myasm.o
myasm.o: myasm.s
        as -o myasm.o myasm.s
clean:
        rm myasm.o libmyasm.a
4

1 回答 1

1

hello的 Makefile 中,将目标文件移到hello.o之前-lmyasm的位置:

hello: hello.o
        ld -o hello hello.o -L../myasm -lmyasm
...

-l参阅此处有关如何完成符号搜索的参考,3.13 链接选项

-llibrary
-l library

...

在命令中编写此选项的位置有所不同;链接器按照指定的顺序搜索和处理库和目标文件。因此,在foo.o -lz bar.o文件z 之后foo.o但在bar.o. 如果bar.o引用 中的函数 z,则可能不会加载这些函数。

...

于 2014-08-14T00:01:50.980 回答