[英]gcc: How to avoid “used but never defined” warning for function defined in assembly
對於Reasons,我正在嘗試在GCC中使用頂級程序集來定義一些靜態函數。 但是,由於GCC沒有“看到”這些函數的主體,它警告我它們“被使用但從未定義過。一個簡單的源代碼示例可能如下所示:
/* 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();
/* ... */
}
接着,
$ gcc -c -o test.o test.c
test.c:74:13: warning: ‘test’ used but never defined [enabled by default]
static void test(void);
^
怎么避免這個?
gcc
在這里很聰明,因為你已經將函數標記為靜態,這意味着它應該在這個翻譯單元中定義。
我要做的第一件事是擺脫static
說明符。 這將允許(但不要求)您在不同的翻譯單元中定義它,因此gcc
將無法在編譯時進行投訴。
這可能會引入其他問題,我們必須看到。
您可以使用符號重命名編譯指示
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
或者(使用與上面相同的asm
塊) asm標簽 :
static void test(void) asm("test1_in_asm");
或者可能是診斷性的pragma,以選擇性地避免警告
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.