空宏的替代方案

发布于 2024-10-08 17:51:37 字数 328 浏览 0 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城挽邺 2024-10-15 17:51:37

不会。

当 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.

自在安然 2024-10-15 17:51:37

通常,它

#ifdef _WIN32
#ifdef _DLL
#define DLL_API __declspec(dllexport) // Being compiled as a DLL.
#else
#define DLL_API __declspec(dllimport) // Not being compiled as a DLL.
#endif
#else
#define DLL_API
#endif

是可移植的,并且 DLL_API 总是被转换为有效的东西。

Usually, it is

#ifdef _WIN32
#ifdef _DLL
#define DLL_API __declspec(dllexport) // Being compiled as a DLL.
#else
#define DLL_API __declspec(dllimport) // Not being compiled as a DLL.
#endif
#else
#define DLL_API
#endif

so that it is portable, and DLL_API is always transformed into something valid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文