Cross-platform development of VS2019 C++ - Cmake project (2)

This article mainly supplements the creation of WSL (Linux subsystem) under Windows, as well as compiling and debugging CMake projects under WSL and remote Linux systems.

Create WSL under Windows

1. Right-click the Windows icon in the lower left corner = "Apps and Features =" Enable or disable Windows features

2. Select "Windows Subsystem for Linux, OK

3. After the installation is complete, a restart should be required.

4. Open the Windows application store, if it is not fixed here, you can search the Microsoft Store in the start menu

5. Search Linux

6. Select a system to install

The first Ubuntu app installs the 20.04 LTS version. The system version I will demonstrate below is also 20.04LTS.

Just click to install:

Preparation for WSL

After installation, the user name and password will be set, the user name does not matter, the password is required by sudo.

The official introduction of MS is here: https://devblogs.microsoft.com/cppblog/c-with-visual-studio-2019-and-windows-subsystem-for-linux-wsl/

First, install several software packages required for VS remote compilation and debugging, and run the command line in WSL:

 sudo apt install g++ gdb make rsync zip

Note: I also used the 18.04 system at the beginning, but when I installed g++, "xxx g++-5 unmet dependencies" failed. Then I tried 20.04 again, and the silky installation would be fine, so I used the 20.04 system directly. The g++ is installed here, and WSL-GCC-xxx should also be selected when selecting the configuration below.

The inability to install g++ on 18.04 turned out to be caused by my replacement of the apt source of the University of Science and Technology of China. Just follow this article and replace it with the Ali source: https://blog.csdn.net/gs80140/article/details/89541655

Some knowledge of WSL:

1. The root folder of WSL is in this location of the system:

C:\Users\LY\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs

CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc Find it yourself according to the Linux system you installed.

2. The file system of Windows is under the /mnt/ path of the WSL system. For example, the path of the test folder of the C drive in the WSL is "/mnt/c/test".

3. Avoid copying files to the WSL folder or modifying a certain file when the WSL system is running.

4. You can use a file editor to directly modify the Linux file of WSL under Windows, but you need to pay attention to the fact that the file may not be recognized and valid by Linux. The main reason is that the newline characters of the DOS file system and the Unix file system are different, '\r\n' and '\n'. If some scripts have this error "syntax error near unexpected token `$'\r'" that is The file system does not correspond to the problem. You can install dos2unix under WSL and use dos2unix a.file to complete the file system conversion.

Add WSL configuration in VS

The project is still the project in the previous article, just add a WSL configuration:

1. Enter the management configuration:

2. Click the + sign

3. Select WSL-GCC-Debug

This depends on whether G++ or Clang is installed on your WSL, refer to the preparation of WSL

4. Select the Ubuntu20.04 just installed

5. Select the configuration of WSL-GCC-Debug

After selecting the new configuration, the contents of the above two red boxes will generally appear: the first one is because the remote generation requires the Cmake version supported by the Linux system installed by the other party; the second is because each configuration has its own CMake cache, so it needs to be generated once.

6. First click "Yes" to install CMake, and after installation, click "Generate +" to generate CMake Cache.

First select "Yes", and after clicking, CMake of WSL will be automatically installed. as follows:

This installation task is in the background, in the lower left corner of VS:

There is a problem here, that is, VS on my computer failed to install CMake for WSL. It feels that the CMake package has not been passed on. Originally this article was intended to be issued before May 1st, but due to this problem, the latter part of the content was only completed today. .

Every time you click "Generate +", the console will still output "CMake 3.8.x or later is not available for remote systems." In my case, it is because VS cannot install CMake for WSL so it cannot generate CMake's Cache. So I decided to manually install the version of CMake 3.7.2, but the output of the console is still like this:
1> CMake generation has been started for the configuration "WSL-GCC-Debug".
1> Find the cmake executable on /usr/bin/cmake.
1> CMake version "3.7.2.0" identified on the remote
CMake 3.8.x or higher is not available on the remote system. An information bar will appear stating that CMake can be automatically deployed to the remote system. Select Yes to start the deployment, or No to ignore the message. See https://aka.ms/linuxcmakeconfig for details  .
CMake binary deployment to remote machine started. After the deployment is complete, the CMake build will continue automatically.

MS employees finally solved this problem for me today. The above is caused by a translation error. "CMake 3.8.x or later is not available for remote systems." This sentence actually means that the remote system does not find CMake 3.8. x or higher version, you need to install a higher version of CMake . And this Chinese translation seems not to install a higher version of CMake. So I manually installed the version of CMake 3.17.2, no problem. Although the automatic installation of VS The problem of failure has not been solved yet, but this will be handed over to MS employees to solve it. I will post the method of manually installing CMake here:

1. First use the two apt commands apt-get update and apt-cache madison cmake to query, which versions are supported by cmake:

2. If your system has a version higher than 3.8.x, install it directly:

sudo apt-get install cmake

3. If your system does not have a suitable cmake version, you can download and install it yourself:

First download the cmake installation script: wget https://github.com/Kitware/CMake/releases/download/v3.17.2/cmake-3.17.2-Linux-x86_64.sh

The version link is found here https://cmake.org/download/

After the download is complete, run the installation script. Either use the absolute path for sudo, or run .sh directly after chmod 777:

sudo sh ~/cmake-3.17.2-Linux-x86_64.sh

Space all the way, skip the license introduction, then enter y to accept the license. Finally, enter a y to confirm the installation path.

Finally, since this is a decompressed package installation, it is a green installation, so you need to create a shortcut under /usr/bin so that VS can find this CMake:

sudo ln -s ~/cmake-3.17.2-Linux-x86_64/bin/cmake /usr/bin/cmake


2021-1-5

Some readers responded that there is still a problem missing here. That is, after installing cmake, VS still cannot find it.

If you install it with apt, it is basically fine. But if you install it through .sh decompression and then link to /usr/bin/cmake through ln -s, you need to make the following changes in the project configuration:

Change the path of the CMake executable file to the full path in the advanced settings. 

I don't know the reason for this problem for the time being. I also felt that it was annoying to change it a while ago, so I asked Microsoft a question, and I haven't replied yet.


Then go back to VS, and the console output is normal operation:

Finally, direct debugging

The generated file path is here:

Note that this is different from the Linux system. It is not in the Linux system but under the Windows VS project.

Configuration of remote Linux system

As for the remote linux system, I will quickly take it over. If you don’t understand, you can refer to this article: Cross-platform development of VS2019 C++-Linux development .

First, just like WSL, you only need to add a new configuration, Linux-GCC-Debug:

Then configure the remote computer name:

The configuration method is introduced in the previous Linux article.

After configuration, switch the configuration and generate cmake cache:

You can also debug directly:

Different from WSL, the remote Linux system is to copy the code to the Linux system, and the generation is also in the remote Linux system.

The build path is here:

Probably:

/home/username/.vs/CMakeProject/01734532-db4d-47d9-a96b-577d471e9b96/out/build

 

Guess you like

Origin blog.csdn.net/luoyu510183/article/details/105847329