Cut*_*low 6 gcc warnings suppress-warnings linker-warning
我一直在讨伐最近,以消除我们的代码的警告,并更加熟悉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>
Run Code Online (Sandbox Code Playgroud)
我得到这个的原因是因为我使用的库是使用default可见性构建的,而我的应用程序是使用hidden可见性构建的.我通过hidden可见性重建库来解决这个问题.
我的问题是:如果我愿意,我该如何压制这个警告?这不是我现在需要做的事情,我已经弄清楚如何解决它但我仍然很好奇你是如何压制那个特别的警告 - 或者一般的链接器警告?
使用-fdiagnostics-show-optionfor for C/C++/linker标志并没有说明警告的来源与其他编译器警告一样.
实际上,您不能禁用GCC链接器警告,因为它存储在要链接的二进制库的特定部分中。(该部分被称为.gnu.warning。符号)
但是,您可以像这样将其静音(这是从libc-symbols.h中提取的):
没有它:
#include <sys/stat.h>
int main()
{
lchmod("/path/to/whatever", 0666);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给出:
$ gcc a.c
/tmp/cc0TGjC8.o: in function « main »:
a.c:(.text+0xf): WARNING: lchmod is not implemented and will always fail
Run Code Online (Sandbox Code Playgroud)
禁用时:
#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;
}
Run Code Online (Sandbox Code Playgroud)
给出:
$ gcc a.c
/tmp/cc195eKj.o: in function « main »:
a.c:(.text+0xf): WARNING:
Run Code Online (Sandbox Code Playgroud)
隐藏:
#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;
}
Run Code Online (Sandbox Code Playgroud)
给出:
$ gcc a.c
/tmp/cc195eKj.o: in function « main »:
Hello world!
Run Code Online (Sandbox Code Playgroud)
显然,在这种情况下,请Hello world!为您的精彩项目替换多个空间或添加一些广告。
不幸的是 ld 似乎没有任何内在的方法来抑制特定选项。我发现有用的一件事是通过传递给 g++ 来限制重复警告的数量-Wl,--warn-once(或者您可以直接传递--warn-once给 ld)。
| 归档时间: |
|
| 查看次数: |
5856 次 |
| 最近记录: |