问题
I am a newbie in C/C++, just in case :) I have cloned an older protocol stack solution written in C with one main class in C++ imported it into VS (Visual C++ 2017 v 15.9.5 targeting Windows SDK 10.0.17134.0) it compiled correctly and working.
Now made a C++ solution (windows console application) created a folder lib
copy pasted all those .h
and .c
files into lib
added the path to additional include directories
and also in linker additional library directories
.
Building the Solution throwing tones of errors. the one I am trying to fix now is:
One of the header files contains type definitions
typedef uint8_t U8;
#ifndef BOOL
typedef U8 BOOL;
#endif
but this is conflicting with minwindef.h
from windows kit. though I #include types.h
getting C2371 'BOOL': redefinition; different basic types
in the whole solution, I want to use this definition of BOOL
and all other ones defined in this header.
How should I solve the issue? Or in General in case of using C codes in C++ projects what settings and MACRO (e.g. extern "C" in methods) should I follow
回答1:
I don't know anything about the library you're trying to work with, because you did not tell us what it is. But I can make some guesses:
- The code did not used to interface with Windows code at all;
- By creating a Windows C++ application you have added Windows dependencies;
- The Windows dependencies (well-known for poisoning the namespace with short names like
BOOL
) are conflicting with the library's code (which is doing the same thing with itsBOOL
macro, when defined, and itsBOOL
type alias, otherwise).
This isn't really to do with C vs C++ or anything like this, and there's no general fix you can make. You can either try to get rid of the Windows dependency (do you need that header for your task?) or you can patch your library not to touch BOOL
(after making sure that Windows's BOOL
is what you need it to be).
And use this as a good lesson not to pollute namespaces!
来源:https://stackoverflow.com/questions/54112438/bool-redefinition-after-including-minwindef-h