NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: mmk213 on June 27, 2017, 04:43:58 AM

Title: [solved] how to link sdl2 to assembly program using nasm + ld?
Post by: mmk213 on June 27, 2017, 04:43:58 AM
hi, i am using slackware linux, i am trying to compile this basic program, but i cannot start the executable when i compile it, it has something to do with linking the sdl2 library but i am new to this, not sure how to procede

my file (sdl.asm) is:
Code: [Select]
section .text
extern SDL_Init
global _start
_start:
  mov rdi, 0x20
  call SDL_Init

  xor eax, eax
  add al, 0x3C
  syscall

so i'm trying to compile this basic code with
Code: [Select]
nasm -f elf64 sdl.asm -o sdl.o
ld sdl.o -o sdl -lSDL2

but when i start the file i get
Code: [Select]
bash: ./sdl: No such file or directory

"file sdl" gets me:
Code: [Select]
sdl: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld64.so.1, not stripped

Title: Re: how to link sdl2 to assembly program using nasm + ld?
Post by: mmk213 on June 27, 2017, 04:57:41 AM
hm, i managed to get it running, not really sure why though

Code: [Select]
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o sdl sdl.o -lSDL2

this gave me a working executable, but i dont know why is this ld-linux-x86-64.so.2 link option needed.. i wil marked it solved
Title: Re: [solved] how to link sdl2 to assembly program using nasm + ld?
Post by: Frank Kotler on June 27, 2017, 05:38:23 AM
32-bit ld does the same thing - the default dynamic linker does not exist. I don't know why. If you want to save some typing, "-I" is an alias for "-dynamic-linker". Good job figuring it out. That "no such file or directory" - when you can see it right there - is pretty confusing!

Best,
Frank