nasm/gcc question [SOLVED]

Started by
8 comments, last by widggman 17 years, 11 months ago
Hi, i'm trying to mix C and x86 assembly code, and i need help to compile. main.c #include <stdio.h> #include "test.h" int main(void) { printf("4 + 5 = %d\n", add(4, 5)); return 0; } test.h int add(int a, int b); test.asm section .text global _add _add: mov eax, [esp+4] add eax, [esp+8] ret And when i compile, i first create an object file with test.asm: nasm -f elf test.asm and after, i link with gcc: gcc main.c test.o and i receive this error: /tmp/cchlu6gd.o: In function 'main': main.c:(.text+0x20): undefined reference to 'add' collect2: ld returned 1 exit status The problem, i think, is about the name of 'add' in the assembly file and the C file. In the 'test.asm' file, if i change the name '_add' for 'add' it doesn't compile. Any suggestion ?? [Edited by - widggman on May 21, 2006 12:21:48 PM]

Advertisement
Which OS and gcc are you running?
The ELF object format doesn't actually use underscores before symbols, so that might be the cause of it.
Ra
Add is the name of a nasm instruction, try changing the name of your function?
I'm on gentoo

and the version of gcc is : 3.3.5-20050130 ...

Quote:Original post by BlodBath
Add is the name of a nasm instruction, try changing the name of your function?

That's it. Lose the underscores and rename your function.
Ra
Quote:Original post by Ra
The ELF object format doesn't actually use underscores before symbols, so that might be the cause of it.


Quote:Original post by BlodBath
Add is the name of a nasm instruction, try changing the name of your function?


I think, between them, these two have got it. You need to remove the underscore, since ELF does not use them. However, when you do so, NASM sees your "add" label as an instruction.

EDIT: Beaten!
i changed the name of my function to "adding" and i have the same error

and if i have to compile without the 'elf' which kind of option should i choose ?

ELF is fine, just get rid of the underscores.

Posting the errors you get will also help.
Ra
IT'S WORKING

it was a name problem with 'add', I changed it to 'adding' and remove the underscore and it works just fine

thanx for your answer, and how quickly you answer it

thanx again

This topic is closed to new replies.

Advertisement