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

876 views Asked by At

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(
        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.

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?

1

There are 1 answers

1
AH3 On

Wow. I feel really bad now...

I never closed the build.c file!!! Therefore gcc could not compile it.

Thanks for the help what might have been helpful sans my stupidty!