Compile error: relocation R_X86_64_PC32 against undefined symbol

Compile error: relocation R_X86_64_PC32 against undefined symbol

我尝试用汇编语言编写函数并将它们放入动态库中,因此我使用以下命令创建 .o 和 .S:
nasm -f elf64 hello.S -o hello.o
但是当我想用 gcc 创建库时:
gcc -fPIC -shared hello.o -o libasm.so
它向我显示此错误:
/usr/bin/ld: hello.o: relocation R_X86_64_PC32 against undefined symbol printf@@GLIBC_2.2.5 can not be used when making a shared object; recompile with -fPIC

来自http://www.nasm.us/xdoc/2.10rc8/html/nasmdoc9.html#section-9.2.5

To call an external routine, you must use another special PIC relocation type, WRT ..plt. This is much easier than the GOT-based ones: you simply replace calls such as CALL printf with the PLT-relative version CALL printf WRT ..plt.

所以而不是

; ...
call     printf

使用

; ...
call     printf WRT ..plt

并正常编译和link。

顺便说一句,“WRT”的意思是“With Respect To ...”,即“在...的上下文中”