Linking object files

Splitting into objects

Each source assembles to one object. Symbols you export are global; symbols you import are extern. The linker joins them by name:

; print.inc user: extern print_string ; print.asm: global print_string print_string: ; ... ret

Linking

List the objects after the output file. Order matters for static libraries but not for plain objects, and every referenced symbol must be defined somewhere or ld reports an undefined reference:

nasm -f elf64 -o main.o main.asm nasm -f elf64 -o print.o print.asm ld -o program main.o print.o

Static linking

ld links statically by default for plain objects. For libc programs, link with gcc -static to embed the C library, or leave it dynamic (the default with gcc) and let the loader resolve symbols at start.

Next: 32-bit vs 64-bit

Article author: Arthur Isaev