使用“ Linux的Windows子系统”从Visual Studio编写和调试Linux C ++应用程序

I've blogged about the "Windows Subsystem for Linux" (also known as "Bash on Ubuntu on Windows") many times before. Response to this Windows feature has been a little funny because folks try to:

我以前写过很多关于“ Linux的Windows子系统”(也称为“ Windows上的Ubuntu上的Bash ”)的博客。 对Windows功能的React有点有趣,因为人们试图:

  • Minimize it - "Oh, it's just Cygwin." (It's actually not, it's the actual Ubuntu elf binaries running on a layer that abstracts the Linux kernel.)

    最小化它-“哦,这只是Cygwin。” (实际上不是,而是在抽象Linux内核的层上运行的实际Ubuntu elf二进制文件。)
  • Design it - "So it's a docker container? A VM?" (Again, it's a whole subsystem. It does WAY more than you'd think, and it's FASTer than a VM.)

    设计它-“所以它是一个Docker容器?一个VM?” (同样,它是一个完整的子系统。它的功能超出了您的想象,并且比VM更快。)

Here's a simple explanation from Andrew Pardoe:

这是来自Andrew Pardoe的简单解释:

1. The developer/user uses a bash shell.2. The bash shell runs on an install of Ubuntu3. The Ubuntu install runs on a Windows subsystem. This subsystem is designed to support Linux.

1.开发人员/用户使用bash shell 2。 bash shell在Ubuntu3的安装上运行。 Ubuntu安装在Windows子系统上运行。 该子系统旨在支持Linux。

It's pretty cool. WSL has, frankly, kept me running Windows because I can run cmd, powershell, OR bash (or zsh or Fish). You can run vim, emacs, tmux, and run Javascript/node.js, Ruby, Python, C/C++, C# & F#, Rust, Go, and more. You can also now run sshd, MySQL, Apache, lighttpd as long as you know that when you close your last console the background services will shut down. Bash on Windows is for developers, not background server apps. And of course, you apt-get your way to glory.

它太酷了。 坦率地说,WSL让我运行Windows,因为我可以运行cmd,powershell或bash(或zshFish )。 您可以运行vim,emacs,tmux,并运行Javascript / node.js,Ruby,Python,C / C ++,C#和F#,Rust,Go等。 现在您还可以运行sshd,MySQL,Apache,lighttpd,只要您知道关闭最后一个控制台时,后台服务就会关闭。 Windows上的Bash适用于开发人员,而不适用于后台服务器应用程序。 当然,您很容易获得荣耀。

Bash on Windows runs Ubuntu user-mode binaries provided by Canonical. This means the command-line utilities are the same as those that run within a native Ubuntu environment.

Windows上的Bash运行Canonical提供的Ubuntu用户模式二进制文件。 这意味着命令行实用程序与在本地Ubuntu环境中运行的命令行实用程序相同。

I wanted to write a Linux Console app in C++ using Visual Studio in Windows. Why? Why not? I like VS.

我想使用Windows中的Visual Studio用C ++编写一个Linux控制台应用程序。 为什么? 为什么不? 我喜欢VS。

设置Visual Studio 2017以在Linux上编译和调试C ++应用 (Setting up Visual Studio 2017 to compile and debug C++ apps on Linux)

Then, from the bash shell make sure you have build-essential, gdb's server, and openssh's server:

然后,从bash shell确保您具有build-essential,gdb的服务器和openssh的服务器:

$ sudo apt update
$ sudo apt install -y build-essential
$ sudo apt install -y gdbserver
$ sudo apt install -y openssh-server

Then open up /etc/ssh/sshd_config with vi (or nano) like

然后使用vi(或nano)打开/ etc / ssh / sshd_config

sudo nano /etc/ssh/sshd_config

and for simplicity's sake, set PasswordAuthentication to yes. Remember that it's not as big a security issue as you'd think as the SSHD daemon closes when your last console does, and because WSL's subsystem has to play well with Windows, it's privy to the Windows Firewall and all its existing rules, plus we're talking localhost also.

为了简单起见,请将PasswordAuthentication设置为yes 。 请记住,当您的最后一个控制台关闭时,SSHD守护程序关闭时,安全问题并不像您想象的那么大,并且由于WSL的子系统必须在Windows上正常运行,因此Windows防火墙及其所有现有规则是私有的,此外我们也正在谈论本地主机。

Now generate SSH keys and manually start the service:

现在生成SSH密钥并手动启动该服务:

$ sudo ssh-keygen -A
$ sudo service ssh start

Create a Linux app in Visual Studio (or open a Makefile app):

在Visual Studio中创建Linux应用(或打开Makefile应用):

File | New Project | Cross Platform | Linux

Make sure you know your target (x64, x86, ARM):

确保您知道目标(x64,x86,ARM):

Remote GDB Debugger options

In Visual Studio's Cross Platform Connection Manager you can control your SSH connections (and set up ones with private keys, if you like.)

在Visual Studio的跨平台连接管理器中,您可以控制SSH连接(并根据需要设置私钥)。

Tools | Options | Cross Platfrom | Connection Manager

Boom. I'm writing C++ for Linux in Visual Studio on Windows...running, compiling and debugging on the local Linux Subsystem

繁荣。 我正在Windows的Visual Studio中为Linux编写C ++ ...在本地Linux子系统上运行,编译和调试

I'm writing C++ in Visual Studio on Windows talking to the local Linux Subsystem

BTW, for those of you, like me, who love your Raspberry Pi tiny Linux computers...this is a great way to write C++ for those little devices as well. There's even a Blink example in File | New Project to start.

顺便说一句,对于喜欢我的Raspberry Pi小型Linux计算机的你们这些人来说……这也是为这些小型设备编写C ++的好方法。 文件|甚至还有一个Blink示例。 新项目开始。

Also, for those of you who are very advanced, stop using Mingw-w64 and do cool stuff like compiling gcc 6.3 from source under WSL and having VS use that! I didn't realize that Visual Studio's C++ support lets you choose between a number of C++ compilers including both GCC and Clang.

另外,对于那些非常高级的人,请停止使用Mingw-w64并做一些很酷的事情,例如在WSL下从源代码编译gcc 6.3并让VS使用它! 我没有意识到Visual Studio的C ++支持可以让您在许多C ++编译器(包括GCC和Clang)之间进行选择

翻译自: https://www.hanselman.com/blog/writing-and-debugging-linux-c-applications-from-visual-studio-using-the-windows-subsystem-for-linux

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值