How to Install & Setup VS Code for running C++ Programs

Setting up Visual Studio Code (VS Code) for running C++ programs is a straightforward process. Here's a step-by-step guide to help you through the installation and setup:

1. Install Visual Studio Code:

  • Go to the Visual Studio Code website and download the installer for your operating system (Windows, macOS, or Linux).
  • Follow the installation instructions provided by the installer

2. Install C++ Extension:

  • Open Visual Studio Code.
  • Go to the Extensions view by clicking on the square icon on the sidebar or by pressing `Ctrl+Shift+X`.
  • Search for "C++" in the Extensions Marketplace.
  • Click on the "Install" button for the "C/C++" extension provided by Microsoft.

3. Install a C++ Compiler:

  • For Windows: You can install MinGW (Minimalist GNU for Windows) or use the Visual C++ Build Tools provided by Microsoft.
  • For macOS: Install Xcode Command Line Tools by running `xcode-select --install` in the terminal.
  • For Linux: Install `build-essential` package or equivalent via your package manager. For Ubuntu/Debian, you can use `sudo apt-get install build-essential`.

4. Configure Visual Studio Code:

  • Open your C++ project folder in Visual Studio Code or create a new one.
  • Create a new file named `tasks.json` in the `.vscode` directory of your project (you may need to create the `.vscode` directory if it doesn't exist).
  • Add the following configuration to `tasks.json` to build and run your C++ program
{
"version": "2.0.0",
"tasks": [
    {
        "label": "build",
        "type": "shell",
        "command": "g++",
        "args": [
            "-g",
              "${file}",
              "-o",
              "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]
}
  • Save `tasks.json`.

5. Writing and Running C++ Code:

  • Create a new C++ file (e.g., `main.cpp`) in your project folder or open an existing one.
  • Write your C++ code in the editor.
  • To compile your code, press `Ctrl+Shift+B` or run the "Tasks: Run Build Task" command from the Command Palette (`Ctrl+Shift+P`).
  • After successful compilation, the executable file will be generated in the same directory.
  • You can run the executable by typing `./filename` in the terminal integrated in VS Code or by navigating to the directory where the executable is located and running it from there.

Additional Tips:

  • You can debug your C++ code using the built-in debugger in Visual Studio Code. Ensure you have set up breakpoints and configurations appropriately in `launch.json`.
  • Customize your VS Code settings and keybindings according to your preferences.
That's it! You should now have Visual Studio Code set up for writing and running C++ programs.

Comments

Popular posts from this blog

Why learn to code ?

Unlocking the Mysteries of the Universe: A Journey into Quantum Mechanics