r/C_Programming • • 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?

14 Upvotes

50 comments sorted by

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.c and this creates an executable file called a.out

Then you just run by typing the filename out in relative path in your terminal again, like so

./a.out

Since 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 -Wall

The -Wall compiler flag will generate warnings that might be potentially causing the issues.

Adding another flag,

gcc filename.c -Wall -Wextra

The -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.

1

u/RealMuffinsTheCat Jul 17 '26

I have no issues with learning more terminal commands, in fact I enjoy running the occasional one. However I’m much too lazy to use it every single time I wanna compile and run lol

2

u/GhostVlvin Jul 17 '26

Either build.sh script or GNU make is just for you then. You can reduce your command to just make run

1

u/PeterDaGrape Jul 18 '26

Takes a bit of setup but absolutely worth it, ai might help with constructing the makefile

1

u/okimiK_iiawaK Jul 22 '26

Configure alias commands or learn make.

`alias pgcc"='gcc *.c - Wall -Wextra' just as an example

Make can be a bit trickier to learn, but it can be so satisfying once you use it and just have to type "make" to compile

-7

u/max123246 Jul 16 '26

They're on windows, GCC won't work. They'd need to use something like WSL first

3

u/NerdStone04 Jul 16 '26

MinGW comes with GCC

7

u/max123246 Jul 16 '26

I would still recommend WSL over MinGW everytime for a developer environment on windows.

2

u/NerdStone04 Jul 16 '26

can't argue with that

-2

u/wtdawson Jul 16 '26

It really depends on what for, and people's preferences

1

u/erroneum Jul 17 '26

MinGW is gcc, but configured to run on and target Windows. Cygwin is an entire compatability layer which mostly lets Linux software run without source modification on Windows. Neither of these are WSL.

Besides, OP what mentioned that they'd used gcc to produce and run an executable, but found it tedious.

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.

5

u/RealMuffinsTheCat Jul 16 '26

I have installed msys64 and done the edit the system environment variables thing and told VSCode to use it. Are there more steps I need to take to get it to work?

8

u/max123246 Jul 16 '26 edited Jul 16 '26

Don't use msys64. Use WSL (Windows Subsystem for Linux). There's a VSCode extension for WSL as well. It'll make your life far easier because you can develop using Linux-native tools without needing to dual-boot. Use Windows for GUI/Vscode, Linux for shell/terminal/programming

https://learn.microsoft.com/en-us/windows/wsl/about

Honestly, don't listen to half the other comments in this thread, it's awful advice and will fall apart as soon as you try to do more advanced work.

If you have trouble learning the terminal and shell commands, I'd highly recommend this MIT course called "The missing semester of your CS Education" which goes over a lot of this: https://missing.csail.mit.edu/

-5

u/RealMuffinsTheCat Jul 16 '26

I’m using Windows tho

5

u/National-Self-8501 Jul 16 '26

You need to actually stop and read what's going on. Use your brain and comprehend what people are saying and study the documentation. Yes it will take you time.

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

1

u/RealMuffinsTheCat Jul 17 '26

Jesus Christ you’re making it sound like a cult πŸ˜‚

Definitely considering turning my pc into a dualbooter tho

1

u/bare_metal_C Jul 17 '26

πŸ˜‚πŸ˜‚

1

u/jschadwell Jul 17 '26

If you don't want to go full Linux, you could set up wsl2. It might give you enough of Linux to do what you need to do. Assuming you're running Windows 11.

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/FACastello Jul 16 '26

I came here just to say this.

On Visual Studio Community you don't need to mess around with extensions, makefiles, none of that bullshit. You just press F5 and it "just works"

0

u/TheChief275 Jul 16 '26

Sorry, but I maybe would agree for C++ but MSVC is absolutely dogshit for C

8

u/[deleted] Jul 16 '26

[removed] β€” view removed comment

-3

u/mrheosuper Jul 16 '26

It's a text editor, like vim or notepad, not ide.

7

u/azurfall88 Jul 16 '26

They never said it was an IDE. They only used the neutral term "developer tool".

1

u/timschwartz Jul 16 '26

Of course it's an IDE.

0

u/awidesky Jul 16 '26

Learning VS code is of course good, if you can setup your toolchain and extensions correctly. But, it's not an IDE.
IDE is Integrated Development Environment.
It has integrated sdk(compiler, linker, debugger, syntax highlighter, etc) with itself.
It's a development environment; it uses its integrated sdk to build, run, or debug a project with a single button click.

You can't call it a Integrated "Development Environment" when you extensions and external toolchain to just compile a source file.
"Extension" means it does more than what main program is supposed to do. VS code needs extension even for syntax highlighting.
It's not an IDE, it's supposed to be, and is a text editor.

I agree on "VS code is the best ubiquitous text editor", and "learning how to configure VS code with various toolchains is good for learning how they work".
But when someone is a beginner, or wants to "click a button and just works", VS code wouldn't be my suggestion.

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/NerdStone04 Jul 16 '26

code runner.

1

u/GhostVlvin Jul 17 '26

If you are lazy to write commands for compilation and running every time there are options:

  1. Creating build.sh script for building and run.sh for building and running
  2. Creating Makefile with rules for building and for running

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.

4

u/max123246 Jul 16 '26

Windows Subsystem for Linux (WSL) over either of those options.

0

u/danja Jul 16 '26

No doubt right.

It's been years since I touched Windows so really haven't a clue about the ecosystem. (Funnily enough I currently have the opposite problem, how to do Windows builds on Linux).

1

u/imradzi Jul 16 '26

I've ask copilot to modify my cmake to enable cross compilation to win32 and 64, after many tries and it build the library i needed for windows, now it works. I have one cmake with 3 targets: linux, win32 and win64. The windows output used static link, and I just need to copy the exe to run. No need installation or any lib. Really thankful to github copilot.

1

u/Ironraptor3 Jul 16 '26 edited Jul 16 '26

It may be helpful to inform / show us the following:

  • what OS? (is it Windows because you are targeting an .exe?)
  • the code
  • how exactly you are compiling it
  • compiler details / versions / paths (and what is VSCode trying to run?)
  • any error messages associated with VSCode failing to build or run the build target

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

abc
xyz
foobarbaz

You can simply have a Makefile which looks like:

foo: foo.c
   abc && xyz && foobarbaz

and run make from the VSCode terminal. Then, you are free to run your program in the terminal as normal, in this case, foo

2

u/max123246 Jul 16 '26

You answered your own question. They're on Windows. None of what you said is applicable unless they install Windows Subsystem for Linux (WSL)

2

u/Ironraptor3 Jul 16 '26 edited Jul 16 '26

I mean... not necessarily right? A new person could think they they need a file named foo.exe even on Linux.

Also, why would relevant VSCode configuration and logs / error messages be "not applicable"? I'd like to see the output of VSCode "compiling" the program and then nothing being in the build directory.

Also sure, if they are on Windows, don't install e.g. WSL, and want a "Makefile" (aka run the set of commands that gives them a working state) they could just write a batch script to run the commands that they say work.

Perhaps some of this is irrelevant / fluff; but I tend to encourage individuals towards asking questions more effectively, and that involves providing a lot of details and such. And if they knew which ones were irrelevant, they might be asking an altogether different question (or fixing it themselves).

1

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?

3

u/Irverter Jul 16 '26

Which is unrelated to the issue as he stated he can compile the code, but can't run it in vscode, but can run it manually.

0

u/theNbomr Jul 16 '26

Or, to put it in friendlier terms, there is more to the process than merely compiling. The compiler process translates C language source code into a binary formatted object module. The object module needs to be linked with the runtime libraries your code uses, including the standard C library (usually called libc) and any other object modules that you or others may have created to comprise the final executable.

Since there are evidently no problems with the compilation, you should check for the presence of the compiler output. It's possible that you do need to deliberately link the object code. Does your IDE give you a 'build' command that possibly does everything in one step?