gcc on Windows: generated "a.exe" file vanishes

10,759

Solution 1

(Since ahoffer's deleted answer isn't quite correct, I'll post this, based on information in the comments.)

On Windows, gcc generates an executable named a.exe by default. (On UNIX-like systems, the default name, for historical reasons, is a.out.) Normally you'd specify a name using the -o option.

Apparently the generated a.exe file generates a false positive match in your antivirus software, so the file is automatically deleted shortly after it's created. I see you've already contacted the developers of Avast about this false positive.

Note that antivirus programs typically check the contents of a file, not its name, so generating the file with a name other than a.exe won't help. Making some changes to the program might change the contents of the executable enough to avoid the problem, though.

You might try compiling a simple "hello, world" program to see if the same thing happens.

Thanks to Chrono Kitsune for linking to this relevant Mingw-users discussion in a comment.

This is not relevant to your problem, but you should print a newline ('\n') at the end of your program's output. It probably doesn't matter much in your Windows environment, but in general a program's standard output should (almost) always have a newline character at the end of its last line.

Solution 2

Try to compile with gcc but without all standard libraries using a command like this:

gcc  -nostdlib -c  test.c -o test.o; gcc test.o -lgcc -o test.exe

One of the mingw libraries binary must generate a false positive, knowing which library would be useful.

Solution 3

There is no issue with your code it is just exiting properly.

You have to run it in the command line which will show you all the info.

start->run->cmd, then cd to your directory. then a.exe. If you don't want to do that you can add a sleep() before the return in main.

More over, in your code when you pass print(5) to your function it's not being used.

Share:
10,759
chapman
Author by

chapman

Updated on June 15, 2022

Comments

  • chapman
    chapman almost 2 years

    I'm using GCC version 4.7.1, but I've also tried this on GCC 4.8. Here is the code I'm trying to compile:

    #include <stdio.h>
    
    void print(int amount) {
        int i;
        for (i = 0; i < 5; i++) {
            printf("%d", i);
        }
    }
    
    int main(int argc, char** argv) {
        print(5);
        return 0;
    }
    

    It looks like it should work, and when I compile with...

    gcc main.c
    

    It takes a while to compile, produces an a.exe file and the the a.exe file disappears. It isn't giving me any errors with my code.

    Here's a gif of proof, as some people are misinterpreting this: proof

    • Raydel Miranda
      Raydel Miranda almost 10 years
      Have you some antivirus installed? Try disabling it and let us know.
    • chapman
      chapman almost 10 years
      Moved it to my desktop and tried compiling it there, same result again
    • chapman
      chapman almost 10 years
      The third window is Sublime Text 3
    • n. m.
      n. m. almost 10 years
      OK, a terminological train wreck. gcc probably has nothing to do with this. Look at your system logs and antivirus logs.
    • chapman
      chapman almost 10 years
      @RaydelMiranda Spot on, strange since it will usually warn me if it quarantines or flags a file. I'm using Avast Antivirus, if that helps.
    • chapman
      chapman almost 10 years
      Now I just need to figure out how to stop avast from blacklisting my code :(
    • Keith Thompson
      Keith Thompson almost 10 years
      Actually ahoffer's deleted answer isn't quite correct; it suggests that the file name a.exe is the problem. In fact virus scanners check the contents of files, not their names. (In this case, it's a false positive.)
    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' almost 10 years
      @KeithThompson Some malware scanners do delete files based on their names (more as a simple-minded way of making it hard to install software not approved by central IT than as a way to remove actual malware).
  • chapman
    chapman almost 10 years
    While I was attempting to solve the solution, I changed the for loop to loop from 0 to 5 rather than 0, to amount. Also I am running it in the command line. And I can't run a.exe if it's not there.
  • Scotty Bauer
    Scotty Bauer almost 10 years
    I see I mis-read your question. It says a.exe is disappearing, not the terminal window. My apologies.
  • chapman
    chapman almost 10 years
    I tried redFives suggestion and it didn't work. Also my antivirus would notify me if it were to quarantine a file.
  • chapman
    chapman almost 10 years
    Ahh, that makes sense :)
  • chapman
    chapman almost 10 years
    I think you're onto something though, since I printed some text after calling the print(int) function, and it executes fine. Any ideas?
  • Scotty Bauer
    Scotty Bauer almost 10 years
    try adding -O0 flag and see if that does anything. GCC could be optimizing everything out for some ungodly reason.
  • chapman
    chapman almost 10 years
    Same result, again :(
  • Scotty Bauer
    Scotty Bauer almost 10 years
    Try adding a \n after the %d to force a flush on stdout
  • chapman
    chapman almost 10 years
    Tried that, same result still.
  • aliteralmind
    aliteralmind almost 10 years
    It is customary to provide commentary along with your "code".
  • Slovakov
    Slovakov about 4 years
    I've faced the same problem and compiling my file this way helped. Any idea about how could I narrow down which library is causing a problem?
  • General Grievance
    General Grievance almost 4 years
    So... your solution is to turn off the thing protecting the computer?