Using VS2019 with WSL (Windows Subsystem for Linux)

John Emmas 161 Reputation points
2020-08-20T15:48:35.387+00:00

Hi there - I tried to post a question about Visual Studio and got redirected here (so hopefully it's the right place !! )

I'm trying to install WSL which is a new-ish technology for allowing Visual Studio to build Linux apps. I followed these instructions

for installing WSL2 and Debian Linux - and I then ran the stated bash command - sudo apt install g++ gdb make rsync zip. Everything seemed to install okay and Debian itself is running fine - but it hasn't made any difference when I launch VS2019.

VS2019 stubbornly refuses to compile for Linux (I've tried both CMake and MS-Build).

I've checked and double-checked and triple-checkde and I'm pretty sure I've followed the instructions correctly. However... I'm only using the Community Edition of rVS2019. Is this maybe something which only works in the paid-for versions ?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,171 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,753 questions
0 comments No comments
{count} vote

Accepted answer
  1. Dylan Zhu-MSFT 6,421 Reputation points
    2020-08-21T10:21:33.987+00:00

    Hi JohnEmmas-0498,

    I follow this instructions with visual studio community 2019 and Debian. There are also some compilation errors on my side, maybe you could try these steps which can work for me:

    Cmake project:

    • Install Ninja on debian, the command: apt-get install ninja-build

    Linux console application:

    • In the project property page, select GCC for Windows Subsystem for Linux instead of GCC for Remote Linux
      19369-annotation-2020-08-21-181438.jpg

    After that, the application can compile and debug normally.

    19427-annotation-2020-08-21-1725716.jpg

    19428-annotation-2020-08-21-175716.jpg

    If these steps doesn't work, could you please provide some details about your compilation error?

    [Update]

    The configuration of msbuild project is different from cmake.

    From first document, you don't need to add connection in Connection Manager. You just need to right-click project in solution explorer, then switch the platform Toolset to GCC for Windows Subsystem for Linux.

    And according to your latest updates, you could follow this document:Configure a Linux MSBuild C++ project in Visual Studio to configure your linux msbuild project, and then build, debug, run it on remote linux system.

    I'm using the name and port number shown in the instructions and I've tried various different user names and passwords.

    You can try to use ip address instead of 'localhost'.

    [Edit...] A bit more info... I can't claim to understand this but I found a command online:- ssh -L1234:localhost:22 localhost and if I issue it from bash it gives me this error:- ssh: connect to host localhost port 22: Connection refused

    You can run these commands in linux system, which will enable ssh feature:

    sudo apt-get install openssh-client  
    sudo apt-get install nano  
    sudo service ssh restart  
    sudo service ssh status  
    ssh localhost  
    

    Now I just need to figure out where it puts the built files...

    You could check this directory in property page.
    sadasdadadasda

    Update:

    Intellisense is the only thing I still need to set up (for the Linux compilers). I did read something about it somewhere so I'll just need to find it again....

    Maybe this document is what you find: Improvements to Accuracy and Performance of Linux IntelliSense

    It doesn't seem possible yet to switch between the two platforms in just a single project.

    Yes, the linux project just works in linux system. If you want to remote debug normal msbuild project in remote windows system, you could read Remote Debugging a C++ Project in Visual Studio

    Best Regards,
    Dylan

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. John Emmas 161 Reputation points
    2020-08-23T15:20:49.12+00:00

    Sorry for the delay - this site didn't tell me there'd been a reply !!

    I followed your advice about installing ninja-build but it doesn't make a difference :-(

    Just to be clear - I'm not seeing any compilation issue. The problem here is that when I drop down the Platform Toolset list (in my project's properties) I don't see any Linux compiler options. It only shows the Windows compilers which were already present. I've tried a CMake project as well as MSbuild (console and non-console) but I simply don't see any Linux compiler options. Here's what I see for CMake:-

    19586-cmake-options.jpg

    Notice that the only compiler options are for Windows.

    BTW - how did your Visual Studio even know that WSL and Debian were installed? Did you have to tell it somehow (e.g. set up a path somewhere)? Or did it just happen automatically?


  2. John Emmas 161 Reputation points
    2020-08-24T13:03:34.167+00:00

    Hi DylanZhu-MSFT and yes, I installed Linux Development with C++ - the good news though, is that there's been some progress this morning...

    VS2019 seems to update itself every few weeks but I've no idea what it's been updating 'cos when I checked my installed version this morning, it was nearly a year old.

    So I updated manually to ver 16.7.2 and now I can suddenly see some WSL options for building a CMake project

    19884-new-cmake-options.jpg

    The only problem is that I'm not seeing those WSL options if I try an MS-Build project

    [Edit...] Ah, I might be getting a bit further here... although I can't yet see any Linux options (for MSBuild) if I create a new MSBuild project it seems to have a readme folder now - which contains some instructions for creating a Linux MSBuild project. It all looks a bit fearsome so I'll leave till tomorrow when I'll have a bit more time to read it !

    0 comments No comments

  3. John Emmas 161 Reputation points
    2020-08-25T06:54:52.857+00:00

    [Edit...] things have improved a bit since I first posted this (read on...)

    Hmmm... it's not going well. I followed the readme instructions. The first stage was to run sudo apt-get install openssh-server g++ gdb gdbserver which seemed t go okay. Next, I had to open the Connection Manager dialog:-

    20115-connection-manager.jpg

    and then try to add a new connection - but each time I try to add one it displays an error with the Host name and Port. I'm using the name and port number shown in the instructions and I've tried various different user names and passwords. Any ideas..?

    20126-connection-setup.jpg

    [Edit...] A bit more info... I can't claim to understand this but I found a command online:- ssh -L1234:localhost:22 localhost and if I issue it from bash it gives me this error:- ssh: connect to host localhost port 22: Connection refused

    [Edit2...] and FWIW I issued the same command from a Windows Powershell and it also gives the same error.

    [Edit3...] I'm still not quite sure what I'm doing but I found a bash command called sudo /etc/init.d/ssh start which then allowed me to add a host called localhost and that seems to have fixed everything. I've no idea if it'll survive a reboot but cross-platform compilation with MSBuild is currently working. Now I just need to figure out where it puts the built files... :-(


  4. John Emmas 161 Reputation points
    2020-08-26T11:26:01.807+00:00

    Many thanks Dylan, you've been fantastically helpful !

    Intellisense is the only thing I still need to set up (for the Linux compilers). I did read something about it somewhere so I'll just need to find it again....

    One last question.... all the MSBuild projects I've seen (so far) contain either a selection of Windows compilers or a selection of Linux compilers. It doesn't seem possible yet to switch between the two platforms in just a single project. Am I right about that or do I just need to look a bit harder?

    [Oops - one more question...] Yesterday when I was running a Linux console app in the Debugger, the VS workspace showed me a small console window (showing the app's ourput). But I'm not seeing that any more. Is there a way to get it back?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.