Easy way to setup VSCode to learn Linux programming
Tags: vscode, linux, tutorial, programming
Published: 2024-04-13 02:37:09

## As an embedded engineer, I often need to practice Linux programming on my Windows laptop. I believe that installing a distro directly into the system is superfluous and somewhat complex. 
I have since discovered WSL, which is more practical. Thus, I make a few notes about how to configure the system to run and build code. 
This is for some one who knows how to do it but struggles to make it work
**1. Use virtual machine**
Firstly, I remind everyone about the old-school method with VM
You need Virtual machine with an Ubuntu distro. I choose Kubuntu because I think it is less prone to error than Ubuntu ( just my personal experience). 

Install ssh to Ubuntu then enable it
The most important part is setting up Network in VM

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7htscxfwkj33p2euhpol.png)
The guess IP is the IP in Ubuntu you get from the ifconfig command. 
You can test the connection in command window

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilr3hlj9zmi5apudlzbp.png)

Now open VSCode or Visual studio, install SSH extension and create new one, use the same command above and you are good to write code. 

**2. Use WSL**

WSL stands for Windows Subsystem for Linux. In short, it works like below

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idid4c6ydt5anq5juv36.png)

[This](https://learn.microsoft.com/en-us/windows/wsl/setup/environment) is the best tutorial on how to install WSL and connect it with VSCode. Just follow it. 
Now you can access a Linux system like using VM, we come to the most important part, compile and run/debug source code. 
You need to create a Hello world cpp file to test. 
We need tasks.json file to compile the code in Linux and a launch.json file to debug. 
tasks.json
```
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: g++ build active file",
			"command": "/usr/bin/g++",
			"args": [
				"-fdiagnostics-color=always",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build",
			"detail": "compiler: /usr/bin/g++"
		}
	]
}
```
  
Install gdb to Linux using command 

```
sudo apt install gdb
```
Now check where gdb is

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/atxkfohlvk9v7f9x492w.png)

Config launch.json file and update miDebuggerPath with the path you get

```
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "MIMode": "gdb",
            "cwd": "${fileDirname}",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file"
        }
    ]
}
```
To compile code, use Ctrl Shift B

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjme57r7vdroofkax1mx.png)
To debug, use Run -> Start Debugging

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/co0hvws6x7ldsjd39y5i.png)
Everything should be ok now. Enjoy!