使用gcc编译c代码而不安装mingW / cygwin

时间:2017-08-23 07:06:23

标签: c batch-file gcc compiler-errors

我的系统中安装了visual studio。因此设置了相应的编译器和环境变量。当我尝试使用 cl 命令编译c文件时,它可以正常工作。现在我从另一个系统压缩mingW并将其解压缩到我的系统。说我在 D:/ mingW 中有它。现在我已经创建了一个用于编译c文件的批处理文件。批处理文件的内容是

set gccPath=D:/mingW/bin
%gccPath%/gcc.exe -c -std=c99 -o myC.o ../myC.c -I..\.

当我运行这个批处理文件时,它产生的错误很少。 一个这样的错误是

stdio.h:209:71: error: macro "snprintf" requires 5 arguments, but only 4 given

上述错误可能是由于编译器使用visual studio的stdio.h而不是mingW的默认头文件。 另一个错误是,

error: previous declaration of 'xxxxFun' was here

我应该在批处理脚本中更改什么来使用mingW完全编译c文件。 我们使用Visual Studio时编译过程成功,但如果我们对同一组文件使用gcc则抛出错误

修改 我修复了后一个错误。 当首先包含stdio.h时,也不会发生第一个错误。但是如果我们在include部分的中间包含stdio.h,就会出现错误。

#include <string.h>
#include <assert.h>
#include <minmax.h>

#include "myFunctions.h"
#include "MyModule.h"
#include <stdio.h>

如图所示,当我们最后有 stdio.h 时,错误即将来临。如果我们将行#include <stdio.h>移动到#include <MyModule.h>以上的任何其他行,则指定的错误不会到来。这种奇怪行为背后的原因是什么?

1 个答案:

答案 0 :(得分:0)

检查您是否在任何头文件中使用宏定义 snprintf 。此错误可能是由于错误/不必要的宏引起的。