We have introduced a new capability to run remote tasks in Visual Studio 2017 15.5 Preview 2. This capability allows you to run any command on a remote system that is defined in Visual Studio’s Connection Manager. Remote tasks also provide the capability to copy files to the remote system. This feature is added when you install the Linux development with C++ workload. If you want to learn more about the Open Folder capability in general read https://aka.ms/openfolder/cpp.
To get started with this create a file main.cpp in a folder on your PC, add the source below to it and open the folder in VS.
#include int main() { std::cout << "Hello" << std::endl; }
To get Linux IntelliSense for this file go to the menu Project > Edit Settings > CppProperties.json. Modify the file so it just has one entry like the below.
{ "configurations": [ { "inheritEnvironments": [ "linux_x64" ], "name": "Linux-x64", "includePath": [ "${env.INCLUDE}" ], "defines": [ ] } ] }
In the Solution Explorer right click main.cpp and choose Configure Tasks. In the tasks.vs.json file that opens modify the task as follows. This task will copy the directory to the remote machine specified, then run “g++ main.cpp” in the directory specified.
{ "version": "0.2.1", "tasks": [ { "taskName": "Build", "appliesTo": "main.cpp", "type": "remote", "contextType": "build", "command": "g++ main.cpp", "remoteMachineName": "ubuntu", "remoteCopyDirectory": "~/sample", "remoteCopyMethod": "sftp", "remoteWorkingDirectory": "~/sample/hello", "remoteCopySourcesOutputVerbosity": "Verbose" } ] }
You can run this task by right clicking main.cpp in the Solution Explorer and selecting Build. The Output window will show the file copy results. There is no output from this compile command, unless you have an error.
The field type is where we can change the context of the type to run on the remote machine, command is what will be executed there. The context type build places this command into that area of the context menu for the file the task applies to. The field remoteMachineName needs to match a defined connection in the Visual Studio Connection Manager. You can find that in the Quick Launch (Ctrl+Q) by typing Connection Manager, or under Tools > Options > Cross Platform > Connection Manager. Note that you can specify different directories for where to copy files vs where to run commands with the fields remoteCopyDirectoy and remoteWorkingDirectory. When the files are copied they are copied into a folder of the same name as the folder you have open in Visual Studio at the location specified. By default commands are executed in your home directory on the remote system. You can also specify the method you want to use for copying, sftp, rsync or none to disable. The rsync method is recommended for large projects. The remoteCopySourcesOutputVerbosity option is not necessary but is useful to know about if you are trying to diagnose an issue.
You can do anything you like with remote tasks of course. To run the output of our first command above, add another task after the one above.
{ "taskName": "Run", "appliesTo": "main.cpp", "type": "remote", "command": "./a.out", "remoteMachineName": "localhost", "remoteWorkingDirectory": "~/sample/hello", "remoteCopySourcesOutputVerbosity": "Verbose" }
This task will show up as “Run” at the bottom of the context menu when you select main.cpp in the Solution Explorer. When executed, after you have compiled, you should see “Hello” in the output window.
What’s next
Download the Visual Studio 2017 Preview, install the Linux C++ Workload, and give it a try with your projects.
This is a very simple example of this new feature. We look forward to hearing about how you are using it in your projects.
The best way to reach us is via our GitHub hosted issue list, directly via mail at vcpplinux-support@microsoft.com or find me on Twitter @robotdad.