Author Topic: nasm / gcc link problem  (Read 10195 times)

nobody

  • Guest
nasm / gcc link problem
« on: January 07, 2008, 02:21:41 PM »
hello, I am trying to write C functions with nasm and I cannot get gcc to link them.

I wrote this assembly code:
somefunc.asm

somefunc:
  ret

And this C code:
main.c

extern void somefunc(void)

int main(void)
{
  somefunc();
  return 0;
}

and when I compile:
nasm -f elf somefunc.asm
gcc -c main.c
gcc main.o somefunc.o -o test

I get:
main.o: In function `main':
main.c:(.text+0x12): undefined reference to `somefunc'
collect2: ld returned 1 exit status

But objdump tells me that there is a somefunc symbol in my object file:
objdump -d somefunc.o
somefunc.o:     file format elf32-i386

Disassembly of section .text:

00000000 :
   0:   c3                      ret

any ideas ?

regards.

nobody

  • Guest
Re: nasm / gcc link problem
« Reply #1 on: January 07, 2008, 06:28:51 PM »
In your asm file, "global somefunc". For "non-ELF C", you'd probably want an underscore on it.

In general, "global" for stuff that's in this file, and we need to tell ld about it. "extern" for stuff that's *not* in this file (but is referenced)... and we need to tell ld about it.

(BTW, there's a utility named "test", so make sure you use "./test" to get "your test", rather than the utility)

Best,
Frank

nobody

  • Guest
Re: nasm / gcc link problem
« Reply #2 on: January 12, 2008, 10:59:39 AM »
It works!
thank you for being so fast.

nobody

  • Guest
Re: nasm / gcc link problem
« Reply #3 on: March 09, 2008, 04:23:52 PM »
I dont Understand Can Post new code?