在导入到命名空间中并导入到全局命名空间中的C函数之间的冲突 [英] Clash between C functions imported into a namespace and imported into the global namespace

查看:144
本文介绍了在导入到命名空间中并导入到全局命名空间中的C函数之间的冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我在一个命名空间中加入OpenSSL头文件,如下所示:

In my code I bring the OpenSSL headers in in a namespace, like this:

#include <cstdio>
namespace OpenSSL {
    #include <openssl/ssl.h>
    #include <openssl/err.h>
}

但我刚刚发现这似乎会导致事情爆炸,如果我尝试这样做时使用Boost ASIO,它有OpenSSL支持,但似乎带来了OpenSSL符号到全局命名空间。有什么我可以做的,或者我只需要离开所有的OpenSSL库的符号在全局命名空间?

But I've just discovered that this seems to cause things to explode if I try to do that when working with Boost ASIO, which has OpenSSL support, but appears to bring the OpenSSL symbols in to the global namespace. Is there anything I can do about this, or do I just have to leave all of the OpenSSL library's symbols in the global namespace?

我只是想想尝试一个使用命名空间OpenSSL包含我的标题后,但不幸的是导致错误,如:

I did just think of trying a "using namespace OpenSSL" in the offending file after including my header, but that unfortunately causes errors such as:

/usr/include/openssl/x509v3.h:83:13: error: reference to ‘v3_ext_ctx’ is ambiguous
/usr/include/openssl/x509v3.h:71:8: error: candidates are: struct v3_ext_ctx
/usr/include/openssl/ossl_typ.h:160:16: error:                 struct OpenSSL::v3_ext_ctx

(注意,OpenSSL是一个C库,而不是一个C ++库,因此原始函数在被引入到C ++编译单元之前不在任何命名空间中,我的技术是由Stroustrup在他的书C ++编程语言特别版。从命名空间中的第9.5节建议:[8] #include C头文件,以避免全局名称;§8.2.9.1,§9.2.2 p>

(Note that OpenSSL is a C library, not a C++ library, and thus the original functions are not in any namespace until brought in to a C++ compilation unit. My technique is recommended by Stroustrup in his book The C++ Programming Language, Special Edition. From section 9.5, "Advice": "[8] #include C headers in namespaces to avoid global names; §8.2.9.1, §9.2.2."

推荐答案

所以问题似乎是这样的:OpenSSL的符号只能带来一次;第二个 #include 由于包含守卫而没有效果。这意味着它们只能被引入到编译单元中的一个命名空间。

So the issue seems to be this: OpenSSL's symbols can be brought in only once; a second #include of them will have no effect due to the include guards. That means that they can be brought in to only one namespace within a compilation unit.

因此,如果你要在编译单元中使用Boost.ASIO,它要求它们在全局命名空间中,您可以自己将它们放入全局命名空间中(在 #include< boost / asio.hpp> 之前或让 include< boost / asio.hpp> 这样做。

Thus, if you're going to work with Boost.ASIO in your compilation unit, which requires them to be in the global namespace, you'll have either to bring them in to the global namespace yourself (before you #include <boost/asio.hpp> or let #include <boost/asio.hpp> do that.

这篇关于在导入到命名空间中并导入到全局命名空间中的C函数之间的冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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