包含 minwindef.h 后的 BOOL 重新定义 [英] BOOL redefinition after including minwindef.h

查看:50
本文介绍了包含 minwindef.h 后的 BOOL 重新定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C/C++ 的新手,以防万一:)我克隆了一个用 C 编写的旧协议栈解决方案,其中一个 C++ 中的主类将它导入到 VS(Visual C++ 2017 v 15.9.5 针对 Windows SDK 10.0.17134.0)它正确编译并运行.

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.

现在制作了一个 C++ 解决方案(Windows 控制台应用程序)创建了一个文件夹 lib 将所有那些 .h.c 文件复制粘贴到 lib 添加了附加包含目录和链接器附加库目录的路径.

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

但这与 Windows 工具包中的 minwindef.h 相冲突.虽然我 #include types.h 得到 C2371 'BOOL': redefinition;不同的基本类型在整个解决方案中,我想使用 BOOL 的这个定义以及在这个头文件中定义的所有其他定义.

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.

我应该如何解决这个问题?或者一般情况下,如果在 C++ 项目中使用 C 代码,我应该遵循哪些设置和宏(例如方法中的 extern "C")

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

推荐答案

我对您尝试使用的库一无所知,因为您没有告诉我们它是什么.但我可以做一些猜测:

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. 该代码根本不用于与 Windows 代码交互;
  2. 通过创建 Windows C++ 应用程序,您已经添加了 Windows 依赖项;
  3. Windows 依赖项(以使用诸如 BOOL 之类的短名称毒化命名空间而闻名)与库的代码(与它的 BOOL 做同样的事情)发生冲突宏,在定义时,及其 BOOL 类型别名,否则).
  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).

这与 C 与 C++ 或类似的东西并没有真正的关系,并且您无法进行一般性修复.您可以尝试摆脱 Windows 依赖项(您的任务需要该标头吗?),或者您可以修补您的库以不触及 BOOL(在确保 Windows 的 BOOL 正是您所需要的).

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!

这篇关于包含 minwindef.h 后的 BOOL 重新定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆