__attribute __((visibility(“ default”)))是否适用于嵌套结构?

时间:2018-08-24 17:52:27

标签: c++ gcc attributes visibility

当我用C ++编写类并使用Pimpl习惯用法时,我通常会设置 像这样的结构:

Foo.hpp

class Foo
{
public:
    Foo();
    ~Foo();


    void method1();
    void method2();

private:
    struct impl;
    std::unique_ptr<impl> p;
};

Foo.cpp

struct Foo::impl
{
    method1()
    {
        ....
    }
    method2()
    {
        ...
    }

    private_thing()
    {
        ...
    }

}

Foo::Foo():p(make_unique<impl>()){}
Foo::~Foo(){}
void Foo::method1(){ p->method1();}
void Foo::method2(){ p->method2();}

我想隐藏一些实现的符号,所以我 用-fvisibility=hidden编译并添加 从__attribute__((visibility("default")))class Foo的定义,如下所示:

class __attribute__((visibility("default"))) Foo {...};

我的问题是,可见性属性是否适用于方法 嵌套的impl结构的并使所有成员可见,或者是否 他们保持隐藏状态。

1 个答案:

答案 0 :(得分:0)

使用“ nm -CD foo.so”检查代码,我们可以看到嵌套结构实际上是可见的:

library(tidyverse)
library(plotly)

set.seed(55)
df <- data.frame(
  A = c(rep(0, 8), rep(1, 8)),
  B = rep(c(rep(0, 4), rep(1, 4)), 2),
  C = rep(c(rep(0, 2), rep(1, 2)), 4),
  Y = rep(c(0, 1), 8),
  X1 = runif(16)
)

p <- ggplot(df, aes(x = factor(Y), y = X1, fill = factor(Y), A = A, B = B, C = C))
p <- p + geom_boxplot() +
  geom_point() +
  xlab("Y") +
  guides(fill = guide_legend("Y")) +
  theme(legend.position = "top")
final.p <- ggplotly(p, tooltip = c("A", "B", "C"))
final.p

但是,将0000000000201048 B __bss_start w __cxa_finalize 0000000000201048 D _edata 0000000000201050 B _end 00000000000009cc T _fini w __gmon_start__ 0000000000000780 T _init w _ITM_deregisterTMCloneTable w _ITM_registerTMCloneTable w _Jv_RegisterClasses U operator delete(void*) 000000000000099c W Foo::impl::method1() 00000000000009a8 W Foo::impl::method2() 00000000000009b4 W Foo::impl::impl() 00000000000009b4 W Foo::impl::impl() 0000000000000960 T Foo::method1() 000000000000097e T Foo::method2() 0000000000000910 T Foo::Foo() 0000000000000910 T Foo::Foo() 0000000000000942 T Foo::~Foo() 0000000000000942 T Foo::~Foo() U operator new(unsigned long) 声明为非嵌套结构Foo::impl,我们将得到以下内容:

Foo_impl

如此看来,是的,可见性说明符确实在向嵌套结构推进。