Search code examples
c++ceclipseexeeclipse-cdt

Run .exe file of C code using Eclipse


I've make a very simple basic code in C language using Eclipse IDE.

Eclipse details:

  • Eclipse IDE for C/C++ Developers
  • Version: Juno Service Release 2
  • Build id: 20130225-0426

Here is the code:

#include <stdio.h>

int main( int argc, char ** argv ) {
    printf("Hello, World!\n");
    return 0;
}

Runs successfully in Eclipse, it generates .exe & .o file in Project/Debug directory. I am trying to run that .exe file but it is not working.

Eclipse acts as if it ran the program very quickly and then terminates it. Window appears, but nothing happens when I run the .exe. It just looks like a flash of a dialogue box.

What should be the problem? I don't want to change the IDE and I've already tried these two things:


Solution

  • run the program from the command prompt window. If you simple double click the exe file .. it just runs the program and shuts out in an instant. You won't have any time to see it.

    Run it through a cmd window by navigating to the directory and doing ./yourexefile

    Or a terrible way to do this with double click is to do this :

    #include <stdio.h>
    
    int main( int argc, char ** argv ) {
       int n;
       printf("Hello, World!\n");
       scanf("%d",&n); // this will force the execution window to stay open until you put in some input
       return 0;
    }
    

    You could also do:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main( int argc, char ** argv ) {
       printf("Hello, World!\n");
       system("pause");
       return 0;
    }
    

    Another way ( more aesthetic ):

    #include <stdio.h>
    
    int main( int argc, char ** argv ) {
       printf("Hello, World!\n");
       printf(" Press enter to exit\n");
       getchar();
       return 0;
    }