0

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?

AH3
  • 11
  • 2
  • Check `gcc --version`, both with `popen` and via `CreateProcess("cmd.exe", "/c gcc --version > result.txt")`. You may have multiple copies. – MSalters Jun 05 '15 at 08:20
  • Is your entry Point `main` or `WinMain`? And maybe this is helping: [http://stackoverflow.com/a/5260237/1587449](http://stackoverflow.com/a/5260237/1587449) – Da Maex Jun 05 '15 at 08:21
  • `gcc --version` returns 4.9.2 for `system`, `popen`, and `ShellExecute`. The entry point is `main` and I am building a CUI so building the app as a GUI does not really do anything (adding the `-Wl,-subsystem,windows` has no effect) – AH3 Jun 05 '15 at 11:15
  • No dice. `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.` – AH3 Jun 05 '15 at 17:05
  • `0xa7` o.o well umm. Haven't seen this. – Imobilis Jun 05 '15 at 19:01
  • when using gcc from a sub shell, it is necessary to include the '-Lpathtolib -llibshortname' as parameters to the linker. Be sure the linker parameters are last on the line – user3629249 Jun 05 '15 at 21:07

1 Answers1

1

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!

AH3
  • 11
  • 2
  • I suspected it is out of something being in use.. didn't want to risk to allow stupidity on the other hand. Could be fatal.. making the life more complex than it is. As you see by yourself it is not even complex. – Imobilis Jun 05 '15 at 20:24