nasm and c

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Whatever5k

nasm and c

Post by Whatever5k »

I want to call a C-function in nasm... .
So I declare my main c-function like this:

extern _kernel_main

and I call it like this:

call _kernel_main

In my main-c-file I have

void kernel_main()

ld sais he cannot find _kernel_main. Why not? I thought I have to declare c-functions in assembler always with a '_' in front.
roswell

Re: nasm and c

Post by roswell »

you're right, you must declare the function name with an underscore.

I think the error is in the compiled c object.

If you've compiled with the c++ extension of your compiler, the function name is modified in the obj. The reason is that C++ can handle multiple use of a function name if the functions have different arguments.

So in the compiled obj, the symbol is not _function_name, but _function_name@argument_type. The linker can not handle this modification.

To ensure that this is this problem in your case, you can use the objdump utility to see which symbols are exported by an obj file.

To avoid this problem in your c code, you must add :

#ifdef _cplusplus
extern "C" {
#endif

//
roswell

Re: nasm and c

Post by roswell »

i finish sorry I have validated by error

// Declare the prototype of your functions

#ifdef _cplusplus
}
#endif

I think this you solve your problem. If not, I don't know why.

Roswell
Whatever5k

Re: nasm and c

Post by Whatever5k »

No, it's because I compiled my asm file in ELF. ELF doesn't need '_'
Post Reply