【问题标题】:C++ third party library typedef conflicts with standard stdin.hC++ 第三方库 typedef 与标准 stdin.h 冲突
【发布时间】:2018-07-04 21:37:22
【问题描述】:

问题

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

对于文件,我使用了来自 JACK 包的 jack/jack.hjack/types.h。它们包括对 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%。

我从 3rd 方包中使用的库适用于 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

【问题讨论】:

  • 您应该向库开发人员提交错误报告。
  • 你唯一能做的就是在你的代码中为这个库实现一个门面,它只包含它们的头文件,你的头文件和你的门面的接口,然后编写你的主要 C++ 代码来使用门面.
  • 您可以尝试将_STDINT_H 定义为一种解决方法。或者也许它应该通过某种配置头来定义。 systemdeps.h 文件一团糟……
  • 只是猜测,但如果您在任何这些 Jack 标头之前添加#include &lt;stdint.h&gt;,问题可能会得到解决。这样您就可以从&lt;stdint.h&gt; 以及包含保护中获得这些类型的定义(假设包含保护的名称是_STDINT_H 是正确的)。
  • 克里斯和@PeteBecker:值得将皮特的评论推广到答案。它对使用 Jack 的人非常有用。

标签: c++


【解决方案1】:

”systemdeps.h”在提供这些类型定义方面做得相当糟糕。根据您的评论,它正在检查错误的包含防护。即使它是正确的,如果您在该标题之后包含&lt;stdint.h&gt;,您也会遇到问题。所以你必须做两件事。首先,无论您在哪里使用任何jack 标头,在您包含jack 标头之前添加#include &lt;stdint.h&gt; 。其次,在#include &lt;stdint.h&gt; 之后立即添加#define _STDINT_H。这样你就可以从编译器的头文件中获取 typedef,然后告诉 ”systemdeps.h” 不要提供它自己的定义。这既乏味又容易出错,因此您可以考虑创建自己的标头,将所有这些都集中在一个地方。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    相关资源
    最近更新 更多