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. However, if I try to call gcc by using:

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

or better yet (because I would like to pipe output from 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:

HINSTANCE hRet = ShellExecute(
        "/c gcc -g -Wall build.c -o build.exe",

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.

I have the path to gcc defined within my PATH variable and it runs correctly from cmd.

Am I missing a fundamental concept, or is there a better way to do this?

Source: gcc

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.