To set up Visual Studio Code (VSCode) for C++ development on Windows, follow these step-by-step instructions:
Install Visual Studio Code:
- Download the Visual Studio Code installer for Windows from the official website: Download Visual Studio Code.
- Run the downloaded installer (typically named
VSCodeSetup-x64.exe
) and follow the installation instructions. You can choose the default settings during installation.
Install a C++ Compiler:
You’ll need a C++ compiler to compile and run your C++ programs. One of the most commonly used C++ compilers on Windows is MinGW (Minimalist GNU for Windows). Here’s how to install MinGW:
- Download the MinGW installer from the official website: MinGW Installation Manager.
- Run the MinGW installer (
mingw-get-setup.exe
) and follow these steps:- Select “Basic Setup” from the left-hand menu.
- In the “All Packages” section, select “mingw32-base” and “mingw32-gcc-g++” for installation or you can select all.
- Go to the “Installation” menu and click on “Apply Changes.”
- Confirm the installation of selected packages.
- Wait for the installation to complete.
Add MinGW to System Path:
To use MinGW from the command line, you need to add its bin
directory to your system’s PATH environment variable:
- Open the Windows “Start” menu, search for “Environment Variables,” and select “Edit the system environment variables.”
- In the “System Properties” window, click the “Environment Variables” button.
- In the “Environment Variables” window, under the “System variables” section, scroll down to find the “Path” variable and select it.
- Click the “Edit” button.
- In the “Edit Environment Variable” window, click “New” and add the path to the MinGW
bin
directory. The path is usually something likeC:\MinGW\bin
. Ensure each path entry is separated by a semicolon (;
). - Click “OK” to save the changes and close all the windows.
Install Visual Studio Code Extensions:
- Open Visual Studio Code.
- Go to the Extensions view by clicking the Extensions icon in the sidebar or pressing
Ctrl+Shift+X
. - Search for and install the following extensions:
- C/C++ by Microsoft: Provides C/C++ language support and debugging capabilities.
Create a C++ Project:
- Create a new folder on your computer where you want to store your C++ projects.
- Open VSCode and click on “File” in the top-left corner, then select “Open Folder” and choose the folder you just created.
- Inside the folder, create a new C++ source file with a
.cpp
extension. You can do this by right-clicking on the folder in the VSCode sidebar, selecting “New File,” and giving it a name likemain.cpp
. - Write your C++ code in the
main.cpp
file.
Compile and Run the C++ Program:
- Open the
main.cpp
file in VSCode. - To compile and run your C++ program, you can use the VSCode terminal or an integrated terminal:
- Open the integrated terminal by clicking “Terminal” in the VSCode menu and selecting “New Terminal.”
- In the terminal, navigate to the directory where your C++ file is located.
- Compile the C++ program using the g++ compiler. For example:
g++ -o myprogram main.cpp
This command compiles main.cpp
and creates an executable file named myprogram
in the same directory.
- Run the executable by entering its name in the terminal:
./myprogram
- You should see the output of your C++ program displayed in the terminal.
That’s it! You’ve successfully set up Visual Studio Code for C++ development on Windows. You can continue to write, compile, and run C++ programs within the VSCode environment.