如何抑制 GCC 链接器警告?
我最近一直致力于消除代码中的警告,并且更加熟悉 GCC 警告标志(例如 -Wall
、-Wno-
ld: warning: <some symbol> has different visibility (default) in
<path/to/library.a> and (hidden) in <path/to/my/class.o>
我收到此警告的原因是因为我使用的库是使用 default
可见性构建的,而我的应用程序是使用 构建的隐藏可见性。我通过重建具有隐藏可见性的库来修复此问题。
但我的问题是:如果我愿意,我将如何抑制该警告?既然我已经弄清楚如何修复它,这不是我需要做的事情,但我仍然很好奇您将如何抑制该特定警告 - 或一般的任何链接器警告?
对任何 C/C++/链接器标志使用 -fdiagnostics-show-option
并不会像其他编译器警告一样说明该警告的来源。
I've been on a crusade lately to eliminate warnings from our code and have become more familiar with GCC warning flags (such as -Wall
, -Wno-<warning to disable>
, -fdiagnostics-show-option
, etc.). However I haven't been able to figure out how to disable (or even control) linker warnings. The most common linker warning that I was getting is of the following form:
ld: warning: <some symbol> has different visibility (default) in
<path/to/library.a> and (hidden) in <path/to/my/class.o>
The reason I was getting this was because the library I was using was built using the default
visibility while my application is built with hidden
visibility. I've fixed this by rebuilding the library with hidden
visibility.
My question though is: how would I suppress that warning if I wanted to? It's not something that I need to do now that I've figured out how to fix it but I'm still curious as to how you'd suppress that particular warning — or any linker warnings in general?
Using the -fdiagnostics-show-option
for any of the C/C++/linker flags doesn't say where that warning comes from like with other compiler warnings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您无法禁用 GCC 链接器警告,因为它存储在您要链接的二进制库的特定部分中。 (该部分称为 .gnu.warning.symbol)
但是,您可以将其静音,如下所示(从 libc-symbols.h 中提取):
没有它:
给出:
禁用:
给出:
带有隐藏:
给出:
显然,在这种情况下,用多个空格或一些广告来替换
Hello world!
为您的精彩项目。Actually, you can't disable a GCC linker warning, as it's stored in a specific section of the binary library you're linking with. (The section is called .gnu.warning.symbol)
You can however mute it, like this (this is extracted from libc-symbols.h):
Without it:
Gives:
With disabling:
gives:
With hiding:
gives:
Obviously, in that case, replace
Hello world!
either by multiple space or some advertisement for your wonderful project.不幸的是 ld 似乎没有任何内在的方法来抑制特定选项。我发现有用的一件事是通过将
-Wl,--warn-once
传递给 g++ 来限制重复警告的数量(或者您可以直接传递--warn-once
到ld)。Unfortunately ld does not appear to have any intrinsic way of suppressing specific options. One thing that I found useful was limiting the number of duplicate warnings by passing
-Wl,--warn-once
to g++ (or you can pass--warn-once
directly to ld).