0

我正在使用 Code::Blocks 将 Python(包含 Mosquitto MQTT)脚本重写为 C。作为测试,我使用了 Mosquitto 存储库中可用的以下代码:

https://bitbucket.org/oojah/mosquitto/src/4deedcb49ff50be94166701f21e5c79ff7667d5b/test/lib/c/02-subscribe-qos0.c?at=default

但是,这会导致以下警告:

||=== Build: Debug in test (compiler: GNU GCC Compiler) ===|
..\..\..\..\..\Program Files (x86)\mosquitto\devel\mosquitto.h|56|warning: "bool" redefined|
c:\mingw32-xy\bin\..\lib\gcc\mingw32\4.5.2\include\stdbool.h|33|note: this is the location of the previous definition|
obj\Debug\main.o||In function `on_connect':|

我一直在深入研究这个主题,我认为可以使用包含警卫来解决它。我做了一些测试,但显然我不知道如何正确应用它们。

由于我不是经验丰富的 C 程序员,我决定寻求帮助。

编辑:我添加了mosquitto.h 代码的链接。

这是可能出错的部分:

#ifndef _MOSQUITTO_H_
#define _MOSQUITTO_H_

#ifdef __cplusplus
extern "C" {
#endif

#if defined(WIN32) && !defined(WITH_BROKER)
#   ifdef libmosquitto_EXPORTS
#       define libmosq_EXPORT  __declspec(dllexport)
#   else
#       define libmosq_EXPORT  __declspec(dllimport)
#   endif
#else
#   define libmosq_EXPORT
#endif

#ifdef WIN32
#   ifndef __cplusplus
#       define bool char
#       define true 1
#       define false 0
#   endif
#else
#   ifndef __cplusplus
#       include <stdbool.h>
#   endif
#endif

是否有快速修复使其工作?

4

1 回答 1

2

确切地说,包括警卫不是问题。

问题在于它mosquitto.h包含宏的重新定义,该宏bool已经由<stdbool.h>.

这似乎只在 Win32 上触发。这可能是有人假设“如果我们在 Win32 上构建,那么我们没有使用符合 C99 的编译器,因此我们必须替换我们自己的stdbool.h- 兼容声明”的结果。

这当然是一个错误的假设。您可以在 Win32 中使用非 Microsoft 编译器进行构建。这可能就是您正在做的事情;Code::Blocks 可以使用 GCC。

我会说mosquitto.h需要修复的逻辑。

于 2014-07-07T08:35:17.500 回答