【问题标题】:Compile c code using gcc without installing mingW/cygwin使用 gcc 编译 c 代码而不安装 mingW/cygwin
【发布时间】:2017-08-23 07:06:23
【问题描述】:

我的系统中安装了 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 &lt;stdio.h&gt; 移动到 #include &lt;MyModule.h&gt; 上方的任何其他行,则不会出现指定的错误。这种奇怪行为背后有什么原因吗?

【问题讨论】:

  • 不想安装的原因是什么?如果它只是缺少管理权限,您可以“安装”例如msys2 在您具有写入权限的任何目录中,它带有一个外壳,所有内容都经过精心设置,可以直接编译(并使用 pacman 安装“包”)。
  • @FelixPalmen 考虑我的代码会喜欢,它会从用户那里获得不同编译器的路径(win64gcc,win32gcc => 可以是 minGW 或 Cygwin),它会使用指定的编译给定的 c 代码编译器。
  • 这是一件非常不寻常的事情。相反,提供使用您支持的任何工具链进行构建的方法(例如,msvc 的批处理文件或 msbuild 脚本、gcc/clang 的 Makefile 等)。
  • 嗯.. 谢谢@FelixPalmen
  • 问题应该出在 "MyModule.h" 上。调查该文件

标签: c batch-file gcc compiler-errors


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 2014-10-17
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多