开发者

Alternatives for empty macros

In my C++ header file, I have the following:

#ifdef _DLL
#define DLL_API __declspec(dllexpor开发者_运维知识库t) // 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜