我最近一直致力于消除代码中的警告,并且更加熟悉 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
重建库来解决这个问题可见性。
但我的问题是:如果我愿意,我将如何抑制该警告?既然我已经弄清楚如何修复它,这不是我需要做的事情,但我仍然很好奇您将如何抑制该特定警告 - 或一般的任何链接器警告?
使用-fdiagnostics-show-option
对于任何 C/C++/链接器标志,都不会像其他编译器警告一样说明该警告来自何处。
最佳答案
实际上,您无法禁用 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!
替换为多个空格或一些有关您精彩项目的广告。
关于gcc - 如何抑制 GCC 链接器警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3409448/