r/C_Programming • u/RealMuffinsTheCat • Jul 16 '26
Question Compiling and running a C file in VSCode
I am learning C as part of my uni course. Getting it to compile and run has been the nightmare. I'm trying to run a simple "Hello, world!" program, but unfortunately, "It just works" isn't applying here.
The code is fine, but when I try to compile and run the file, VSCode tells me it compiles successfully, then tells me that the output exe does not exist when it tries to run it. I look inside the output folder, and it is indeed not there.
I have managed to get it to compile and run correctly using a series of G++ commands, and once it's compiled, VSCode lets me run the file in the terminal just fine, but I don't want to have to do this every single time I need to compile and run.
How can I get VSCode to compile and run my C code at the touch of a button, rather than manually running commands every time?
11
u/NerdStone04 Jul 16 '26
For VSCode, there's an extension called CodeRunner and this will allow you to run your program with the click of a button.
Though I'd suggest you get familiar with the terminal instead of taking it easy with a button. The terminal is going to be your best friend as you keep using it.
For your reference (you can skip this if you think it isn't valuable),
In your terminal,
you run the
gcc filename.cand this creates an executable file calleda.outThen you just run by typing the filename out in relative path in your terminal again, like so
./a.outSince you mentioned that your code did not generate an output file, you can verify your code runs successfully by adding a few flags when you compile. Like,
gcc filename.c -WallThe -Wall compiler flag will generate warnings that might be potentially causing the issues.
Adding another flag,
gcc filename.c -Wall -WextraThe -Wextra will generate more warnings that were skipped by -Wall.
I recommend that you try these out and see if the compiler generated any sort of warning that might have led to the output file to not generate.
13
u/National-Self-8501 Jul 16 '26
Vscode is not a compiler. To make your code run you need a compiler. Go learn to use a compiler. Ignore people saying to use visual studio.
1
u/bare_metal_C Jul 17 '26
you will be happy when you switch to Linux, nothing you need to install to run a c program. gcc compiler pre-installed and a bare minimum editor like nano or vim
3
u/UltimaN3rd Jul 16 '26
Here's my tutorial on setting up VSCode as a C IDE on Windows:Β https://goldenpathprogramming.com/oasis/setup/vscodium/windows/
9
u/awidesky Jul 16 '26
If you can, just use Visual Studio.
VS Code is just a fancy code editor. All the rest(finding toolchain, compiling, linking, running, debugging) must be done by extensions.
If you're lucky, you can just install a single C/C++ extension and "it just works".
Otherwise, you might waste a lot of time installing MSYS2 or MingW, installing VS code extensions, configuring them all..
Just use VS and save your time.
1
u/Mediocre_Chart_8624 Jul 16 '26
I think there was a plugin called "runner". I am pretty sure a simple google search can tell you about a plugin to compile c as well. Everything in VSCode are done by plugins
1
u/GhostVlvin Jul 17 '26
If you are lazy to write commands for compilation and running every time there are options:
1
u/beanfromthesun Jul 17 '26
If you're using windows, getting vscode to run c code at the press of a button is very hacky and requires a lot of configuration that might not be very transferable to other systems. What i'd recommended instead, if you insist on vscode, is getting wsl (specifically Arch, as it's the lightest and im assuming youre only using it for this) then making sure you have make and gcc installed, i dont remember if they come bundled. Then you can create a makefile which can compile and run the script with a single command if configured properly. Just enter wsl in your working directory and run the make command. Alternatively use msys2 in your path and also install gcc and make but thats not what I'd recommend, its harder to work with in my experience. also i would recommended using gcc or clang to compile instead of g++ as theyre dedicated c compilers
1
u/Vollink Jul 24 '26
Microsoft Compiler on the Terminal...
CL.EXE /c /utf-8 /O1 /Foanban.obj anban.c
LINK.EXE /OUT:anban.exe anban.obj
1
u/danja Jul 16 '26
Native coding is easier on Linux, so you might want to run a Virtual Machine or make a partition for it and dual-boot. But you still have to jump through a bunch of hoops : either CMake scripts or whatever and/or loads of VS Code plugins.
1
u/Ironraptor3 Jul 16 '26 edited Jul 16 '26
It may be helpful to inform / show us the following:
Off that bat, I'm not sure why you don't just use a Makefile, since you already have a "series of commands" which seems to correctly compile the file. The point of a Makefile (and similar constructs) is to run one or more commands whenever a source file changes to produce a target.
For example, if you are trying to compile foo.c into foo (which is an executable), and to do so you run commands
You can simply have a Makefile which looks like:
and run
makefrom the VSCode terminal. Then, you are free to run your program in the terminal as normal, in this case,foo1
u/AndrewBorg1126 Jul 16 '26
Text editors don't have built in compilers. Try Visual Studio, an IDE that does have a built in compiler.
-11
u/arihoenig Jul 16 '26
Ever hear of linking?