检查预处理器中的__declspec宏 [英] Check a __declspec macro in preprocessor
问题描述
如果我有 SOME_MACRO
,它被定义为 __ declspec(dllimport)
或 __ declspec (dllexport)
,有没有一种方法可以在编译时检查正在使用哪个?
If I have SOME_MACRO
which is defined as either __declspec(dllimport)
or __declspec(dllexport)
, is there a way to check at compile time which one is being used?
Ie像这样的东西:
#if SOME_MACRO == __declspec(dllimport)
// do something
#else
// do something else
#endif
UPD。
看得到的答案,我想我应该更具体地说明为什么需要这个。
UPD. Looking at the answers I'm getting I guess I should be more specific in why I need this.
我正在尝试编译一个相当大的3rd方库,该库在其代码的大部分包含该函数的地方声明为 dllexport
。但是,其中有一个组件是 dllimport
。
I'm trying to compile a rather big 3rd party library, which has a function declared as dllexport
in most parts of their code where it's included. There's however one component in which it's a dllimport
.
我需要为<$ c稍作修改声明$ c> dllimport 案例。这两个声明之间的切换不是很简单,这是由于 #ifdef
指令的树很深而导致的,这些指令分布在多个文件中。原则上,我可以从这些说明中提取此信息,但是要确保我正确地进行了操作,则必须尝试在几种不同的配置下编译整个库(每次编译需要几个小时)。
I need to modify the declaration slighly for the dllimport
case. The switch between the two declarations is not very simple, it is a result of quite a deep tree of #ifdef
instructions spread across several files. In principle I could dig this info out form these instructions, but to be sure I did it correctly I'd have to try and compile the whole library under several different configurations (each compilation taking a couple hours).
另一方面,如果有一种简单的方法来检查其 SOME_MACRO
是否被评估为可以导入或导出,我可以在一个很小的范围内进行测试
If on the other hand there was a simple way check whether their SOME_MACRO
is evaluated to import or export, I could test this on a small program quickly and safely put that inside the library.
推荐答案
您不能使用
#if SOME_MACRO == __declspec(dllimport)
__ declspec(dllimport)
不是预处理器表达式的有效令牌。
__declspec(dllimport)
is not a valid token for a preprocessor expression.
您最好的选择是使用另一个预处理器宏,例如:
Your best option is to use another preprocessor macro, such as:
// Are we building the DLL?
#if defined(BUILD_DLL)
// Yes, we are.
#define SOME_MACRO __declspec(dllexport)
#else
// No. We are using the DLL
#define SOME_MACRO __declspec(dllimport)
#endif
现在,您可以使用:
#if defined(BUILD_DLL)
包含条件代码取决于您是否构建DLL或使用DLL。
to include conditional code depending on whether you are building the DLL or using the DLL.
实际上,这最终会涉及更多点。
Practically speaking, that ends to be a little bit more involved.
-
大多数项目具有多个DLL。
BUILD_DLL
无法正常工作。每个生成的DLL将需要BUILD_xxx_DLL
。假设您有两个DLL,即实用程序和核心。
Most projects have more than one DLL.
BUILD_DLL
is not going to work. You will needBUILD_xxx_DLL
for each DLL you build. Let's say you have two DLLs, utility and core. and an application that depends on both.
您可能还需要创建一个静态库。
You may also need to create a static library.
在实用程序库的每个公共.h文件中,您都将需要以下内容。
You will need something like the following in every public .h file of the utility library.
#if defined(BUILD_UTILITY_STATIC)
#define UTLIITY_EXPORT
#elif defined(BUILD_UTILITY_DLL)
#define UTLIITY_EXPORT__declspec(dllexport)
#else
#define UTLIITY_EXPORT__declspec(dllimport)
#endif
当然,您不需要在许多.h文件中重复相同的代码。您将创建一个.h文件,其中包含上述文件,并且 #include
包含在所有其他.h文件中。
Of course, you don't want to have to repeat the same code in lots of .h files. You will create a .h file that contains the above and #include
that in all other .h files.
生成Utility.dll时,需要定义 BUILD_UTILITY_DLL
并保留 BUILD_UTILITY_STATIC
未定义。
When building utility.dll, you will need to define BUILD_UTILITY_DLL
and leave BUILD_UTILITY_STATIC
undefined.
构建utllity.lib(静态库)时,需要定义 BUILD_UTILITY_STATIC
并保留 BUILD_UTILITY_DLL
未定义。
When building utllity.lib (static library), you will need to define BUILD_UTILITY_STATIC
and leave BUILD_UTILITY_DLL
undefined.
utility.dll的用户将离开 BUILD_UTILITY_STATIC
以及 BUILD_UTILITY_DLL
未定义。
Users of utility.dll will leave BUILD_UTILITY_STATIC
as well as BUILD_UTILITY_DLL
undefined.
utility.lib(静态库)的用户将需要定义 BUILD_UTILITY_STATIC
并保留 BUILD_UTILITY_DLL
未定义。
Users of utility.lib (static library) will need to define BUILD_UTILITY_STATIC
and leave BUILD_UTILITY_DLL
undefined.
对于core.dll和core.lib,您将需要一个类似的文件。
You will need a similar file for core.dll and core.lib.
这篇关于检查预处理器中的__declspec宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!