VS Code: Write, Compile, & Run a C++ Program

Creating a Workspace in VS Code (helloworld example)

  1. If you do not already have VS Code open, then open it.
  2. On the left side menu, click on the Explorer Icon. The Explorer allows you to open folders and create files.
    explorer
  3. Click the blue Open Folder button.
  4. Navigate in the Open Folder window to where you want to save your programs for this course. I am going to navigate to C:/CSC 1300/ and then click the Select Folder button.
    open folder

  5. You may see a “Do you trust the authors of the files in this folder?” message. If you do, click the checkbox beside “Trust the authors of all the files in the parent” and click on the blue button “Yes, I trust the authors“.
  6. Now that this folder is open in the Explorer window, you can how hover beside the folder name and 4 icons will appear.
    • The first icon is New File
    • The second icon is New Folder
    • The third icon is Refresh Explorer
    • The fourth icon is Collapse Folders in Explorer
      4 icons
  7. Click on the New Folder icon to create a new folder in the folder that you opened and name it helloworld.
  8. Then, click on the helloworld folder, which will cause the arrow beside it to point downward instead of to the right.
    helloworld folder

Create a Source File (C++ Program)

  1. Then, click on the New File icon to create a file inside of the helloworld folder and name the file helloworld.cpp.

    new file
  2. Type the following lines of code in your newly created helloworld.cpp file.
    helloworld code
  3. Now press Ctrl + S to save the file.
    You can also alternately click on File and then Save.

Set Up Auto Save

You can enable Auto Save to automatically save your file changes, by clicking on File at the top and then scrolling down to click on Auto Save.

The Activity Bar on the edge of Visual Studio Code lets you open different views such as SearchSource Control, and Run. You can find out more about the other views in the VS Code at the link https://code.visualstudio.com/docs/getstarted/userinterface

Creating a Build Task to be able to Compile & Run helloworld.cpp Within VS Code

  1. While you are still in VS Code with your helloworld.cpp source file open, click CTRL + Shift + P
    (CMD + Shift + P on a Mac) on your keyboard, which is used to Create the Build Task.
  2. A menu will appear at the top. Type “Tasks: Configure Task” and select this option that appears.
    configure task
  3. Under the Select a task to configure menu, choose C/C++: g++.exe build active file from the list of options.
    Select g++ exe build task
  4. VS Code will generate a folder named .vscode within your helloworld directory and a tasks.json file within the .vscode folder. It will also automatically open the tasks.json so you can edit it.
    tasks.json
  5. Modify the compile args (arguments) as shown below:
    compile flags
  6. If your project has more than one source file, then you will need to modify this as shown below.
    list out all the sources
    compile with wildcard
  7. Open a Terminal so you can compile and run. Click on Terminal and then Run Terminal. If you do not see the Terminal link at the top, click on the three dots and then you will see Terminal.
    open terminal
  8. This will open the terminal.
  9. Then, build the file by clicking CTRL+Shift+B (or CMD+Shift+B on a Mac) and select C/C++:g++.exe build active file, which will open a Terminal and compile the file using your tasks.json file.
    select build
  10. If it is able to be compiled, you will see “Build finished successfully.”
    build finished successfully
  11. If there are errors (or warnings), you will see the errors listed and then “Build finished with errors.
    errors
  12. Open a Terminal so you can run the program. Click on Terminal and then Run Terminal. If you do not see the Terminal link at the top, click on the three dots and then you will see Terminal.
    open terminal
  13. Then, you will have to navigate to the location of your helloworld folder by typing
    cd helloworld. Then, you can run the program by typing ./helloworld, which will run the helloworld.exe file.
    run executable

Explore IntelliSense

IntelliSense is an intelligent code-completion tool that includes several features such as list members, parameter information, quick information, and complete word.  These features help you to learn more about the code you’re using, keep track of the parameters you’re typing, and add calls to properties and methods with just a few keystrokes. 

The Red Squiggles

Go to the helloworld.cpp file used in an earlier example. Create a Dog struct in this file like the one below and purposefully leave off the semi-colon when creating the Dog age data member.
red squiggly

Do you see the red squiggly line? Hover over the line to see a mini-version of the compile error. Note that compiling the source file is still the best way to thoroughly test your code. Also, please know that sometimes the red squiggly lines can sometimes be buggy – they can appear when they shouldn’t and vice versa. So trust the red squiggles WITH CAUTION.

Auto-Adding Code

Another thing that IntelliSense does is auto-fill in some code like the data members of a structure after typing the dot operator. See example below.
dog member