Exporting global variables from DLL

江枫思渺然 提交于 2019-12-03 09:52:24

In your header:

API extern const Foo foo;

In your source file:

API const Foo foo;

If you don't have the extern keyword, your C compiler assumes you mean to declare a local variable. (It doesn't care that you happened to have included the definition from a header file.) You also need to tell the compiler that you're planning on exporting the variable when you actually declare it in your source file.

The class Foo most likely will have member functions in reality, calling those from another module would cause linker errors with the OP/accepted answer. The class must be defined as dll export/import as well in order to use the exported instance of it outside this module to eliminate the link errors.

class API Foo
{
public:
    Foo()
    {}
    void DoSomeWork(); // calling this would cause link error if Foo is not defined as import/export class
};

With that said, it might be better to rename #define API with something like DLLEXPORT so it makes sense for both APIs and export class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!