问题描述
我正在查找库的源代码,并且许多类使用以下形式定义:
I'm looking through the source of a library and many classes are defined using the following form
class THING_API ClassName
{
...
跳转到宏定义。 。 。
Jumping to the macro definition . . .
#ifndef THING_API
#define THING_API /**< This macro is added to all public class declarations. */
#endif
这是什么可以做到的,
What could this be for, and is it a common technique?
推荐答案
它对我来说非常喜欢导出宏,这是在Windows上构建共享库当使用MSVC编译时,在构建库时,必须在该位置插入 __ declspec(export)
, __ declspec(import)
当构建其客户端时。这是这样实现的:
It looks to me very much like export macro, which is required when building a shared library (.dll) on Windows. When compiling with MSVC, You have to put __declspec(export)
in that spot when building a library, and __declspec(import)
when building its client. This is achieved like so:
#if COMPILING_DLL
#define THING_API __declspec(dllexport)
#else
#define THING_API __declspec(dllimport)
#endif
code> COMPILING_DLL 用于库项目,对所有其他项目保留未定义。如果你不是在Windows或编译一个静态库,你需要定义它的空白,就像在你的问题。
Then you define COMPILING_DLL
for the library project, and leave it undefined for all other projects. And if you're not on Windows or compiling a static library, you need to define it blank like it's done in your question.
P。其他Windows编译器使用自己的关键字,而不是 __ declspec(dllimport)
,但原则仍然存在。
P. S. Other Windows compilers use their own keywords instead of __declspec(dllimport)
, but the principle remains.
这篇关于在类定义的开始,这个宏是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!