Linux Native for Programming C++

Linux

Setting up VS Code under Linux by following this video by Dr. Brian, or this guide from Microsoft.

Install Tools in Linux

  1. Open a Linux terminal run the command to update the package manager:
    sudo apt update
    (Enter you password as needed; it won't show anything as you type.)
  2. Install C++ compiler and debugger:
    sudo apt install g++ gdb
    Select yes if asked to confirm.
  3. Verify tools installed with the following commands in the Ubuntu terminal:
    g++ --version
    gdb --version
  4. Create a CMPT130 directory for your work:
    mkdir cmpt130

Install VS Code

Follow these steps to configure VS Code, taken from this Microsoft guide.

  1. Into Windows, download and install VS Code.
  2. File > Open Folder; select cmpt130 from the list, click OK.

Compile and Run

Now, each time you want to work on CMPT 130 C++ code, open VS Code. It should re-open your cmpt130 folder for you.

  1. Create a new folder for your work, using the Explorer view (left side). Name the folder something like lab1.
  2. Create a .cpp file inside the lab1 folder. Name it something like lab1.cpp. The .cpp extension is critical!
  3. If asked, install the C++ IntelliSense and other C++ extensions.
  4. Build the current file:
    Select Terminal > Run Build Task
    Select C/C++: g++ build active file (note it's g++!!) (Click the cog icon on this option if it's available)
  5. Run the current file: Run > Start without debugging
    You should see some output in the Debugger tab, plus your program's output and input will be in the Terminal tab.
  6. You should see a play button to the top right of your .cpp file. You can now click this to run your program.

Troubleshooting

  • Ensure you have your .cpp file open and are clicked into it before trying to build or run the file. For example, trying to build and run a .json file will fail.
  • Try deleting the .vscode folder and repeating the Compile and Run steps. Ensure you are selecting the g++ options, instead of the gcc options!
  • If you don't see the play button to run your code:
    • Ensure you have C++ IntelliSense addon installed in VS Code
    • Ensure you have your .cpp file open.
    • Ensure your file's name ends with .cpp.
    • Ensure you have installed g++ and gdb
    • If you still don't see the play button, try installing the "Code Runner" extension. It has worked for some students.

Submitting Code from Linux Native

With a native install under Linux, all your code is stored on your personal computer in the cmpt130 folder that you created to start with. Each time you launch VS Code you'll need to re-open that folder (if not done automatically).

To submit your C++ code (.cpp files) for labs or assignments, use your normal web browser and upload your files from the cmpt130 folder.

Note: Since the files are on your home computer, it's essential that you backup your files. I recommend having an automatic backup program for these files. Almost every student experiences a file loss some time throughout their degree (I did, twice!); have a backup!