Use Visual Studio 2019 remote development of Linux

Generally, there are two options when we develop Linux applications:

  1. Written on Linux and run the program directly test and debug
  2. Remote develop on Windows or Mac OS X using tools

Although I was directly developed on Linux environment, but there are many people who are engaged in development work in the Windows environment, if they are familiar with the system to leave the strange environment might affect the work efficiency.

So today we'll look at how to use Visual Studio 2019 on Windows for the Linux remote development and how to avoid common pitfalls.

Citation

  • Cross-Platform Visual Studio development Introduction
  • Linux remote development carried out using vs2019
    • Create a project
    • Configuring remote project
    • Add Remote Environment
    • Write local and remote debugging
  • Avoid stepping pit
    • Chinese garbled
    • Use mathematical functions and third-party libraries

Cross-Platform Visual Studio development Introduction

You might wonder, vs what is the development of remote, though you do not understand this knowledge can be developed, but I would like to spend two minutes to make a short explanation.

vs Remote development is divided into two steps:

  1. Create a connection to the remote environment, and then let the system header files vs remote environment synchronized to the local (can also specify other parts of the header files, will explain later), c ++ code completion requires only the first file.
  2. When the code is written, select the appropriate remote environment, vs copy the code and object files to a specified location remote environment and then compiled according to your configuration.
  3. Subsequently vs will run your program in the gdb console or in gdbserver, during which you can fully enjoy the efficiency and convenience vs debugger brings.

After the above steps you can debug their own cross-platform programs written in vs inside.

Use Visual Studio 2019 remote development of Linux

Introduction to this end, let's take a look at the graphic tutorials were developed Linux in vs2019. Before we begin, first of all point to do the preparatory work:

  1. Installed vs2019, and check the c ++ for Linux functions;
  2. Prepare a usable Linux remote environment, such as configuring static IP Linux virtual machine, and have installed the GCC tool chain and openssh.

When you are ready we ought to get to the the.

Create a project

After installing c ++ for Linux Linux features we'll see the option to create a new project in the panel, as shown:

Here we have chosen to use traditional vs project to solve a blank console application program constructed subsequent articles you can also see how to create cmake project, put aside here.

Here nothing to say, the storage location select an item, pay attention to the local location, location of the remote machine in the back will be configured:

Click to create our remote development project will create success.

Configuring remote project

vs can not edit the configuration space project, so we created a first in the project main.cpp, then click on the top menu: Project -> Properties, you can see the configuration interface of the project:

The remote computer is in a remote debugging connection manager added. Here generally do not need change, unless you need to change the type of project or compile the results of the stowed position. If you have multiple remote environment, where you can make a selection.

Section provides debugging gdband gdbserverthe former is let vs start a console on Linux, then in which to run gdb and return output, if the terminal on your Linux configuration color output, and regret vs not know them, it will be displayed as the original string; gdbserver will enable the remote use gdbserver, local vs resolve backhaul data will not murmur. Here we have chosen gdbserver, if you found it impossible to break point, then refer to Microsoft's recommendations, in exchange for gdb program:

Next is key configuration, first header files need to be synchronized remote configuration environment, with these files vs to your code auto-completion and tips:

Copy the default path is usually already contains most of the header files on Linux, we usually do not need to be changed. Header file synchronization occurs after the first building after the success of the project or add a remote connection manually synchronize.

Then choose c / c ++ compiler, which is configured to gcc and g ++ compiler argument to explain these parameters is beyond the scope of our discussion, we only need to select the appropriate ++ standard version c:

Here we have chosen c ++ 17. Other settings when developing on Windows, like, vs parameters can be automatically converted to g ++, and not repeat them here.

Add Remote Environment

With remote environment we can sync header files for debugging or running.

The first time you compile or debug a project vs will automatically allow you to connect to a remote environment, of course, we recommend Debug -> Options -> Cross-platform -> set Connection Manager:

Fill in your remote ip / domain name, ssh port 22 by default, be safe you need to change it to another port, here demonstrate the convenience of using the default configuration, password above, you should consider using a private key more secure ssh login.

After successful login, the connection is complete addition, we see the settings Manager header followed by a remote manager, which is used to synchronize the header file:

Click the Update button will start the synchronization header files, these files will be cached locally, because a large number of files copied from a remote one time, it may take a long time.

Such a remote environment to add OK, you can start writing code.

Write local and remote debugging

At this point you can already write the code for the Linux platform in vs in, and auto-complete works correctly:

You can see the header files and structure in Linux are already identified. If you can not find auto-completion (usually occurs after just added a remote connection or project settings have changed), first try to re-open vs closed, please try to refresh intellisense if useless or resynchronize the header file.

At the end we will be able to click on the Edit button to run debugging our program of:

Note that the construction of the architecture must be consistent and remote environments, such as remote x64 environment, where you can choose x64 or x86, but can not choose the arm, otherwise it will error.

This is a test code, it will output the current version of the Linux kernel:

#include <sys/utsname.h>
#include <iostream>
#include <cstdio>

int main()
{
    auto start = chrono::high_resolution_clock::now();
    utsname names;
    if (uname(&names) != 0) {
        std::perror("cannot get unames");
    }

    std::cout << "Linux kernel version: " << names.release << std::endl;
}

Click Debug -> Linux Console, displays that can interact with console, you can enter the content or see the output of the program:

The program runs successfully.

Avoid stepping pit

After successful completion of the remote compile, we can then use vs debugger to set breakpoints, watch variables at the breakpoint, even running Linux dynamic performance analyzes.

But in the meantime, there are some pits stepped out in advance.

Chinese garbled

The trouble with coding problems will always be in the first place, after all, when people see the expected output is actually a bunch of gibberish will always inevitably get nervous.

As we all know, has been a chronic problem of coding, especially in the Chinese Windows environment is often GB18030 or GBK, and unity is the Linux utf8.

Look at a practical example below, we usually program contains only ASCII characters, then the problem is not easy to produce, so we add a little Chinese characters:

#include <sys/utsname.h>
#include <iostream>
#include <cstdio>
#include <string>

int main()
{
    utsname names;
    if (uname(&names) != 0) {
        std::perror("cannot get unames");
    }

    std::cout << "Linux kernel version: " << names.release << std::endl;
    std::cout << "输入内容:";
    std::string input;
    std::cin >> input;
    std::cout << "你输入了:" << input << std::endl;
}

For the above test procedures, we add a bit of Chinese output, now open the console for debugging:

You can see the Chinese become garbled output, we enter some information into it, which are the results:

It can be seen in the Chinese written procedure took place garbled, and our input is not. The reason is simple, at the time it entered the linux console environment, the default encoding is utf8, so our input is properly coded, and the contents of the source file is GB18030, so in Linux console (default to utf8 decode the data and displays) will occur garbled.

Know the cause of the error to solve it is very simple, the encoding of the source file is changed utf8 on the line, we choose the easiest way, the 高级保存选项modified encoding (this menu option is hidden by default, there are many online explains how to display it data method):

After setting the saved file is now encoded file has been changed to a utf8.

Now run the program after the modification:

Operating results is normal:

Use mathematical functions and third-party libraries

Use standard mathematical functions provided by the library on Linux is also a nagging question, based on your use cpp or c there are several situations:

  1. When using cpp, libstdc ++ depend on libm, so use will automatically link the math library when g ++ to compile your program;
  2. When using c, if it is sqrt(4)such a form, the newer gcc offers a replacement measures do not need to show a link libm;
  3. Connect one, if your argument is a variable, then the compiler may choose to require you to link libm.

Therefore the safe side, if you are writing a program using the c mathematical functions, then specify the connection libm is always correct. (Specific reference here)

In addition, when you use this type of third-party libraries such as boost, also need attention. On Windows Additional good we usually specify the directory containing additional library directory and can be compiled normally, but on Linux must explicitly specify the name of the DLL, so we set in the project properties.

On Linux we can use pkg-config to mitigate the above-mentioned duplication of effort, and in vs, we can not directly use this tool, when your project uses a large number of third-party libraries will become a little trouble, if you want to to solve this problem, you can refer to the follow-up article, I will introduce vs + cmake to build the project.

Here we give an example with a little boost chrono function test, you need to specify on Linux -lboost_chrono, which is set up:

Here is the complete code:

#include <sys/utsname.h>
#include <iostream>
#include <cstdio>
#include <string>
#include <boost/chrono.hpp>

int main()
{
    namespace chrono = boost::chrono;
    auto start = chrono::high_resolution_clock::now();
    utsname names;
    if (uname(&names) != 0) {
        std::perror("cannot get unames");
    }

    std::cout << "Linux kernel version: " << names.release << std::endl;
    std::cout << "输入内容:";
    std::string input;
    std::cin >> input;
    std::cout << "你输入了:" << input << std::endl;
    auto counter = chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start);
    std::cout << "程序运行了:" << counter.count() << "ms\n";
}

Click the Run button, the program will be able to properly debugged, otherwise it will error:

Guess you like

Origin www.linuxidc.com/Linux/2019-09/160471.htm