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
- 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.) - Install C++ compiler and debugger:
sudo apt install g++ gdb
Selectyes
if asked to confirm. - Verify tools installed with the following commands in the Ubuntu terminal:
g++ --version
gdb --version
- Create a CMPT130 directory for your work:
mkdir cmpt130
Install VS Code
Follow these steps to configure VS Code, taken from this Microsoft guide.
- Into Windows, download and install VS Code.
- 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.
- Create a new folder for your work, using the Explorer view (left side). Name the folder something like
lab1
. - Create a .cpp file inside the
lab1
folder. Name it something likelab1.cpp
. The .cpp extension is critical! - If asked, install the C++ IntelliSense and other C++ extensions.
- Build the current file:
Select Terminal > Run Build Task
SelectC/C++: g++ build active file
(note it'sg++
!!) (Click the cog icon on this option if it's available) - 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. - 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 thegcc
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++
andgdb
- 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!