【问题标题】:Error when calling gcc with system() or popen() in Windows在 Windows 中使用 system() 或 popen() 调用 gcc 时出错
【发布时间】:2015-06-05 08:02:47
【问题描述】:

我正在尝试编写一个调用 gcc 来编译和链接在我的程序中构建的 C 文件的程序。但是,如果我尝试使用以下方法调用 gcc:

system("gcc -g -Wall build.c -o build.exe");

或者更好(因为我想从 gcc 管道输出):

popen("gcc -g -Wall build.c -o build.exe", "r");

我认为是链接错误:

/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/../../../../lib/libcygwin.a(libcmain.o): In function `main':
/usr/src/debug/cygwin-2.0.2-1/winsup/cygwin/lib/libcmain.c:39: undefined reference to `WinMain'
/usr/src/debug/cygwin-2.0.2-1/winsup/cygwin/lib/libcmain.c:39:(.text.startup+0x7f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `WinMain'
collect2: error: ld returned 1 exit status

我通过使用 Windows 特定的ShellExecute 来缓解这个问题:

HINSTANCE hRet = ShellExecute(
        0,
        NULL,
        "cmd.exe",
        "/c gcc -g -Wall build.c -o build.exe",
        NULL,
        SW_HIDE);

这一切都很好,但是,使用ShellExecute 不允许我通过管道输出以查看文件是否正确编译,我希望有一个跨平台非 Windows 特定的方法来执行此操作,因此使用 Windows 函数CreateProcess 是不可取的,我已经读到 popen 应该让我这样做。

我在我的 PATH 变量中定义了 gcc 的路径,它可以从 cmd 正确运行。

我是否遗漏了一个基本概念,还是有更好的方法来做到这一点?

【问题讨论】:

  • 检查gcc --version,使用popenCreateProcess("cmd.exe", "/c gcc --version > result.txt")。您可能有多个副本。
  • 您的入口点是main 还是WinMain?也许这有帮助:http://stackoverflow.com/a/5260237/1587449
  • gcc --versionsystempopenShellExecute 返回 4.9.2。入口点是 main,我正在构建一个 CUI,因此将应用程序构建为 GUI 并没有真正做任何事情(添加 -Wl,-subsystem,windows 没有效果)
  • 没有骰子。 c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libmingw32.a(main.o) (.text.startup+0xa7): undefined reference to WinMain@16' collect2.exe: error: ld returned 1 exit status Could not run more or other error.
  • 0xa7 o.o 嗯嗯。没见过。

标签: c windows gcc popen system-calls


【解决方案1】:

哇。我现在感觉很糟糕......

我从未关闭过 build.c 文件!!!因此 gcc 无法编译。

感谢您的帮助,我的愚蠢可能会有所帮助!

【讨论】:

  • 我怀疑它已经不在使用中了。另一方面,我不想冒险允许愚蠢。可能是致命的……让生活变得比现在更复杂。正如您自己看到的那样,它甚至并不复杂。
猜你喜欢
  • 2014-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
  • 2015-06-12
  • 2011-03-13
  • 2010-09-27
  • 1970-01-01
相关资源
最近更新 更多