2

While writing some small code for open-source, I have encountered a behavior difference when compiling using G++ and MSBuild (VS compiler). I am wondering whether this is something that is injected into the executable by the compiler or is this a Windows property set to the executable. In either case, I'd like to turn it off... (is there a flag in g++?).

The problem: When I pass an asterisk("*") as a parameter to the executable that was compiled in Visual Studio, argv contains an asterisk (argc==2, argv[1]=="*"), while doing the same thing using a code that is compiled with G++, the asterisk is converted into a file list (argc==7, argv[1]=="first file in folder", argv[1]=="second file in folder", ...).

I am working on Windows 10, compiling with VS 2019 and G++ 10.2.0 (MinGW).

You can recreate the scenario just by printing the argv content:

int main(int argc, char* argv[])
{
    for (int i = 0; i < argc; i++)
    {
        std::cout << argv[i] << std::endl;
    }
}

The call to the executable is from command-line (cmd):

a.exe *

Thanks Lior

Lior
  • 284
  • 1
  • 6
  • If this is shell-dependent, why would two executables compiled with different compilers behave differently in the same shell? BTW: I've also double-checked with "echo *" and a batch file. G++ output behaves differently from them as well – Lior May 03 '21 at 06:59
  • How is G++ involved with the `echo` command and its behavior? When you say "G++" with `echo` do you mean MinGW and MSYS and their shell? – Some programmer dude May 03 '21 at 07:11
  • I am trying to depict the shell behavior... If the shell was converting the asterisk to file list, that would have also happen in "echo *". (and in the VS compiled executable). – Lior May 03 '21 at 07:15
  • So you called both executables directly from cmd? Or from some other shell? – Lukas-T May 03 '21 at 07:18
  • both were called from cmd, from the same window, one after the other – Lior May 03 '21 at 07:18
  • 2
    That's strange indeed and I'm out of ideas. But a little bit of googling turned up [this](https://stackoverflow.com/questions/33860141/windows-mingw-asterisk-passing-by-argv1-to-string) and [this](https://stackoverflow.com/questions/3995493/gnuwin32-find-exe-expands-wildcard-before-performing-search) which basically say, that the mingw-runtime screws you over. Passing as explicit string `"*"` should hopefully fix it. – Lukas-T May 03 '21 at 07:53

1 Answers1

2

Problem solved.

Following on churill's advice from above (Referencing this). There is a way to disable minGW's argument expansion if you either link with CRT_noglob.o or set the global variable "int _CRT_glob" to zero (int _CRT_glob = 0;).

I've done the latter and it works.

Thanks!

Lior
  • 284
  • 1
  • 6