Reputation: 1426
I'd like to send the adress of a function defined in inline assembly to a C function but I am getting this error :
error: "func" undeclared (first use in this function)
Here is the code used for the call :
__asm__(".global func\n"
"func:\n"
// func code
);
another_func(&func);
I thought I should use a forward declaration in C (like "void func();" ahead in the code) but is there another cleaner way to do it ?
Upvotes: 1
Views: 1068
Reputation: 2320
It may depend on your compiler, assembler & linker tool chain, but a forward declaration is the right approach.
extern void func(void);
The compiler and assembler may not be invoked in the order you expect them to be. The compiler has to know about the name of the function at the time it is run. Declaring the function as .global
(or .public
) will cause the assembler to emit a public symbol: _func
, but because this is inline assembly, it is not available to the C compiler. You need to tell the C compiler about the symbol so that it knows what to expect, and the linker has to know to use the output symbol from the assembler as the input symbol for the C reference.
Upvotes: 3
Reputation: 409176
It's because the C compiler doesn't know about assembler symbols, only C symbols.
The simple solution is to make a standard C function and in it have the inline assembly:
void func(void)
{
__asm__("...");
}
Upvotes: 1