需要在我的 msvc cmake 项目中添加多个 inlcude devpkey.h

Need to add multiple inlcude devpkey.h in my cmake project in msvc

包含我的 BundleController class 的头文件被多次包含 devpkey.h 头文件。所以 MSVC 给出如下错误;

**Severity  Code    Description Project File    Line    Suppression State
Error   C2374   'DEVPKEY_Device_Address': redefinition; multiple initialization (compiling source file BundleController.cpp) C:\Program Files (x86)\Windows Kits\Include.0.22000.0\shared\devpkey.h   66  
Error   C2374   'DEVPKEY_Device_Address': redefinition; multiple initialization (compiling source file main.cpp)    C:\Program Files (x86)\Windows Kits\Include.0.22000.0\shared\devpkey.h    66  
Error   C2374   'DEVPKEY_Device_AssignedToGuest': redefinition; multiple initialization (compiling source file BundleController.cpp)    C:\Program Files (x86)\Windows Kits\Include.0.22000.0\shared\devpkey.h    167 
Error   C2374   'DEVPKEY_Device_AssignedToGuest': redefinition; multiple initialization (compiling source file main.cpp)    C:\Program Files (x86)\Windows Kits\Include.0.22000.0\shared\devpkey.h    167 
Error   C2374   'DEVPKEY_Device_BaseContainerId': redefinition; multiple initialization (compiling source file Bundlecontroller.cpp)    C:\Program Files (x86)\Windows Kits\Include.0.22000.0\shared\devpkey.h    74  
Error   C2374   'DEVPKEY_Device_BaseContainerId': redefinition; multiple initialization (compiling source file main.cpp)    C:\Program Files (x86)\Windows Kits\Include.0.22000.0\shared\devpkey.h    74  
Error   C2374   'DEVPKEY_Device_BiosDeviceName': redefinition; multiple initialization (compiling source file BundleController.cpp)     C:\Program Files (x86)\Windows Kits\Include.0.22000.0\shared\devpkey.h    153 
Error   C2374   'DEVPKEY_Device_BiosDeviceName': redefinition; multiple initialization (compiling source file main.cpp) C:\Program Files (x86)\Windows Kits\Include.0.22000.0\shared\devpkey.h    153 
.
.
.**

我需要多次将 devpkey.h 头文件包含到我的项目中,我该如何添加该文件?

项目编译工具是cmake。

在这种情况下,总是需要查看代码并尝试了解错误来源。 DEVPKEY_Device_Address(以及其他有问题的符号)用 DEFINE_DEVPROPKEY macro. based on - are INITGUID is defined - DEFINE_DEVPROPKEY expanded to declaration or definition. if not defined INITGUID, before include devpkey.h ( usually this is done not direct but by #include <initguid.h>, because here exist additional important macros ), we will be have only declarations of DEVPKEY_Device_Address and other symbols, without actual definitions. as result we got error - unresolved symbol on link time. if include initguid.h before every include devpkey.h - we will be have multiple definitions of symbols. usual this must not lead to error, because was DECLSPEC_SELECTANY in symbol definition, which can expand to __declspec( selectany )

定义

Tells the compiler that the declared global data item (variable or object) is a pick-any COMDAT (a packaged function).At link time, if multiple definitions of a COMDAT are seen, the linker picks one and discards the rest.

但看起来你的编译器没有 support/understand __declspec( selectany ),如果你得到错误 C2374

在这种情况下 - 需要在任何文件中都有声明,它只在任何一个文件中使用符号和定义。为此,在某些选定的源文件中执行

#include <initguid.h>
// ...
#include <devpkey.h>

并且在另一个文件中 - 仅

#include <devpkey.h>

之前没有#include <initguid.h>