in

SadSad lovelove

[Solved] the preLaunchTask ‘C/C++:gcc.exe build active file’ terminated with exit code -1 (VS Code)

the preLaunchTask 'C/C++gcc.exe build active file' terminated with exit code -1 Solution In VS Code

“The preLaunchTask ‘C/C++:gcc.exe build active file’ terminated with exit code -1” error in Visual Studio Code (VS Code) is a common issue faced by C++ developers. This error occurs when the integrated terminal in VS Code cannot locate the GCC compiler, which is required to build, run, and debug C++ programs.

In this article, we will discuss all the possible reasons which can cause this issue, and the ways to solve them. We’re confident that by the end of this article, you’ll have solved this error in your computer system.

Solving the “the preLaunchTask ‘C/C++:gcc.exe build active file’ terminated with exit code -1” Error In VS Code

As discussed earlier, this problem occurs when the terminal inside VS code fails to run the GCC compiler when trying to build your program.

However, there could be multiple reasons why VS code isn’t able to locate or run the GCC compiler. We’ll discuss all of them and provide solutions to them.

The steps to detect and solve the error are given below.

Step 1: Check Whether GCC Is Installed In Your System

This is to ensure that the compiler responsible for compiling your C++ code, is actually installed in your system.

In command prompt, power shell, or any other terminal installed in your system, type gcc –version. If GCC is installed correctly, its version number will be displayed on the screen, as shown below.

GCC Version check

Otherwise, it means that GCC is not installed in your system properly, and you need to install GCC first, from here.

Step 2: Check GCC Installation Path In Environment Variables

If the gcc -version command doesn’t work on your terminal even after installing GCC, you need to check whether the environment variable is correctly set up for GCC.

To check the same, press CTRL + R to open the Run box, type sysdm.cpl, and hit Enter.

The System Properties window will open. Go to the Advanced tab, and click on the Environment Variables button, as shown in the picture below.

Advanced System Properties

The list of user and system variables will appear. Now, select the Path variable from under user variables, and double-click on it. Make sure the location of your GCC compiler’s executable is mentioned there. If not, add it there. In the picture given below, we’ve used mingw64.

Step 3: Check If C/C++ Extension For VS Code Is Properly Installed

The C/C++ Extension for VS Code configures the GCC environment for programs to be built and run in VS Code smoothly.

Go to the extensions tab in VS Code (CTRL + SHIFT + X), type “C/C++”, and install the extension if not already installed.

Step 4: Check If The Extension Is Configured Properly

Press CTRL+SHIFT+P inside VS Code to launch the command palette, and type “c/c++: edit configurations (ui)“.

Select the option shown in the picture given below, and hit Enter.

Make sure that all the values in your configuration match the values given below.

  1. Compiler path: This varies based on your GCC installation path and operating system. Double-check to make sure that the location mentioned in your compiler path is correct or not. If not, update it with the right location of the gcc executable.
  2. IntelliSense mode: As shown in the image below.
  3. Include path: ${workspaceFolder}/**
  4. Defines: As shown in the given image.
  5. C standard: gnu17
  6. C++ standard: gnu++ 17

Alternative Solution

We’d like to thank @seungxx3051 on YouTube for sharing the solution mentioned below.

If anyone is still having this issue, kindly follow the steps given below:

  1. From the “Edit Configurations (UI)” access the “c_cpp_properties.json” file and change the “compilerPath” to g++.exe instead of gcc.exe.
  2. In the folder of your cpp file you’ll find a “.vscode” folder. Open the file “tasks.json” inside it.
  3. Change the “command” path to g++.exe instead of gcc.exe

I hope that this article was helpful in resolving the “The preLaunchTask ‘C/C++:gcc.exe build active file’ terminated with exit code -1” error in Visual Studio Code (VS Code). By following the steps outlined in the article, you should be able to get back to building and running C++ programs in VS Code without any further issues.

If you still encounter any issues or have further questions, please feel free to comment down below, and I’ll try my best to help you out.

What do you think?

105 Points
Upvote Downvote

Written by Anirban Roy

Anirban is a full-stack developer and an SEO expert. He has 6 years of experience in web development and software engineering. With a passion for writing, he has been blogging since 2017.

Leave a Reply

Your email address will not be published. Required fields are marked *

5 Comments