0

I'm writing a simple transpiler in C++ that takes Brainfuck code and converts it into C++. It's then supposed to compile it with G++ with the following code:

std::string compileCommand{ "g++ file.cpp -o file" };
system(compileCommand.c_str());

But when I run the program, G++ produces this error:

c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status

However, when I run...

g++ file.cpp -o file

...from outside the program, it compiles just fine. Why is this happening and how can I fix it?

file.cpp:

#include <iostream>
#include <stdio.h>

int main(int argc, char *argv[])
{
char *ptr = new char[30000]{ 0 };
++*ptr;
++*ptr;
putchar(*ptr)
}
Dan
  • 88
  • 7
  • 1
    Please show a [mcve], including your `file.cpp` file. – user202729 Jul 08 '18 at 01:23
  • It's very likely that `compileCommand` is not what you hope it ought to be. Add a diagnostic message to verify its value. `std::cout << "compileCommand: " << compileCommand << std::endl;` One thing that seems to be an error is that you are using `" -o"`. I think that should be`'" -o "`, i.e. you need an additional space after the `o` in the command. – R Sahu Jul 08 '18 at 01:35
  • @RSahu I've tried everything you suggested, I added the space to ` -o ` and compileCommand is correct - however G++ still returns the same error I described above. – Dan Jul 08 '18 at 01:47
  • @Dan, that is strange indeed. I am afraid I don't have any other ideas to help you. Good luck. – R Sahu Jul 08 '18 at 01:54
  • It is trying to compile it as a windows application. Using system may change the interpreter and the path of the binaries called. Try the same command in a cmd window, I expect it to fail in the same way. – Michael Doubez Jul 08 '18 at 06:07
  • @MichaelDoubez As you can see above, I've already tried the same command in a cmd window. It compiled just fine. I'm only facing issues when executing it with system() – Dan Jul 08 '18 at 07:01
  • 2
    The error is linking with mingw libraries. Your environment is not consistent. – Michael Doubez Jul 08 '18 at 09:55

1 Answers1

0

I am unable to exactly replicate your scenario, but I think there could be some issue with the library paths as Michael Doubez has mentioned in the comments. Since this case looks environment specific, I think there will be no closed form/general solution as such. If there is an issue with the linker as anticipated, the following sequence should work fine -

  1. Since the program compiling fine for you on command line, you should try running

     g++ file.cpp -Wl,--verbose -o file  
    

    to see what flags being set to the linker by default. Specifically, the SEARCH_DIR fields (there will be multiple fields of it) of the output of the command will have a list of the directories where the linker will search for the libraries Reference.

  2. Note down those directory paths from above step and pass those paths to your compileCommand variable with the "-L" flag example -

    std::string compileCommand{ "g++ file.cpp -o file -L <path1> -L <path2> -L <path3>" };
    

    where <path1>, <path2>, <path3> ... are replaced with the paths you have found in step1. Here, we are specifically passing the folder paths where the linker should look for the libraries.

  3. If the above step does not solve the issue, we have to specify the library names manually to the compileCommand. To know the library names, run the following command -

    g++ file.cpp -o file  -Wl,--trace
    

    The names of the libraries will start with "-l" example - -lstdc++, -lgcc_s etc. Note down these names and pass them as flags in addition to the flags you are passing in step 2. Your compileCommand variable should look like this -

    std::string compileCommand{ "g++ file.cpp -o file -L <path1> -L <path2> -L <path3> -lstdc++ -lgcc_s" }; 
    

    This sequence should typically solve your issue if our assumption that this is a linker issue is correct.