【发布时间】: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,使用popen和CreateProcess("cmd.exe", "/c gcc --version > result.txt")。您可能有多个副本。 -
您的入口点是
main还是WinMain?也许这有帮助:http://stackoverflow.com/a/5260237/1587449 -
gcc --version为system、popen和ShellExecute返回 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. -
0xa7o.o 嗯嗯。没见过。
标签: c windows gcc popen system-calls