I have made a c program with codeBlocks on windows 10, but the exe crashes as soon as I open it, actually it does not matter the program all exe produced by codeblocks won't run, maybe there are some problems with the settings? Cause if I run the program from inside codeBlocks it runs without problems, any suggestion?
Asked
Active
Viewed 197 times
0
-
Does the program `int main(void) { return 0; }` (yes, that's the whole program) crash? – pmg Jul 09 '21 at 14:50
-
Yes,it does.... – Peppe Jul 09 '21 at 14:59
-
1What exactly does "crashes" mean? Do you get a segfault or other error message? Does the window close immediately after starting the program? How do you start the program? – Gerhardh Jul 09 '21 at 15:00
-
the window opens for a split second and then closes again, i just double click on the exe file – Peppe Jul 09 '21 at 15:01
-
1Well ... it's not a crash! You're using the wrong term for what you see hapenning. – pmg Jul 09 '21 at 15:01
-
1Does your program contain any user input? If not, I would assume the program just terminates without error and as a result the window is closed. Try to open a cmd window, change folder to your program folder and run your program in cmd window – Gerhardh Jul 09 '21 at 15:02
-
Try adding `puts("Press ENTERs"); getchar(); getchar(); getchar();` right before the final `return`. Use the proper `#include`s. – pmg Jul 09 '21 at 15:03
-
the program is a project, takes different user inpputs, reads stuff from files etc,the thing is it just happens with codeblocks, i tried devC++ and everything is fine – Peppe Jul 09 '21 at 15:07
-
If the program works in one environment but not in another (e.g. Windows vs. linux, linux vs macOS, one IDE vs another), this usually indicates UB (undefined behavior). (e.g.) You could be indexing too far past the end of an array. See my recent answer for a very similar problem: https://stackoverflow.com/questions/68276119/why-i-keep-getting-aborted-core-dumped-in-kali-vm/68277133#68277133 – Craig Estey Jul 09 '21 at 15:13
-
Welcome to Stack Overflow. Please read the [About](http://stackoverflow.com/tour) page soon and also visit the links describing [How to Ask a Question](http://stackoverflow.com/questions/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) (MCVE). Providing the necessary details, including your MCVE, compiler warnings and associated errors, if any, will allow everyone here to help you with your question. – David C. Rankin Jul 09 '21 at 19:15
1 Answers
0
You are compiling "command line programs", which don't open a window on their own. Since they apparently don't expect any user input they run to the end quite happily and quit.
If you want to run this type of programs, you need a shell, which provides a window.
This is a simple way to get one: Open a file explorer window and navigate to the directory containing your program. Click into the address bar so that it changes into a textual representation of the path. Replace all of it with the word cmd
and press enter. Now the shell's window opens, commonly with white text on a black background. Enter the name of the program, and it will run.

the busybee
- 10,755
- 3
- 13
- 30
-
Or you can add `getchar();` as the last command to hold the window open until a key is pressed `:)` – David C. Rankin Jul 09 '21 at 19:14
-
@DavidC.Rankin ... which renders such a solution annoying if the program is used in a shell or batch. Why not learn the correct way from the beginning? ;-) – the busybee Jul 10 '21 at 12:11
-
Most, learning "Hello World" programs will never see further use. Yes, I would advocate compiling and running all console applications from the terminal to begin with, but then there are editors masquerading as IDE's like codeblocks used by a number of new programmers that just need to hold the terminal open... – David C. Rankin Jul 10 '21 at 17:14