如何正确导出DLL中的向量

问题描述 投票:0回答:1

我在DLL visual studio项目中有一个向量,我想在我的可执行项目和另一个DLL中使用。我希望所有项目都使用向量的相同实例。我不希望每个项目都创建自己的实例。我怎样才能做到这一点?

//  DLL A project
//  A.h
extern std::vector<int> ids;

//  A.cpp
std::vector<int> ids;

//  DLL B project
//  B.h
void foo();

//  B.cpp
#include "A.h"

void foo()
{
    ids.push_back(2);
}



//  executable project
//  main.cpp
#include "A.h"
#include "B.h"

int main()
{   
    foo();
    ids.push_back(1);

    //  should print 21
    for(auto i : ids)
    {
        std::cout << i << std::endl;
    }
    return 0;
}
c++ visual-studio dll
1个回答
0
投票

不得不将此添加到我的DLL项目和可执行项目中。 USE_DLL仅在DLL项目中定义,其中包含.h / .cpp。

#ifdef USE_DLL
#define DllAPI   __declspec( dllexport )
#else
#define DllAPI   __declspec( dllimport )
#endif
© www.soinside.com 2019 - 2024. All rights reserved.