简体   繁体   English

在Windows中使用system()或popen()调用gcc时出错

[英]Error when calling gcc with system() or popen() in Windows

I am trying to write a program that calls gcc to compile and link a C file that is built within my program. 我正在尝试编写一个调用gcc的程序,以编译和链接在我的程序中内置的C文件。 However, if I try to call gcc by using: 但是,如果我尝试使用以下方法调用gcc:

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

or better yet (because I would like to pipe output from gcc): 或更好(因为我想通过管道输送gcc的输出):

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

I get what I assume is a link error: 我得到的是链接错误:

/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

I mitigated this problem by using the Windows specific ShellExecute as such: 我通过使用Windows特定的ShellExecute来缓解此问题:

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

This run everything fine, however, using ShellExecute does not allow me to pipe the output to see if the file compiled correctly and I would like to have a crossplatform non-Windows specific way to do this so using the Windows function CreateProcess is undesirable and I have read that popen should let me do this. 这样运行起来一切都很好,但是,使用ShellExecute不允许我通过管道传输输出以查看文件是否正确编译,并且我希望采用跨平台的非Windows特定方法来执行此操作,因此不希望使用Windows函数CreateProcess阅读过popen应该让我这样做。

I have the path to gcc defined within my PATH variable and it runs correctly from cmd. 我在PATH变量中定义了gcc的路径,并且可以从cmd正确运行。

Am I missing a fundamental concept, or is there a better way to do this? 我是否缺少基本概念,还是有更好的方法来做到这一点?

Wow. 哇。 I feel really bad now... 我现在真的好难受...

I never closed the build.c file!!! 我从未关闭过build.c文件!!! Therefore gcc could not compile it. 因此,gcc无法编译它。

Thanks for the help what might have been helpful sans my stupidty! 感谢您的帮助,如果没有我的愚蠢,可能会有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM