获取GCC链接器警告多个函数定义 [英] Getting GCC linker to warn about multiple function definitions

查看:246
本文介绍了获取GCC链接器警告多个函数定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑我的小例子C库:

Consider my small example C library:

#include <external_library.h>

void some_function(void)
{
    external_library_call();
    // Do other stuff...
}

它计划制作some_function()公开可调用。但是,该库不起作用,因为它需要的外部库也恰好使用了一个名为some_function()的函数,该函数恰好具有相同的原型。尽管如此,GCC的链接器并不在乎有多少个some_function符号来源。它从表面上随机挑选一个,外部库可能会或可能不会使用我的some_function()而不是它自己的。疯了吧。不是图书馆不工作的事实。这个库肯定不应该工作。事实上,符号'some_function'有两个来源,但链接程序对此不做任何事情。而且你知道,这并不会打扰我太多,因为我习惯了GCC和C,一般在病态上都是鲁莽的。但是,有一种方法可以让链接器在相同符号有两个来源时提醒我。我已经尝试过--Wall -Wextra -Wshadow,但是不会产生任何警告。

It plans to make some_function() publicly callable. The library doesn't work, though, because the external library it requires also happens to use a function called some_function(), which happens to have the same prototype. GCC's linker doesn't care how many sources of the some_function symbol there are, though. It picks one seemingly at random and the external library may or may not use my some_function() instead of its own. This is insane. Not the fact that the library doesn't work. This library definitely shouldn't work. It's more the fact that there are two sources for the symbol 'some_function', but the linker doesn't do anything about it. And you know, that doesn't bother me too much because I'm used to GCC and C in general being pathologically reckless by default. There's gotta be a way, though, to get the linker to warn me when there are two sources for the same symbol. I've already tried -Wall -Wextra -Wshadow, but that doesn't produce any warnings.

请注意,-fvisibility = hidden在这里没有帮助,因为这两个库想要导出some_function()。我知道你可以对我进行函数调用时没有一个唯一的前缀而感到羞耻。你是对的。这是一个错误。我不在乎。这个错误可以被链接器捕获,所以应该被捕获。没有理由为什么链接器不应该捕获这个错误。此外,您使用的库可能会导出一些奇怪的意外符号,并且您不一定能控制其他人的库导出内容。在程序员停止并着火之前,前缀和前缀只能是唯一的。

Note that -fvisibility=hidden won't help here because both libraries want to export some_function(). I know you can just say shame on me for making function calls without a unique prefix. You're right. It's a mistake. I don't care. This mistake is catchable by the linker and so should be caught. There is no reason why the linker shouldn't catch this mistake. Besides, the library you're using might export some weird unexpected symbols, and you do not necessarily have control over what somebody else's library exports. That and prefixes can only be made so unique before the programmer halts and catches fire.

推荐答案

Add:

-fvisibility=hidden

到您的构建标志。注意一些警告,但是;一些标题可能不会期待这一点。在这些情况下,您需要在包含它们之前使用一个杂注:

To your build flags. Note some caveats, however; some headers might not be expecting this. In those cases, you need to use a pragma before including them:

#pragma GCC visibility push(hidden)
#include <problematic_header>
#pragma GCC visibility pop

除了避免符号外,还有其他一些好处碰撞。请参阅:

There are some other benefits to this other than just avoiding symbol collisions. See:

http://gcc.gnu。 org / wiki /可见性

如果您有兴趣的话。

这篇关于获取GCC链接器警告多个函数定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆