如何抑制GCC链接器警告?

如何抑制GCC链接器警告?,gcc,warnings,suppress-warnings,linker-warning,Gcc,Warnings,Suppress Warnings,Linker Warning,我最近一直在努力消除代码中的警告,并对GCC警告标志(如-Wall,-Wno-,-fdiagnostics show option)更加熟悉。但是,我还没有弄清楚如何禁用(甚至控制)链接器警告。我收到的最常见的链接器警告如下: ld: warning: <some symbol> has different visibility (default) in <path/to/library.a> and (hidden) in <path/to/my/class.o

我最近一直在努力消除代码中的警告,并对GCC警告标志(如
-Wall
-Wno-
-fdiagnostics show option
)更加熟悉。但是,我还没有弄清楚如何禁用(甚至控制)链接器警告。我收到的最常见的链接器警告如下:

ld: warning: <some symbol> has different visibility (default) in 
<path/to/library.a> and (hidden) in <path/to/my/class.o>
ld:warning:在中具有不同的可见性(默认)
和(隐藏)在
我之所以得到这个结果,是因为我使用的库是使用
默认的
可见性构建的,而我的应用程序是使用
隐藏的
可见性构建的。我通过使用
隐藏
可见性重建库修复了这个问题

但我的问题是:如果我想的话,我将如何压制这一警告?这不是我现在需要做的事情,因为我已经找到了修复它的方法,但是我仍然很好奇你会如何抑制那个特别的警告,或者任何链接器警告


对任何C/C++/链接器标志使用
-fddiagnostics show选项
,都不会像其他编译器警告一样说明该警告来自何处。

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

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

但是,您可以将其静音,如下所示(这是从libc symbols.h中提取的):

没有它:

#include <sys/stat.h>

int main()
{
    lchmod("/path/to/whatever", 0666);
    return 0;
}
禁用时:

#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;
}
使用隐藏:

#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;
}

显然,在这种情况下,替换
helloworld通过多个空格或一些广告来宣传您的精彩项目。

ld的手册页没有说明有任何选项可以关闭链接器警告:(
$ 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!