【问题标题】:gcc: How to avoid "used but never defined" warning for function defined in assemblygcc:如何避免程序集中定义的函数的“使用但从未定义”警告
【发布时间】:2014-01-04 03:54:04
【问题描述】:

出于某些原因,我试图在 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);
             ^

如何避免这种情况?

【问题讨论】:

  • 为什么不能使用asminside函数体?
  • 因为我希望能有函数的地址,在几个地方使用。
  • 看起来你正在重新发明dynamic linking的过程链接表
  • 是的,这是我的目的之一;但这并不是说我可以在自己的系统中重用现有的 dynlinker。 :)

标签: c gcc compiler-warnings inline-assembly


【解决方案1】:

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

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

可能会引入其他问题,我们将拭目以待。

【讨论】:

  • 哦,对了。我的下意识反应是,使原型非static 会导出符号,但现在你提到它,情况可能并非如此。 :)
  • @Dolda2000,非静态函数的定义会使其可见,你所拥有的是一个声明。我怀疑你'会发现没事的。
【解决方案2】:

您可以使用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 来选择性地避免警告

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    相关资源
    最近更新 更多