相关疑难解决方法(0)

Linux上的共享库中的多个单例实例

正如标题所提到的,我的问题很明显,我详细描述了这个场景.在文件singleton.h中有一个名为singleton的类,由singleton模式实现,如下所示:

/*
 * singleton.h
 *
 *  Created on: 2011-12-24
 *      Author: bourneli
 */

#ifndef SINGLETON_H_
#define SINGLETON_H_

class singleton
{
private:
    singleton() {num = -1;}
    static singleton* pInstance;
public:
    static singleton& instance()
    {
        if (NULL == pInstance)
        {
            pInstance = new singleton();
        }
        return *pInstance;
    }
public:
    int num;
};

singleton* singleton::pInstance = NULL;

#endif /* SINGLETON_H_ */
Run Code Online (Sandbox Code Playgroud)

然后,有一个名为hello.cpp的插件如下:

#include <iostream>
#include "singleton.h"

extern "C" void hello() {
    std::cout << "singleton.num in hello.so : " << singleton::instance().num << std::endl;
    ++singleton::instance().num; …
Run Code Online (Sandbox Code Playgroud)

c++ singleton dlopen

36
推荐指数
2
解决办法
2万
查看次数

版本脚本和隐藏的可见性

使用gcc构建共享库时,可以使用限制符号的可见性-fvisibility=hidden.我还了解到,您可以使用version-script选项限制可见性ld.

现在我想知道是否可以将这些结合起来.假设我有一个包含以下内容的程序:

void foobar() {}
void say_hello() {}
Run Code Online (Sandbox Code Playgroud)

然后我有版本脚本文件:

{
  global:
    foobar;
}
Run Code Online (Sandbox Code Playgroud)

我编译它:

gcc -fvisibility=hidden -Wl,--version-script=<version-script> test.c -shared -o libtest.so
Run Code Online (Sandbox Code Playgroud)

当我nm之后运行时,我发现没有符号被导出.无论如何,我可以将默认可见性设置为隐藏,并使用版本脚本(或其他东西)导出符号?

gcc shared-libraries ld

9
推荐指数
1
解决办法
8737
查看次数

标签 统计

c++ ×1

dlopen ×1

gcc ×1

ld ×1

shared-libraries ×1

singleton ×1