Included that header files of my BundleController class are included multiple times devpkey.h header file. So MSVC give errors like below;
**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\10\Include\10.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\10\Include\10.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\10\Include\10.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\10\Include\10.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\10\Include\10.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\10\Include\10.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\10\Include\10.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\10\Include\10.0.22000.0\shared\devpkey.h 153
.
.
.**
I need to include multiple times devpkey.h header file to my project, how can I add that file?
Project compilation tool is cmake.
CodePudding user response:
in such situation always need look code and try understand source of error. DEVPKEY_Device_Address
(and another symbols in question) defined with 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.
but look like your comiler dont support/understand __declspec( selectany ), if you got error C2374
in this case - need have declaration in any file, which use symbol and definition only in any one. for this in some selected source file do
#include <initguid.h>
// ...
#include <devpkey.h>
and in another files - only
#include <devpkey.h>
without #include <initguid.h>
before