Author: Doug, 10+ year veteran of embedded development.

Public number: [IOT town], focusing on: C/C++, Linux operating system, application design, Internet of Things, SCM and embedded development and other fields. Public account reply [books], get Linux, embedded field classic books.

Transfer: welcome to reprint the article, reprint need to indicate the source.

[TOC]

As an embedded software development engineer, it is inevitable to write code, compile and debug in Linux system.

But very few students will use a real Linux machine as their workbench, unless it is compiled using a remote server.

A more common scenario is to install the Linux OS on a Windows/Mac VM.

Virtual machines commonly used in Windows systems include: Virtualbox, VMWare, Parallels Desktop is generally used in Mac systems, which is really fast!

A long time ago, the project I was involved in was mainly about Internet of Things gateway. I only needed to compile executable programs under Linux system, so in daily work, I typed code (editor: VSCode, SubLime) directly in Ubuntu VIRTUAL machine, compiled and debugging.

Later, when cross-platform applications were needed, the development model changed to:

  1. Knock code: Visual Studio in Indows system;

  2. Compiling Windows applications: Compile directly with the VC compiler in Visual Studio;

  3. Compiling Linux applications: Synchronize the code to the Ubuntu VIRTUAL machine through remote deployment, and compile and debug it remotely.

This development mode is relatively common, but each time the Ubuntu VIRTUAL machine consumes resources and starts slowly.

It is also convenient to write cross-platform code and use the CMake tool to manage the build process.

I have written two related articles before:

“Building cross-platform Application Frameworks with Cmake: C version”

Cross-platform application frameworks with cmake: C++ version

These two summaries can be regarded as a simple project template.

If I need to write a simple Demo and send it to someone else, I’ll just copy it and add the Demo code.

All this is to describe the working scenario, or development model, of cross-platform development.

There is another way to install Linux on Windows, WSL/WSL2!

Below, I will put their own installation, configuration process to share with you!

What is WSL?

There’s nothing to be said for this part, but here’s an excerpt from the Microsoft website:

WSL(Windows Subsystem for Linux) : A Subsystem of the Windows system on which the Linux operating system can be run.

Allows developers to run the GNU/Linux environment (including most command-line tools, utilities, and applications) as-is directly on Windows without the overhead of traditional virtual machines or dual-boot setup.

What is WSL2?

WSL2 is a new version of the Windows subsystem architecture for Linux, which is a major overhaul of the underlying architecture.

It uses virtualization technology and the Linux kernel to implement its new capabilities, with the primary goal of improving file system performance and adding full system call compatibility.

Personal Understanding:

WSL: Not really a Linux operating system, just an adaptation layer between Linux applications and Windows operating systems.

On top of this adaptation layer, Linux applications can be run, somewhat similar to the way cygwin used to be.

WSL2: It is a virtual machine, similar to Vitual Box, running a full Linux operating system on top of this virtual machine.

Compared with Virtual Box and VMWare, WSL2 provides more comprehensive compatibility, better interoperability with Windows, faster running speed and less system resources.

Activate the WSL service

Press Win+X to start Windows PowerShell (Administrator).

Or simply type Power into the search window and select Run as administrator:

PowerShell Windows are:

You can think of PowerShell as an upgraded, more powerful version of CMD.

In the PowerShell window, enter the following command to activate the WSL service:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Copy the code

You need to restart the system (needless to say, you must select Y) :

After the computer restarts, press Win + R to bring up the command input window. Enter the command appwiz.cpl.

Click [Enable or disable Windows features] on the left:

The following window pops up:

You can see that the [Windows Subsystem for Linux] column has been checked, indicating that the WSL service has been started.

The next thing to do is install the Ubuntu operating system.

Install ubuntu-18.04

Start Windows Store:

In the search bar at the top right, type: Ubuntu

Let’s go with Ubuntu-18.04, which is the version I use at work.

The download speed is pretty fast:

After the installation is complete, click the “Start” button:

The first time the opening speed is slightly slower, about 1 minute or so, prompting to set the user name, password, and then enter the window we are familiar with:

And now ubuntu-18.04 is installed!

I usually set the password of the root user after the system is installed. I will always need to log in to the system as root.

$ sudo passwd
Copy the code

After the system is installed, the first thing is usually to change the software source.

You can choose the source of Ali, Tsinghua or UNIVERSITY of Science and Technology of China. Here, tsinghua software source is directly used:

$ cd /etc/apt
$ sudo cp sources.list source.list.bak
$ sudo vim sources.list
Copy the code

Copy and paste the following software source:

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
Copy the code

After saving, execute the update instruction:

$sudo apt-get upgrade $sudo apt-get upgrade $sudo apt-get upgrade When prompted for input, select Y.Copy the code

Remote login over SSH

If you find the black window a bit ugly, you can log in remotely (SSH) using the Xshell tool in Windows.

For this purpose, we need to reinstall the SSHD service in Ubuntu-18.04:

$ sudo apt purge openssh-server
$ sudo apt install openssh-server
Copy the code

After the installation is complete, you need to modify the configuration file to run the remote login:

$ sudo vim  /etc/ssh/sshd_config
Copy the code

Change 1: Change #Port 22 in line 13 to Port 22.

Change 2: Change #PermitRootLogin prohibit-password in line 32 to PermitRootLogin yes.

Change 3: Change #PasswordAuthentication yes to PasswordAuthentication yes in line 56.

After modification, start SSHD service:

$ sudo service ssh restart
Copy the code

To log in remotely, start Xshell in Windows. The host name is localhost.

When creating a link, you need to save the secret key:

Then enter your username and password:

If the following prompt appears, ignore it and simply select [no], which is related to Linux graphical Windows, as explained later.

At this point, you can happily operate the Ubuntu command line window in Xshell.

Does it feel like something’s missing? Where are the Desktop, Documents, Pictures, etc default folders?

This problem, later in the demo WSL2 will see!

Compile the Hello, World!

Finally, let’s compile an application. Install GCC compiler first:

$ sudo apt-get install gcc
Copy the code

Write another hello.c file:

#include <stdio.h>

int main()
{
        printf("Hello,World! \n");
        return 0;
}
Copy the code

Then compile and execute:

ccc@DESKTOP-5LT2QM5:~/tmp$ gcc hello.c -o hello
ccc@DESKTOP-5LT2QM5:~/tmp$ ./hello
Hello,World!
Copy the code

Perfect!





Next is the introduction of WSL2 and graphics window, I have verified the process, and I will share it with you after sorting it out, thank you!

Recommended reading

[1] C language pointer – from the underlying principle to the tricks, with graphics and code to help you explain thoroughly [2] step by step analysis – how to use C to achieve object-oriented programming [3] The original GDB underlying debugging principle is so simple [4] inline assembly is terrible? Finish this article and end it!

Other series albums: selected articles, C language, Linux operating system, application design, Internet of Things