空宏的替代方案
在我的 C++ 头文件中,我有以下内容:
#ifdef _DLL
#define DLL_API __declspec(dllexport) // Being compiled as a DLL.
#else
#define DLL_API // Not being compiled as a DLL.
#endif
稍后,我有类似的内容:
DLL_API int GetNumber();
我过于简单化了,但这里的基本问题是是否有办法让编译器跳过 DLL_API
如果没有定义。
In my C++ header file, I have the following:
#ifdef _DLL
#define DLL_API __declspec(dllexport) // Being compiled as a DLL.
#else
#define DLL_API // Not being compiled as a DLL.
#endif
Later on, I have things like:
DLL_API int GetNumber();
I'm oversimplifying, but the basic question here is whether there's a way to get the compiler to just skip over DLL_API
if it's not defined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不会。
当 DLL_API 被定义为不包含任何内容的预处理器宏时,预处理器将不包含任何内容替换 DLL_API,并且编译器将在那里看不到任何内容。如果预处理器未定义它,则预处理器不会对其执行任何操作。然后编译器会看到它没有改变,并且你会得到关于未知标识符 DLL_API 的编译器错误,因为这样的东西不是 C++ 语言的一部分。
像 __declspec() 这样的属性是特定于平台的扩展,通常的约定是将它们在接口中的用法包装到预处理器宏中。
No.
When DLL_API is defined as preprocessor macro that contains nothing then preprocessor replaces DLL_API with nothing and compiler will see nothing there. If it is undefined for preprocessor then preprocessor does nothing with it. Then compiler will see it unchanged and you get compiler error about unknown identifier DLL_API, because such thing is not part of C++ language.
Attributes like __declspec() are platform specific extensions and it is common convention to wrap their usage in interfaces into preprocessor macros.
通常,它
是可移植的,并且 DLL_API 总是被转换为有效的东西。
Usually, it is
so that it is portable, and DLL_API is always transformed into something valid.