gcc:如何避免在程序集中定义的函数使用“已使用但未定义”的警告

时间:2014-01-04 03:54:05

标签: c gcc compiler-warnings inline-assembly

对于原因,我正在尝试在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);
             ^

如何避免这种情况?

2 个答案:

答案 0 :(得分:9)

gcc在这里非常聪明,因为您已将该函数标记为静态,这意味着它应该在此翻译单元中定义。

我要做的第一件事是摆脱static说明符。这将允许(但不要求)您在不同的翻译单元中定义它,因此gcc将无法在编译时进行投诉。

可能引入其他问题,我们必须看到。

答案 1 :(得分:1)

您可以使用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

或(使用上面相同的asm块)asm labels

 static void test(void) asm("test1_in_asm");

或者diagnostic pragmas可以选择性地避免警告

相关问题