C++ 第三方库 typedef 与标准 stdin.h 冲突

标签 c++

问题

我正在尝试编写一个程序来监听使用JACK audio server创建的音频流。 。该包在单独使用时工作正常,但是当我尝试在文件中与对标准 C++ 库(例如 iostream)的引用一起使用它时,在第 3 方库和 stdint.h 之间会引发 typedef 冲突错误。

对于该文件,我使用的是 jack/jack.hjack/types.h,它们都来自 JACK 包。它们包括对 systemdeps.h 文件(在下面展开)的引用,这是错误的根源。

我尝试在网上寻找解决方案,但似乎没有任何效果。任何有关可能导致此问题的原因的见解,或者解决问题的正确方向的观点将不胜感激。

错误消息

2>\Jack\includes\jack/systemdeps.h(69): error C2371: 'int8_t': redefinition; different basic types (compiling source file src\jack\jack_interface.cpp)

2>stdint.h(17): note: see declaration of 'int8_t' (compiling source file src\jack\jack_interface.cpp)

2>\Jack\includes\jack/systemdeps.h(73): error C2371: 'int32_t': redefinition; different basic types (compiling source file src\jack\jack_interface.cpp)

2>stdint.h(19): note: see declaration of 'int32_t' (compiling source file src\jack\jack_interface.cpp)

2>\Jack\includes\jack/systemdeps.h(74): error C2371: 'uint32_t': redefinition; different basic types (compiling source file src\jack\jack_interface.cpp)

2>stdint.h(23): note: see declaration of 'uint32_t' (compiling source file src\jack\jack_interface.cpp)

我的实现

// jack_interface.cpp
#include <iostream>
#include "jack/jack.h"                // Core library .h
#include "jack/types.h"               // library types .h
#include "jack/jack_interface.h"      // My .h extending the lib

namespace jack 
{
}

这是我可以编写导致抛出错误的最低限度。

jack_interface.h 为空。

jack.h 和 types.h 都包含对产生冲突的 systemdeps.h 的引用。

只有当库和任何引用 stdint.h 的标准 C++ 文件都包含时,才会抛出错误,例如iostream。如果我删除iostream,库函数100%。

我使用的第 3 方包中的库适用于 32 位 Windows。我正在使用 Visual Studio 2017 进行 32 位编译。

systemdeps.h

引发冲突的库头文件。下面标记了引发错误的行。 See file on github here.

#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(GNU_WIN32)

#include <windows.h>

#ifdef _MSC_VER     /* Microsoft compiler */
    #define __inline__ inline
    #if (!defined(int8_t) && !defined(_STDINT_H))
        #define __int8_t_defined
        typedef char int8_t;                 <-- ERROR
        typedef unsigned char uint8_t;
        typedef short int16_t;
        typedef unsigned short uint16_t;
        typedef long int32_t;                <-- ERROR
        typedef unsigned long uint32_t;      <-- ERROR
        typedef LONGLONG int64_t;
        typedef ULONGLONG uint64_t;
    #endif

最佳答案

那个”systemdeps.h”在提供这些 typedef 方面做得相当糟糕。根据您的评论,它正在检查错误的包含防护。即使它是正确的,如果包含 <stdint.h>,您也会遇到问题在该标题之后。所以你必须做两件事。首先,无论您在何处使用这些 jack标题,添加 #include <stdint.h> 之前添加 jack header 。第二个,紧接在 #include <stdint.h> 之后添加#define _STDINT_H 。这样你就可以从编译器的头中获取 typedef,然后告诉 ”systemdeps.h”不提供自己的定义。这既乏味又容易出错,因此您可能会考虑创建自己的 header ,在一个地方完成所有这些操作。

关于C++ 第三方库 typedef 与标准 stdin.h 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51181008/

相关文章:

c++ - 如何制作 C 宏预构建预处理器?

c++ - 我的控制台正在打印随机单词并且没有执行正确的数学运算

c++ - 使用 boost::test 进行内存泄漏检测

c++ - 从接口(interface)转换为实现接口(interface)的类的子类

c++ - cmake 和 boost-signals 的问题

c++ - 使用迭代器从 C++ 中的 vector 末尾删除元素

c++ - 从 OpenCV 文件中读取算法参数

使用 getter 和 setter 存储全局 std::string 标签的 C++ 策略

c++ - 错误 c2065 : 'filename' : undeclared identifier

c++ - 关于C++中的 float 比较