BOOL redefinition after including minwindef.h

狂风中的少年 提交于 2021-02-10 15:45:01

问题


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:

  1. The code did not used to interface with Windows code at all;
  2. By creating a Windows C++ application you have added Windows dependencies;
  3. 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 its BOOL macro, when defined, and its BOOL 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!