Linux Linux remote development remote development

Linux Remote Development

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

Starting from the visual studio 2017 Microsoft launched a cross-platform development function, you can edit the code vs in vs, followed by cross-platform compiling and remote debugging, originally we need to manually complete the work was automated, significantly reduce our burden . Which supported platforms including Android and Linux, which is what we want to focus on the protagonist of today.

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.

Linux remote development carried out using vs2019

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:

这里我们选择了c++17。其他设置与在Windows上进行开发时一样,vs可以自动转换成g++的参数,这里就不再赘述。

添加远程环境

有了远程环境我们才能同步头文件或者进行调试运行。

在第一次编译或调试你的项目时vs会自动让你连接远程环境,当然,我们推荐在调试->选项->跨平台->连接管理器中进行设置:

填入你的远程ip/域名,端口ssh默认为22,安全起见你需要修改成其他端口,这里方便演示使用了默认配置,密码同上,你应该考虑使用更安全的ssh私钥登录。

登录成功后这个连接就添加完成了,我们看到管理器下面还有一个远程标头管理器的设置项,这就是用来同步头文件的:

点击更新按钮就会开始同步头文件,这些文件会被缓存在本地,因为要从远程一次性复制大量文件,所以可能会花费较长的时间。

这样远程环境就添加好了,可以开始写代码了。

本地编写和远程调试

至此你已经可以在vs中编写面向Linux平台的代码了,自动补全可以正常工作:

可以看到Linux中的头文件和结构体都已经可以识别了。如果你发现无法自动补全(通常发生在刚添加远程连接或是项目设置发生了变化后),先试试关闭vs重新打开,如果没用请尝试刷新intellisense或重新同步头文件。

在编辑结束后我们就能点击调试按钮运行我们的程序了:

注意,构建的体系架构必须是和远程环境一致的,比如远程环境是x64,这里可以选择x64或x86,但是不能选择arm,否则会报错。

这是测试代码,它将输出当前Linux系统内核的版本:

#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; }

点击调试->Linux 控制台,会显示一个可以交互的console,你可以在其中输入内容或是看到程序的输出:

程序运行成功。

避免踩坑

远程编译顺利完成后,我们就可以接着利用vs debugger设置断点,在断点处查看变量,甚至对运行中的Linux进行动态性能分析了。

不过在此之前,还有一些坑需要提前踩掉。

中文乱码

编码问题带来的麻烦永远会被放在第一位,毕竟当人们看到预想的输出实际上是一堆乱码时总会不可避免得紧张起来。

众所周知,编码问题一直是老大难,特别是Windows上中文环境通常是GB18030或GBK,而Linux上统一为utf8时。

下面看个实际例子,通常我们的程序里只包含ASCII字符的话不容易产生问题,所以我们加上一点中文字符:

#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; }

对于上面的测试程序,我们添加了一点中文输出信息,现在打开控制台进行调试:

可以看到中文输出变成了乱码,我们输入一些信息进去,这是运行结果:

可以看到,程序内写入的中文发生了乱码,而我们的输入没有。原因很简单,输入时实在linux的控制台环境下,编码默认是utf8的,所以我们的输入被正确编码,而源文件中的内容是GB18030的,所以在Linux控制台(默认以utf8解码数据并显示)中会发生乱码。

错误的原因知道了解决起来也就很简单了,把源文件的编码改成utf8就行,我们选择最简单的方法,在高级保存选项中修改编码(这个菜单选项默认被隐藏,网上有很多介绍如何显示它的方法的资料):

设置好后保存文件,现在文件的编码已经被改为了utf8了。

现在运行修改后的程序:

运行结果也是正常的:

使用数学函数和第三方库

在Linux上使用标准库提供的数学函数也是一个老生常谈的问题,根据你使用cpp还是c会有如下几个情况:

  1. 使用cpp时,libstdc++依赖于libm,所以使用g++编译你的程序时会自动链接数学函数库;
  2. 使用c时,如果是sqrt(4)这样的形式,较新的gcc提供了替换措施,不需要显示链接libm;
  3. 接上一条,如果你的参数是个变量,那么编译器可能会选择需要你链接libm。

通常在Windows上我们无需操心这点,但在Linux上使用c语言时就很难忽略这个问题了。

因此保险起见,如果你正在编写一个使用了数学函数的c程序,那么总是指定连接libm是没错的。(具体可以参考这里

另外当你使用例如boost这类第三方库时,也需要注意。在Windows上我们通常指定好附加包含目录和附加库目录即可正常编译,但是Linux上必须明确指定链接库的名字,因此我们在项目属性中进行设置。

在Linux上我们可以使用pkg-config来减轻上述的重复劳动,而在vs中我们不能直接利用这一工具,当你的项目使用了大量第三方库时就会成为不小的麻烦,如果想要解决这一问题,可以参考后续文章里我会介绍的vs+cmake构建项目。

下面我们给例子加上一点boost chrono的功能测试,在Linux上需要指定-lboost_chrono,这是设置:

下面是完整的代码:

#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"; }

点击运行按钮,程序就能正常调试了,否则会报错:

Guess you like

Origin www.cnblogs.com/Leo_wl/p/10994230.html