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

Dol*_*000 4 c gcc inline-assembly compiler-warnings

对于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();
    /* ... */
}
Run Code Online (Sandbox Code Playgroud)

然后,

$ gcc -c -o test.o test.c
test.c:74:13: warning: ‘test’ used but never defined [enabled by default]
 static void test(void);
             ^
Run Code Online (Sandbox Code Playgroud)

怎么避免这个?

pax*_*blo 9

gcc 这里很聪明,因为你已经将这个函数标记为静态,这意味着它应该在这个翻译单元中定义.

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

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

  • @ Dolda2000,一个非静态函数的_definition_会使它可见,你有一个_declaration._我怀疑你会发现它没关系. (2认同)