【问题标题】:GCC finding errors in MinGW's stdio.hGCC 在 MinGW 的 stdio.h 中发现错误
【发布时间】:2019-03-30 08:07:57
【问题描述】:

我正在尝试在 Windows 7 上使用 GCC/MinGW 编译一些 example C code。示例代码包括一些最终包含 stdio.h 的本地头文件,当我尝试编译时出现此错误:

c:\mingw\include\stdio.h:345:12: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__mingw__snprintf'
extern int __mingw_stdio_redirect__(snprintf)(char*, size_t, const char*, ...);

这对我来说很奇怪。 stdio.h怎么可能有错误?

【问题讨论】:

  • 在“stdio.h”之前包含的文件中可能有一些东西会混淆编译器。如果您首先尝试只编译一个包含“stdio.h”的文件,是否还会出现错误?
  • 请发布头文件pcap.h的内容请不要发布代码链接,而是将代码复制/粘贴到您的问题中
  • main() 只有两个有效签名,它们是:int main( void )int main( int argc, char *argv[] )
  • 请发minimal reproducible example,以便我们重现问题并帮助您调试
  • @user3629249:文件pcap.h(以及,就此而言,示例代码)是winpcap代码包的一部分,而不是OP的工作。因此,您对主要原型的评论并没有得到应有的指导。

标签: c gcc mingw winpcap


【解决方案1】:

关于:

if (i == 0)
{
    printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
    return 0;
}  
pcap_freealldevs(alldevs);

由于变量 i 被初始化为 0 并且从未修改过,因此该 if() 语句将始终为 true。结果之一是调用:pcap_freealldev() 永远不会被调用。

变量的scope 应尽可能合理地加以限制。

代码不应依赖操作系统自行清理。建议

#include <stdio.h>
#include <stdlib.h>
#include "pcap.h"

int main( void )
{
    pcap_if_t *alldevs = NULL;
    char errbuf[PCAP_ERRBUF_SIZE];

    /* Retrieve the device list from the local machine */
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
        exit(1);
    }

    /* Print the list */
    for( pcap_if_t *d = alldevs; d != NULL; d= d->next)
    {
        printf("%d. %s", ++i, d->name);
        if (d->description)
            printf(" (%s)\n", d->description);
        else
            printf(" (No description available)\n");
    }

    if ( ! alldevs )
    {
        printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
    }

    /* We don't need any more the device list. Free it */
    pcap_freealldevs(alldevs);
}

【讨论】:

  • i 在第 20 行 ( printf("%d. %s", ++i, d-&gt;name); ) 被修改,为找到的每个设备执行。据推测,winpcap 的人(not OP)认为如果没有找到设备,释放所有开发人员是没有意义的。无论如何,这个答案与 OP 的问题无关。 (我同意你的测试比他们的测试好,fwiw。但最终效果是一样的。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
  • 1970-01-01
  • 2022-01-13
  • 2019-10-09
相关资源
最近更新 更多