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

Compiling and Running C/C++ Code Using GCC in VS Code on Windows

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

  1. Install GCC:
    1. Visit Winlibs and install the latest version.
    2. winlibs
    3. After downloading, find the installed zip and extract it.
      1. Find the file in Downloads folder.
      2. winlibs
      3. Extract the zip file.
      4. winlibs
    4. Follow the instructions to place the files in the correct directory.
      1. Open the extracted folder and find the `mingw64` folder.
      2. mingw64
      3. Copy the `mingw64` folder.
      4. Paste the `mingw64` folder in the `C:\` directory.
      5. mingw64
    5. Adding the path to the system environment variables.
      1. Copy the path of the `bin` folder.
      2. bin
      3. Open the Start Menu and search for "Environment Variables".
      4. env
      5. Click on "Edit the system environment variables for your account".
      6. Click on "Path" and then "Edit".
      7. path
      8. Click on "New" and then paste the copied path.
      9. path
      10. Click on "OK" to close the window and exit.
  2. Install VS Code:
    1. Download VS Code from here.
    2. Install it.
      1. Locate the downloaded file.
      2. downloaded file
      3. Double click on the file and click accept and next.
      4. accept and next
      5. Click on next.
      6. next
      7. Click on next.
      8. next
      9. Tick all options and click on next.
      10. select all
      11. Click on install.
      12. install
  3. Setting up VS Code:
    1. Open VS Code, Click on `File > Auto Save`
    2. Click on the extensions icon and install these two extensions from here and here
    3. winlibs
    4. Setting up the C/C++ extension.
      1. Click on the extensions icon.
      2. Search on the settings icon of the C/C++ extension.
      3. settings
      4. Here find the `C_Cpp > Default: Compiler Path` and click on the `Edit in settings.json`.
      5. configurationProvider
      6. 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`]
      7. compilerPath
      8. Now find the `C_Cpp: Debug Shortcut` and untick it.
      9. debugShortcut
    5. Setting up the Code Runner extension.
      1. Click on the extensions icon.
      2. Click on the settings icon of the Code Runner extension.
      3. Find the `Code-Runner: Run In Terminal` and tick it.
      4. runInTerminal
  4. Testing the setup:
    1. Open a new file and write a simple C/C++ code.
    2. 
      #include <stdio.h>
      
      int main() {
          printf("setup complete!\n");
          return 0;
      }
                      
      run
    3. Click on the run icon in the top-right corner.
    4. run

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

Popular posts from this blog

Compiling and Running Assembly Code Using NASM & WSL on Windows