DLL文件中的多个类 [英] Multiple Classes in DLL file
问题描述
我在这里遇到的问题几乎是一个愚蠢的问题,但我只是无法在网络上找到答案。
当我想创建一个DLL项目(不在一个现有的c ++项目中)时,我读到我需要在.h文件的开头实现以下几行:
what I've got here is pretty much a dumb and noobie question, but I just can`t find an answer on the web. When I want to create a DLL project (out of an existing c++ project), I read that I need to implement the following lines in the beginning of the .h files:
#ifdef _EXPORTING
#define CLASS_DECLSPEC __declspec(dllexport)
#else
#define CLASS_DECLSPEC __declspec(dllimport)
#endif
我看了MSDN中的示例:
I looked at the example in the MSDN:
// MathFuncsDll.h
#ifdef MATHFUNCSDLL_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport)
#else
#define MATHFUNCSDLL_API __declspec(dllimport)
#endif
现在我想了解一下,是否需要更改我进行的每个新课程的 _EXPORTING和 CLASS_DECLSPEC?
例如,如果要在与 MathFuncsDll.h相同的项目中创建一个名为 foo的类,则需要将以下行放在.h文件的开头:
Now I want to understand, do I need to change the "_EXPORTING" and the "CLASS_DECLSPEC" for every new class I make? For example if I'd create a class named "foo" within the same project as "MathFuncsDll.h" I'd need to put the following lines at the start of the .h file:
// FooDll.h
#ifdef FOO_EXPORTS
#define FOO_API __declspec(dllexport)
#else
#define FOO_API __declspec(dllimport)
#endif
在项目的所有.h文件中都一样?
Or is some line is the same in all .h files of the project?
还有另一件事,如果我使用命名空间
来引用整个dll作为一个文件并从中提取类,是否需要在每个.h文件中将使用命名空间* NAME *
放入?
And another thing, if I use a namespace
to reference the whole dll as one and extract the classes from it, do I need to put the using namespace *NAME*
in every .h file?
推荐答案
不,您不必为每个类创建新的宏:
No, you need not to create new macros for each classes:
class MATHFUNCSDLL_API Foo {...};
class MATHFUNCSDLL_API Boo {...};
class MATHFUNCSDLL_API MyNewClass {...};
第二个问:不要在头文件中使用命名空间:使用名称空间在c ++标题中
For the second q: don't use using namespace inside header file: "using namespace" in c++ headers
您的标题可能如下所示:
Your header can looks like the following:
#pragma once
namespace foo {
class MATHFUNCSDLL_API Foo {...};
class MATHFUNCSDLL_API Boo {...};
class MATHFUNCSDLL_API MyNewClass {...};
}
已编辑
// mylibdef.h
#pragma once
#ifdef _EXPORTING
#define CLASS_DECLSPEC __declspec(dllexport)
#else
#define CLASS_DECLSPEC __declspec(dllimport)
#endif
// myclass1.h
#pragma once
#include "mylibdef.h"
namespace mylib {
class CLASS_DECLSPEC MyClass1 {...};
}
// myclass2.h
#pragma once
#include "mylibdef.h"
namespace mylib {
class CLASS_DECLSPEC MyClass2 {...};
}
这篇关于DLL文件中的多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!