C++ Devlog 1

#C++#VS Code#WSL

Installing C++ and WSL in VS Code..

This will be a way to journal down my learning process for C++ kind of like a dev log. I will be logging down all the issues I face and the reasoning behind some of my decisions. Since this is my first post, there’s a lot for me to learn about writing and the platform itself. But one baby step at a time.

Why use WSL and VS Code to install C++?

  1. So that I can play around for a feel of Linux style cross-platform dev experience. Hopefully, I’ll become more familiar with it since I rarely get the chance to use Linux at work.
  2. Once I am ready, I plan to put my code to my home server (to be setup later).
  3. My professor used to recommend this route and I did a whole semester in this way. (mainly I think he want to cater the materials to both the mac’s and window’s users)

Where to get started?

We’ll begin with the official VS Code documentation: Using C++ and WSL in VS Code.

Setup Linux environment

The first step already gave me an error —  partly because I didn’t read the instruction carefully. (Lesson: good programmer should first be good at following instructions!) So I decided to follow the documentation on installing Linux on Windows with WSL, instead of using the Microsoft Store. *I had another bad experience of it when using it to install python too, so just avoid it, do it properly. WSL install error in the terminal

I will just use the default Linux distro (Ubuntu) that comes with the following command wsl —install Alternatively, as suggested by the documentation we can use the wsl — list — online to see what other distro we can play with.

WSL Valid Distribution list

Virtual Machine Platform and WSL feature issue

Alright seems like I get another error here. Which I think is due to some virtual machine platform feature not enable and also to allow Linux distro to run on windows. WSL missing feature error

Open PowerShell as Administrator and run the following then restart

Terminal window
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

WSL enable feature

After restarting, I reran the install command, and this time it worked! Reinstall ubuntu

Note

When setting up your password in Linux, nothing will show on screen — this is called blind typing. It’s normal. The account will also be the admin account that can run sudo (Super User Do) commands.

MinionHooray

Install VS Code

This should be quite straight forward, simply download and install.

VS Code Extensions

Install the following extensions as the title suggests. Remote Development extension pack Remote Development extension pack steps

Before we start installing more tools, update the Linux distro to the latest package. Follow this link to update. One thing to note is that WSL extension splits VS Code into a “client-server” architecture with the client (our UI) running on our Windows machine and the server (our code, git and plugins, etc) running “remotely” in our WSL distribution. AKA we have to install our local (window’s) extension into WSL.

Local VS WSL Extension

LOCAL and WSL have their own extension list Steps to install into WSL

Install the relevant extensions into WSL.

Opening WSL project in VS Code

There are a few ways we can access the folders in WSL.

  1. Using the distro command line

    Open the distro command line (for me, it would be Ubuntu) > then use code . to open the VS code in the Linux. But this also means it will open the default folder, for me, its /home/user. So we could simply create the project directory like

    Terminal window
    mkdir cpp_practice
    cd cpp_practice
    mkdir helloworld
  2. Using Windows files system normally to navigate and create our project folders.

Windows File System

  1. Directly use VS Code

Create the project folder then use ctrl + shift + p to open folder in WSL and put in the path in my case is home/user/cpp_practice

Direct Use VS Code

create project folder in home directory (or which ever we like using vs code or command line inside vs code)

WSL open folder

Then we can open the project directory

open WSL folder

input existing project directory only

Installing compiler

We install by typing into the command the following. It will ask for password for admin we setup up earlier and permission to continue. Just press Y and continue (like arguing with your partners, just agree and move on).

Terminal window
sudo apt-get install build-essential gdb

After installing, we can then check if we can locate the compiler and debugger with whereis g++ and whereis gdb.

Locating Compiler

Finally adding some cpp code

Click “new file” → name of the file say name of the file say helloworld.cpp → we will see the below suggestion to add more extension pack for cpp. Again, agree/install and move on. Cpp Vscode Extension

Tada, we have all the cpp avengers added too, oh my and the CMake always give me so much trouble during my lessons in the past. Never mind, in this dev log series I will try to get a better understanding of you and how you work.

CMake Extensions

Time to add some code.

From here on, I will be following along with this documentation and see if I face any issues. I will paste the code here just for easy reference.

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}

When I first saw this code, I wondered why the Hello World looked so “extra”? Then I realised that this is to allow us to debug when there is something to loop though and test our debugger.

Explore IntelliSense

Hover over vector → see type info ✔ Vector Intellisense

Type msg. → see autocomplete ✔

AutoComplete

Compile and run

Compiling Code

So I am going to choose g++ one without explicit call to a version, for me, it is -13 . For now, I choose g++ because it is the standard compiler for C++ code since I have no specific needs to use version -13 nor will I be using C. In that case, I would perhaps choose gcc. Although, using gcc should be fine to run these simple helloworld.cpp kind of files or practice some LeetCode problems. Nonetheless, I will just use the standard one.

Building Code

Yes its successful! But the terminal tab UI is on the right because this is the first time I setup in WSL. I prefer the terminal tab on the left, so I moved it: right-click the terminal tab → Move Tabs Left.

Move Terminal Tabs

Next up would be the task.json file generated from the built. It seems like its still the same as the documentation at time of writing.

Task Json

Ok, maybe now we can try breaking the code with some syntax error and see if compiler will complain? This is what we call “itchy hand”, here in Singapore. We can see that once we remove just one ‘;’, vscode already start hinting that there is an issue with different signs — just like how your other half suddenly doesn’t reply your questions, or starts giving you the black face, deep down you know you screwed something up but not sure what is it. Then you proceed to trigger your partner sorry i meant trigger the build. This is what you can see.

Angry Gif

Sorry I meant this.

Erro and warning

Simply Ctrl + left click will bring us to the error with clear error message, something I wish python could do better.

Debugging

Next, I will try to debug as per suggested by the document.

Debugging

One thing to note is that register section which lets us see what value is stored where in processor. I think this could be useful when I manage to learn optimization or should I need to do assembly debugging. For now, let me just try not to get overwhelmed.

Install Git

Last but not least, install Git inside WSL so I can push my practices into my GitHub account. One important to note according to the Get started using Git on Windows Subsystem for Linux documentation, is that if we install many version of Linux then for each of the distributions be it Ubuntu or Debian for example, we will need Git installed separately in each one. So if I open up my Ubuntu command and go to the home directory with cd ~ and type in explorer.exe . , the file explorer will open and show the windows path to access home folder.

Explorer in wsl

SSH

I’ll use Git with SSH. We can follow this Generating a new SSH key.

After we have created the new ssh key, we can run the following command to see our folder.

Terminal window
la ~/.ssh

Note on alias on many Linux Distribution like Ubuntu, when we start a shell, some alias are pre-defined, and we can run alias to see them. la here is just ls -a ie show all files including .ssh or any . dotfiles.

Alias Commands

Then, we can copy the contents of public key generated and go to GitHub account > settings > SSH and GPG keys > New SSH key

If we connect with an ed25519 algorithm key, SSH might show something like:

Terminal window
The authenticity of host 'github.com (140.82.xx.xx)' can't be established.
ED25519 key fingerprint is SHA256:+DiYpU+…something…

Check the known_hosts keys

We can check GitHub’s page: if their official ED25519 fingerprint is the same, we know it’s safe to accept and add to known_hosts. This is to ensure we are connecting to, in fact, GitHub, not random servers that look like GitHub.

We can totally just replace the ssh key entries like the documentation suggest. Or we can compare the 3 to see if they are the same.

Known Host keys

Our known_hosts could be using hashed hostnames so it may not start with github.com as per the documentation. Check the file by showing the file content with cat.

Terminal window
cat ~/.ssh/known_hosts

Then, we can test the SSH connection to GitHub.

Terminal window
$ ssh -T git@github.com

Now we can create our GitHub repo and do our first commit. According to the GitHub repo workflow, it suggests the following steps.

Terminal window
…or create a new repository on the command line
echo "# cpp_practice" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:hyunpangvictor/cpp_practice.git
git push -u origin main

In order to improve my CLI skills, I will try not to touch the VS Code git tab and try to use as much bash as possible.

Git Tab in VS Code

Init the Git with git init

Update the git config, I want to practice using vim, so I will be changing the default to vim or we can simply edit in the vscode with git config — global core.editor “code — wait”. Else, it will be defaulted to nano.

Terminal window
git config --global user.name "<Your Name>"
git config --global user.email <Your Email>@gmail.com
git config --global core.editor "vim"

git add . to add all the files

git commit without message to trigger the editor

Simple vim command to add commit message

  • press i to start writing
  • press esc to go back to navigation
  • type :wq to save and quit

Yeah finally done setting up my WSL and the VS Code for practicing. Hope I will keep finding time to work on this.