For Reasons, I'm trying to use top-level assembly in GCC to define some static functions. However, since GCC doesn't "see" the body of those functions, it warns me that they are "used but never defined. A simple source code example could look like this:
/* I'm going to patch the jump offset manually. */
asm(".pushsection .slen,\"awx\",@progbits;"
".type test1, @function;"
"test1: jmp 0;"
".popsection;");
/* Give GCC a C prototype: */
static void test(void);
int main(int argc, char **argv)
{
/* ... */
test();
/* ... */
}
And then,
$ gcc -c -o test.o test.c
test.c:74:13: warning: ‘test’ used but never defined [enabled by default]
static void test(void);
^
How to avoid this?
gcc
is being smart here because you've marked the function as static, meaning that it's expected be defined in this translation unit.
The first thing I'd do is get rid of the static
specifier. This will permit (but not require) you to define it in a different translation unit so gcc
will not be able to complain at the point of compilation.
That may introduce other problems, we'll have to see.
You could use the symbol renaming pragma
asm(".pushsection .slen,\"awx\",@progbits;"
".type test1_in_asm, @function;"
"test1_in_asm: jmp 0;"
".popsection;");
static void test(void);
#pragma redefine_extname test test1_in_asm
or (using the same asm
chunk above) the asm labels:
static void test(void) asm("test1_in_asm");
or perhaps the diagnostic pragmas to selectively avoid the warning
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With