Compiling and Running C/C++ code in VS Code in Windows 11

In this blog post, we will discuss how to compile and run C/C++ code using GCC in VS Code on Windows 11.
Why doesn't VS Code work directly for C/C++?
VS Code is a powerful code editor that supports a wide range of programming languages, including C/C++. However, it doesn't natively include the compiler used for C/C++ development on Windows. This is why we need to separately install GCC to run C/C++ code in VS Code.
Steps to Install GCC and setting it up in VS Code
- Install GCC:
- Visit Winlibs and install the latest version.
- After downloading, find the installed zip and extract it.
- Find the file in Downloads folder.
- Extract the zip file.
-
Follow the instructions to place the files in the correct directory.
- Open the extracted folder and find the `mingw64` folder.
- Copy the `mingw64` folder.
- Paste the `mingw64` folder in the `C:\` directory.
-
Adding the path to the system environment variables.
- Copy the path of the `bin` folder.
- Open the Start Menu and search for "Environment Variables".
- Click on "Edit the system environment variables for your account".
- Click on "Path" and then "Edit".
- Click on "New" and then paste the copied path.
- Click on "OK" to close the window and exit.
- Download VS Code from here.
-
Install it.
- Locate the downloaded file.
- Double click on the file and click accept and next.
- Click on next.
- Click on next.
- Tick all options and click on next.
- Click on install.
- Open VS Code, Click on `File > Auto Save`
- Click on the extensions icon and install these two extensions from here and here
-
Setting up the C/C++ extension.
- Click on the extensions icon.
- Search on the settings icon of the C/C++ extension.
- Here find the `C_Cpp > Default: Compiler Path` and click on the `Edit in settings.json`.
- Add the following path to the compiler in the `compilerPath` field. [Note: for cpp files, add `g++.exe` and for c files, add `gcc.exe`]
- Now find the `C_Cpp: Debug Shortcut` and untick it.
-
Setting up the Code Runner extension.
- Click on the extensions icon.
- Click on the settings icon of the Code Runner extension.
- Find the `Code-Runner: Run In Terminal` and tick it.

- Open a new file and write a simple C/C++ code.
- Click on the run icon in the top-right corner.
#include <stdio.h>
int main() {
printf("setup complete!\n");
return 0;
}


Conclusion
Compiling and running C/C++ code in VS Code on Windows can be a bit challenging, but it is a rewarding experience. With a little practice, you will be able to write and debug C/C++ code like a pro.
In this blog post, we have discussed how to compile and run C/C++ code using GCC in VS Code on Windows.
Additional Resources
I hope this blog post has been helpful. If you have any questions, please feel free to leave a comment below.
Comments
Post a Comment