C++ dllexport/dllimport 开关的宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14980649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:51:42  来源:igfitidea点击:

Macro for dllexport/dllimport switch

c++visual-studio-2010visual-c++

提问by Blub

#if COMPILING_DLL
    #define DLLEXPORT __declspec(dllexport)
#else
    #define DLLEXPORT __declspec(dllimport)
#endif

How / where do I define COMPILING_DLL?

我如何/在哪里定义COMPILING_DLL

Seen here: what does __declspec(dllimport) really mean?

在这里看到: __declspec(dllimport) 真正意味着什么?

Sounds like I can't use load-time dynamic linking at all if I can't use the same header?

听起来如果我不能使用相同的标题,我就不能使用加载时动态链接?

enter image description here

enter image description here

回答by SridharKritha

One another option:

另一种选择:

Use the defaultdefined macro localto the project.

使用项目本地默认定义宏。

You can see the default defined macros local to the project in the below location:

您可以在以下位置看到项目本地默认定义的宏:

Properties -> C/C++ -> Preprocessor -> Preprocessor Definition.

属性 -> C/C++ -> 预处理器 -> 预处理器定义。

Example:

例子:

Suppose your Project Name is: MyDLL

假设您的项目名称是:MyDLL

Default Macro Local to that project: MYDLL_EXPORTS

该项目的默认宏本地:MYDLL_EXPORTS

 #ifdef  MYDLL_EXPORTS 
    /*Enabled as "export" while compiling the dll project*/
    #define DLLEXPORT __declspec(dllexport)  
 #else
    /*Enabled as "import" in the Client side for using already created dll file*/
    #define DLLEXPORT __declspec(dllimport)  
 #endif

回答by ?? Tiib

Best place to define COMPILING_DLL=1is command line of compiler. If you use Visual Studio IDE then it is in Project properties ... C/C++ ... Preprocessor ... Preprocessor Definitions.

定义的最佳位置COMPILING_DLL=1是编译器的命令行。如果您使用 Visual Studio IDE,那么它位于项目属性...C/C++...预处理器...预处理器定义中。

__declspec(dllimport)is Microsoft specific extension to C++. Microsoft has excellent online documentation.

__declspec(dllimport)是 Microsoft 对 C++ 的特定扩展。Microsoft 有出色的在线文档。

回答by Some programmer dude

In the DLL project, you add a #define(either in a header file or in the project properties) for COMPILING_DLL. As this will not be set for any other project (especially if you name it something better than COMPILING_DLL) then the #ifdirective will work properly.

在 DLL 项目中,您#defineCOMPILING_DLL. 由于这不会为任何其他项目设置(特别是如果您将其命名为比 更好的名称COMPILING_DLL),那么该#if指令将正常工作。

回答by Ferenc Deak

You (actually Visual Studio in ideal cases) defines the COMPILING_DLLas an argument to the compiler when you build the DLL. So, it will default to __declspec(dllexport). On the other end, when you USE the DLL's header file, you don't define this, so DLLEXPORTwill be evaluated by default to __declspec(dllimport).

您(在理想情况下实际上是 Visual Studio)COMPILING_DLL在构建 DLL 时将 定义为编译器的参数。因此,它将默认为__declspec(dllexport). 另一方面,当您使用 DLL 的头文件时,您没有定义它,因此DLLEXPORT默认情况下将评估为__declspec(dllimport).

回答by Massood Khaari

You can't define function body that way in the header file. It is prohibited by __declspec(dllimport). This specifier can only be specified on function declaration, not definition.

您不能在头文件中以这种方式定义函数体。__declspec(dllimport) 禁止它。此说明符只能在函数声明中指定,而不能在定义中指定。

You have to move the function body to a source file.

您必须将函数体移动到源文件中。

in header file:

在头文件中:

extern DLLEXPORT void test2();

In .cpp file:

在 .cpp 文件中:

void test2()
{
   // ...
}

As folks said, don't forget to add COMPILING_DLL to the project preprocessor definitions.

正如人们所说,不要忘记将 COMPILING_DLL 添加到项目预处理器定义中。

回答by Manuel Garnier

Actually, the real problem is the preprocessor directive. You should use #ifdefand not #ifto test if the variable is really defined (and we don't care about the defined value or if there is any).

实际上,真正的问题是预处理器指令。你应该使用#ifdefand 不要#if测试变量是否真的被定义(我们不关心定义的值或者是否有任何值)。

NOTE: I know this thread is 1-year old but it still may be useful for somebody who have this problem in the future.

注意:我知道该线程已有 1 年历史,但对于将来遇到此问题的人仍然可能有用。

回答by Gabriel Devillers

If you use CMake to generate your build configuration, you should be able to use the macro <projectname>_EXPORTSthe way you want to use COMPILING_DLL, where projectnamewas defined with the CMake command project(projectname):

如果您使用 CMake 生成您的构建配置,您应该能够以<projectname>_EXPORTS您想要使用的方式使用宏COMPILING_DLL,其中使用projectnameCMake 命令定义project(projectname)

A preprocessor macro, <target_name>_EXPORTSis defined when a shared library compilation is detected.

预处理器宏<target_name>_EXPORTS在检测到共享库编译时定义。

source

来源

I tested and it works on Windows using the Ninja generator with compiler MSVC from Visual Studio 2015 Express.

我使用 Ninja 生成器和 Visual Studio 2015 Express 的编译器 MSVC 进行了测试,它可以在 Windows 上运行。

Related: CMake adds -Dlibname_EXPORTS compile definition

相关:CMake 添加 -Dlibname_EXPORTS 编译定义