Sign in to follow this  
widggman

nasm/gcc question [SOLVED]

Recommended Posts

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]

Share this post


Link to post
Share on other sites
The ELF object format doesn't actually use underscores before symbols, so that might be the cause of it.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
ELF is fine, just get rid of the underscores.

Posting the errors you get will also help.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this