c++ 多个动态库中,调用了static 变量,输出地址完全不相同,这种情况怎么解决?

c++ 多个动态库中,调用了static 变量,输出地址完全不相同,这种情况怎么解决?具体如下代码:

一个基类:


class BaseClass
{
public:
    BaseClass(){}
    ~BaseClass() {}

public:
    virtual void printValue() = 0;
};

typedef BaseClass* create_t();
typedef void destroy_t(BaseClass*);

这是我想要共用的static类:

#ifndef TESTCLASS_H
#define TESTCLASS_H
#include <iostream>

class TestClass{
private:
    TestClass()
    {
       _value = 0;
    }
public:
    ~TestClass()
    {
       _value = 0;
    }
    void printValue();

    void addValue(int pNum);
private:
    int _value;
private:
    friend TestClass* getTestClass();

};

inline TestClass* getTestClass()
{
    static TestClass s_TestClass;
    return &s_TestClass;
}
#endif // TESTCLASS_H



void TestClass::printValue()
{
    std::cout <<_value << std::endl;
}


void TestClass::addValue(int pNum)
{
    _value += pNum;
    std::cout <<_value << std::endl;
}

然后创建了一个动态库,里面很简单,如下:

#include <baseclass.h>

class UNTITLED1SHARED_EXPORT Untitled1 : public BaseClass
{

public:
    Untitled1();

    virtual void printValue();
};



Untitled1::Untitled1()
{
}

void Untitled1::printValue()
{
    TestClass *testPhreadObj = getTestClass();
    std::cout << "Untitled1 = " << testPhreadObj << std::endl;

    getTestClass()->printValue();
}

// the class factories
extern "C" BaseClass* create()
{
    return new Untitled1();
}

extern "C" void destroy(BaseClass* p)
{
    delete p;
}

在main函数中调用:

#include "mainwindow.h"
#include <QApplication>
#include <iostream>
#include <time.h>
#include <unistd.h>
#include "testclass.h"
#include "baseclass.h"
#include <dlfcn.h>

static void* testPhread(void *pVoid)
{
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);


    //open lib
    void* libtest = dlopen("/home/topband/untitled1/bin_linux-g++_release/libuntitled1.so", RTLD_LAZY);
    if (!libtest) {
        return NULL;
    }
    create_t* create_test = (create_t*)dlsym(libtest, "create");
    const char* dlsym_error = dlerror();
    if (dlsym_error) {
       return NULL;
    }

    BaseClass *testObj = create_test();

    testObj->printValue();
    return NULL;
}

int main(int argc, char *argv[])
{
    pthread_t                       _pThread;

    TestClass* mainObj = getTestClass();
    std::cout << "mainObj = " << mainObj << std::endl;

    getTestClass()->printValue();
    getTestClass()->addValue(10);

    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    if (pthread_create(&_pThread, &attr, testPhread, NULL) == 0) {
    }
    else {

        _pThread = 0;
    }
    pthread_attr_destroy(&attr);

    sleep(100);
}


运行结果及报错内容

mainObj = 0x55555575a01c
0
10
RTTI symbol not found for class 'Untitled1'
RTTI symbol not found for class 'Untitled1'
RTTI symbol not found for class 'Untitled1'
RTTI symbol not found for class 'Untitled1'
RTTI symbol not found for class 'Untitled1'
RTTI symbol not found for class 'Untitled1'
RTTI symbol not found for class 'Untitled1'
Untitled1 = 0x7777705b10b0
/home/topband/build-untitled-unknown-Debug/untitled: symbol lookup error: /home/topband/untitled1/bin_linux-g++_release/libuntitled1.so: undefined symbol: _ZN9TestClass10printValueEv

两个地方输出的地址完成不同,如何解决?

我想要达到的结果

去掉inline TestClass* getTestClass()的inline看看地址是否一致

跨进程全局静态变量需要放在共享内存