C++ 如何在windows中导出类模板静态变量DLL
我不知道如何将模板中的静态变量导出到我的可执行文件中 我使用的entityx库具有组件模板: 实体h:C++ 如何在windows中导出类模板静态变量DLL,c++,windows,gcc,dll,entityx,C++,Windows,Gcc,Dll,Entityx,我不知道如何将模板中的静态变量导出到我的可执行文件中 我使用的entityx库具有组件模板: 实体h: struct BaseComponent { public: typedef size_t Family; protected: static Family family_counter_; //THIS!! }; template <typename Derived> struct Component : public BaseComponent { public:
struct BaseComponent {
public:
typedef size_t Family;
protected:
static Family family_counter_; //THIS!!
};
template <typename Derived>
struct Component : public BaseComponent {
public:
typedef ComponentHandle<Derived> Handle;
typedef ComponentHandle<const Derived, const EntityManager> ConstHandle;
private:
friend class EntityManager;
/// Used internally for registration.
static Family family();
};
现在我有了链接到entityx的库,并使用它:
World.hpp:
#include <entityx/entityx.h>
namespace edv {
class Transform : public entityx::Component<Transform> {
public:
Transform();
};
class World {
public:
World();
entityx::Entity createEntity();
private:
entityx::EventManager m_events;
entityx::EntityManager m_entities;
};
}
我调试了应用程序,似乎有两个family_counter_静态变量实例,一个用于我的dll,另一个用于可执行文件myprogram.exe
当我在Linux中编译相同的代码时,它会按预期工作:
createEntity componenthdnlr OK!!
test componenthndlr OK!!
所以我认为这应该是关于Windows DLL在GCC中的可见性。
我是这个Windows DLL世界的新手,我不知道我必须在哪里导出什么,才能让它工作
我使用MSYS2和GCC 5.2.0来编译这个。
谢谢我不知道这是否是最好的解决方案,但它确实有效: 外部模板类entityx::Component 这样,它只创建该静态变量的一个实例
#include <Endavant/game/World.h>
#include <iostream>
namespace edv {
Transform::Transform() {
}
World::World(): m_entities(m_events){
}
void World::update() {
}
entityx::Entity World::createEntity() {
auto e = m_entities.create();
e.assign<Transform>();
auto c = e.component<Transform>();
if (c.valid()) {
std::cout<<"createEntity componenthdnlr OK!!"<<e.id()<<std::endl;
} else {
std::cout<<"createEntity componenthdnlr INVALID!!"<<e.id()<<std::endl;
}
return e;
}
}
#include <Endavant/Root.h>
#include <Endavant/game/World.h>
void test() {
auto entity = edv::Root::get().getWorld().createEntity();
auto comp = entity.component<edv::Transform>();
if (comp.valid()) {
std::cout<<"test componenthndlr VALID!!"<<entity.id()<<std::endl;
} else {
std::cout<<"test componenthndlr INVALID!!"<<entity.id()<<std::endl;
}
}
int main() {
try {
edv::Root::get().init();
test();
edv::Root::get().run();
} catch (std::exception & e) {
std::cout<<e.what()<<std::endl;
}
return 0;
}
createEntity componenthdnlr OK!!
test componenthndlr INVALID!!
createEntity componenthdnlr OK!!
test componenthndlr OK!!