r/C_Programming • u/IndoRexian2 • May 21 '26
Question gcc unable to create the executable
Hi y'all, I've been trying to learn C and I installed mingw using msys2.
I'm trying to compile the following code:
// hello.c
#include <stdio.h>
int main(void)
{
printf("Hello, Windows!\n");
return 0;
}When running gcc hello.c -o hello.exe, I don't get any executable. I've tried reinstalling mingw, and I've also checked that I'm running the command on the same directory where the file. I also added the particular folder to Windows Defender Exclusion list but still can't get anything done. What am I doing wrong?
Edit: Got it to work by changing my terminal from Powershell to Git Bash.
Edit 2: Even msys2 terminal works. I'm using that only from now-onwards.
7
u/TheOtherBorgCube May 21 '26
The command
gcc -v hello.c -o hello.exewill print out a lot of diagnostic telling you what the compiler is up to, what files it's using and places where it's looking for headers and libraries.The next command would be
echo $?to get the exit status of the compiler, which should be 0. If you get some other number, that might suggest it's silently failing for some reason.Last, do
ls -a, just in case it's creating a dot-file, which doesn't show up with normalls.2
u/ConstantElegant5781 May 22 '26
Unless I'm missing something, the problem is that the source file(s) were listed before a gcc command line option(s). Instead of:
try:
gcc -o hello.exe hello.cor better yet:
gcc -o hello.exe -- hello.c
Most shells use the '-' option to specify the final flag argument. Normally this is not needed, but it allows the specification of a positional parameter that starts with a dash. For example, a source file with a name like "-likely_poor_source_filename.c". Most users won't need to do something like this, but I'm a retired test engineer, and I frequently did things that were unusual.
12
u/SRART25 May 21 '26
Edit, adding semicolons since newline are ignored.
pwd; gcc -v hello.c; gcc - v hello.c -o hello; ls -A; . /hello;
Paste everything back as a response if you don't get it working like that.
1
u/BarracudaDefiant4702 May 21 '26
Any output after you put in the gcc command? If you do a directory command, what files are in the directory?
3
u/harrison_314 May 21 '26
First step, run
gcc -vto check if you have gcc in your PATH.2
u/am_Snowie May 21 '26
Just type gcc hello.c and check what happens.
1
u/furdog_grey May 21 '26
Use native msys2 terminal. Otherwise pretty much software will silently fail and you'll never have any output. Not windows powershell and not even git bash.
I used git bash before, unless it stopped work by the same way. It just silently failed.
1
u/sam661203 May 27 '26
Instead of using mingw or git bash, I’d recommend using wsl2 on windows, it will give you full Linux support (Ubuntu)
0
u/[deleted] May 21 '26
[deleted] — view deleted comment