【问题标题】:How do you suppress GCC linker warnings?你如何抑制 GCC 链接器警告?
【发布时间】:2011-03-25 11:29:53
【问题描述】:

我最近一直在努力消除代码中的警告,并且更加熟悉 GCC 警告标志(例如 -Wall-Wno-<warning to disable>-fdiagnostics-show-option 等)。但是我无法弄清楚如何禁用(甚至控制)链接器警告。我得到的最常见的链接器警告是以下形式:

ld: warning: <some symbol> has different visibility (default) in 
<path/to/library.a> and (hidden) in <path/to/my/class.o>

我得到这个的原因是因为我使用的库是使用 default 可见性构建的,而我的应用程序是使用 hidden 可见性构建的。我已通过使用 hidden 可见性重建库来解决此问题。

但我的问题是:如果我愿意,我将如何抑制该警告?这不是我现在需要做的事情,因为我已经想出了如何解决它,但我仍然很好奇你将如何抑制该特定警告 - 或一般的任何链接器警告?

对任何 C/C++/链接器标志使用 -fdiagnostics-show-option 并不能像其他编译器警告一样说明该警告的来源。

【问题讨论】:

  • ld 的手册页没有说有任何选项可以关闭链接器警告:(

标签: gcc warnings suppress-warnings linker-warning


【解决方案1】:

不幸的是,ld 似乎没有任何隐藏特定选项的内在方法。我发现有用的一件事是通过将 -Wl,--warn-once 传递给 g++(或者您可以将 --warn-once 直接传递给 ld)来限制重复警告的数量。

【讨论】:

    【解决方案2】:

    实际上,您无法禁用 GCC 链接器警告,因为它存储在您要链接的二进制库的特定部分中。 (该部分称为 .gnu.warning。符号

    您可以像这样将其静音(这是从 libc-symbols.h 中提取的):

    没有它:

    #include <sys/stat.h>
    
    int main()
    {
        lchmod("/path/to/whatever", 0666);
        return 0;
    }
    

    给予:

    $ gcc a.c
    /tmp/cc0TGjC8.o: in function « main »:
    a.c:(.text+0xf): WARNING: lchmod is not implemented and will always fail
    

    禁用:

    #include <sys/stat.h>
    
    /* We want the .gnu.warning.SYMBOL section to be unallocated.  */
    #define __make_section_unallocated(section_string)    \
      __asm__ (".section " section_string "\n\t.previous");
    
    /* When a reference to SYMBOL is encountered, the linker will emit a
       warning message MSG.  */
    #define silent_warning(symbol) \
      __make_section_unallocated (".gnu.warning." #symbol) 
    
    silent_warning(lchmod)
    
    int main()
    {
        lchmod("/path/to/whatever", 0666);
        return 0;
    }
    

    给予:

    $ gcc a.c
    /tmp/cc195eKj.o: in function « main »:
    a.c:(.text+0xf): WARNING:
    

    隐藏:

    #include <sys/stat.h>
    
    #define __hide_section_warning(section_string)    \
        __asm__ (".section " section_string "\n.string \"\rHello world!                      \"\n\t.previous");
    
    /* If you want to hide the linker's output */
    #define hide_warning(symbol) \
      __hide_section_warning (".gnu.warning." #symbol) 
    
    
    hide_warning(lchmod)
    
    int main()
    {
        lchmod("/path/to/whatever", 0666);
        return 0;
    }
    

    给予:

    $ gcc a.c
    /tmp/cc195eKj.o: in function « main »:
    Hello world!
    

    显然,在这种情况下,将Hello world! 替换为多个空格或为您的精彩项目添加一些广告。

    【讨论】:

      猜你喜欢
      • 2013-02-01
      • 2021-12-23
      • 2012-10-02
      • 2010-12-24
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      相关资源
      最近更新 更多