如何在可见性生效时导出公共内部/嵌套类

How to export a public inner/nested class when visibility is in effect?

本文关键字:内部 嵌套 可见性      更新时间:2023-10-16

我正在整理一个补丁,增加了Crypto++库的可见性。根据GCC可见性维基:

在二进制

文件以外的二进制文件中捕获用户定义类型的异常 抛出异常的一个需要类型信息查找...然而 这不是故事的全部 - 它变得更加困难。符号可见性为 默认情况下为"默认",但如果链接器只遇到一个定义 隐藏它 - 只有一个 - 该类型信息符号成为永久 隐藏(记住C++标准的ODR - 一个定义规则)。

我的结论:所有内容(包括基类)都需要导出或用__attribute__ ((visibility ("default")))装饰。所以我从异常类引用中得到了一个异常列表,并且......

class CRYPTOPP_DLL AlgorithmParametersBase
{
public:
    class ParameterNotUsed : public Exception
    {
    public: 
            ...
        }
}

然后:

cryptopp$ nm -D libcryptopp.so | c++filt | grep ParametersNotUsed
cryptopp$

如果我用class CRYPTOPP_DLL ParameterNotUsed : public Exception重建,那么我会得到相同的结果:

class CRYPTOPP_DLL AlgorithmParametersBase
{
public:
    class CRYPTOPP_DLL ParameterNotUsed : public Exception
    {
    public: 
            ...
        }
}

现在,我相当确定基类已导出:

$ nm -D libcryptopp.so | c++filt | grep Exception
00000000004d6980 V typeinfo for CryptoPP::Exception
0000000000230700 V typeinfo name for CryptoPP::Exception
00000000004d6bf0 V vtable for CryptoPP::Exception

我的问题:

  • 如何导出内部或嵌套类,因为它似乎没有导出?
  • nm -D用于验证typeinfo信息的正确工具吗?
  • 我能捕捉到ParameterNotUsed异常吗(我怀疑不是)?
  • 我是否能够捕获Exception基类异常(我怀疑是这样)?

万一重要:

$ gcc --version
gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.

您是否尝试过像这样导出内部类:

class Exception {};
class __declspec(dllexport) AlgorithmParametersBase
{
public:
    class ParameterNotUsed : public Exception
    {
    public:
    };
};
// exporting known type
class __declspec(dllexport) AlgorithmParametersBase::ParameterNotUsed;

但是,我没有测试上述调用,但它编译得很好。