⬆️ ⬇️

Visual C ++ Extension for Linux Development

The Visual C ++ for Linux Development extension allows you to create C ++ solutions for servers, desktops, and devices running Linux. You can manage connections to them directly in Visual Studio. VS will automatically copy and remotely collect the source code of programs. Also, the environment allows you to run your application in debug mode. The project management system supports the creation of solutions for specific architectures, including APM. Under the cut, we will tell you how to start working with new projects for Linux.







So far, the extension only supports remote builds on the target Linux computer. There are no restrictions on the Linux distribution, but certain tools must be installed on the system. In particular, you will need openssh-server, g ++, gdb and gdbserver. Install them using any package manager. For example, on systems based on Debian, you can use the command:



sudo apt-get install openssh-server g++ gdb gdbserver 


Table of contents



  1. Installation
  2. Your first Linux project in VS
  3. Linux project properties
  4. Console window
  5. IoT projects for Linux
  6. How to work with Intel Edison board through VC ++ for Linux extension
  7. Classic applications
  8. Makefile Project Template
  9. Usage tips
  10. Detailed build log
  11. Where to get the included files
  12. Manage copying remote files
  13. Redefining the path to the C / C ++ compiler
  14. Build Events
  15. Debug Options
  16. useful links


Installation



Download the Visual C ++ for Linux Development extension or install it using the Visual Studio Extension Manager.

')

To get started, create a new project and select Templates> Visual C ++> Cross Platform> Linux .







Currently, three templates are available: Blink (for Internet of Things devices — for example, Raspberry Pi), Console Application (console application, almost empty frame for your code) and Empty (empty template for self-adding files with source code and settings).



Your first Linux project in VS



First, create a console application. Create a project based on this template and add a breakpoint on the printf line. Then press F5 or click the Remote GDB Debugger button. By default, the debug / x64 configuration will be selected for the console application. If the architecture of the remote target computer is x86 or ARM, then these settings will need to be changed. In this example, I will use the Ubuntu x64 virtual machine.







Since this is the first time we are building a solution for a Linux computer, the environment will ask for connection information. The corresponding form is opened when attempting to build a project.







Supplement supports authorization by password or certificate, including through password phrases with certificates. Successful connection information is saved for subsequent connections. To manage saved connections, select Tools → Options → Cross Platform → Linux . Yes, passwords and pass phrases are stored in encrypted form. In a future update, we are going to add connectivity without saving data about it.



After connecting, your source code files will be copied to a remote Linux computer, and the extension will launch gcc to build them with the parameters specified in the project properties. After a successful build, your code will run on the remote machine and will work until it reaches the previously set breakpoint.







Linux project properties



To understand exactly where files are deployed on a remote Linux computer, let's look at the project properties.







As indicated in Remote settings, by default, the ~ / projects / directory is used as the root folder on the remote computer, and the name of the remote project directory corresponds to the name of our project. If we browse the contents of directories on a remote Linux computer, we will find main.cpp and the files generated during the build process in the ~ / projects / ConsoleApplication1 folder.







The General section lists the paths to the output directory and intermediate files. In addition, as you can see, the configuration type for the project is Application. Therefore, the executable file is saved in the bin / x64 / Debug / directory under the name ConsoleApplication1.out. Please note: two other types of configuration are available - static and dynamic libraries.



Console window



You can work with remote executable files through the console window. In this window, you can view the result of the program and enter input data. To activate this window, select Debug → Linux Console . Here is how it looks in practice.







Below is a simple program code - use it to work with the console yourself.



 #include <cstdio> void log(char filename[], char visitor[]) { FILE * pLog; pLog = fopen(filename, "a"); if (pLog != NULL) { fputs(visitor, pLog); fputs("\n", pLog); fclose(pLog); } } int main(int argc, char* argv[]) { if (argc != 2) { printf("Please pass filename as input parameter\n"); return 1; } char input[20]; printf("What IDE are you using?\n"); scanf("%19[0-9a-zA-Z ]", input); printf("%s! You can use that with me?!\n", input); log(argv[1], input); return 0; } 


In the project properties on the Debugging page, in the Command Arguments field, specify the file name. On this page, you can also change the working folder if your home directory for some reason does not fit.







IoT projects for Linux



Now consider the work with the Internet of Things device - Raspberry Pi. The extension supports any Pi device running Raspbian. In our Blink template, we will use wiringPi - if you do not have such a system, you can install it via apt or from the source code. To add a connection, select Tools → Options and enter Linux in the search field. Now click Add to connect to the Raspberry Pi device.







Open the project properties and in the Build Events section, find the category Remote Post-Build Events.







In these parameters, you can specify the execution of a command on a remote Linux computer after the build is completed. This template is initially configured to export the GPIO contact for the LED indicator - thanks to this, the executable file does not need to be run as root.



Now connect the LED indicator to pin 17 of your Raspberry Pi as shown below.







Open the main.cpp file and set a breakpoint to invoke the delay after the first digitalWrite instruction. Press F5. When a breakpoint is reached, execution will pause. In this case, the LED will be on. Continue executing the code until the next digitalWrite call. On it, the LED goes out.



How to work with Intel Edison board through VC ++ for Linux extension



Working with the Intel Edison board through the VC ++ for Linux extension is almost the same as using other Linux systems. The first step is to configure the board in accordance with the instructions from Intel . After connecting Edison to the Wi-Fi network, you can connect to it using our Connection Manager. If you need to connect to Edison directly, use the instructions for connecting to Edison via Ethernet over USB - our connection manager also supports this option.



Edison makes it easy to start creating an IoT application that processes data from sensors. Devices such as Arduino expansion cards make it easy to connect additional modules, such as the Grove expansion card from Seeed Studios. This expansion card helps you work with a variety of Grove sensors available, without worrying about wiring to connect to the breadboard — you can start writing code right away. Intel has released the UPM Sensor Library, which makes the task even easier: it supports a wide range of sensors, including Grove.



The image shows an Edison compute module on an Arduino circuit with a Grove expansion card connected and a temperature sensor connected.







Create a new project in Visual Studio. Select VC ++ → Cross Platform → Linux → Empty Project . When building Edison solutions, you need to select x86 as the solution platform. Add a C ++ file to the project. Use the code from Intel's Grove Temperature Sample . UPM libraries are included in the Yocto Linux image, which Edison uses by default, so you don’t need to install them further. Change the include options by specifying the appropriate file location.



 #include <upm/grove.h> 


After that, the code can be collected and executed. Here is the result of running this example in Visual Studio debug mode. You may have been embarrassed by the first line of output. The fact is that the first attempt to read data from sensors of this type usually gives a not entirely accurate result.







To enable IntelliSense, follow the instructions below. You will need to copy the included files from Edison to your local environment. While IntelliSense works with an error which is shown in this example. This will be fixed in a future update.



Classic applications



So, we considered the creation of applications for systems without monitors and for specialized devices running Linux. But what about desktop PCs? We have prepared something special for you. We will now launch the OpenGL application on a desktop PC running Linux. First make sure your Linux PC is properly configured to develop OpenGL solutions. We used the following apt packages: libgles1-mesa, libgles1-mesa-dev, freeglut3, freeglut3-dev.



Now create an empty Linux project and download the Spinning Cube source code from the link from the tutorial article about OpenGL by Julien Gerto (Julien Guertault). Unzip the archive and add main.c to your project. To use IntelliSense technology, you need to add OpenGL header files to VC ++ directories. You can download them from the OpenGL registry . Now open the project properties and in the Pre-Launch Command field enter “export DISPLAY =: 0.0”.







In the Linker Input field, specify the library dependencies: "m; GL; GLU; glut".



Make sure that the required computer is specified in the remote connection settings.







Now press F5.







To get interesting results, you can put a breakpoint approximately on line 80, where the cube rotation is set (try changing the alpha value), or on the KeyboardFunc call, where you can view the values ​​of the key pressed.



Makefile Project Template



The Makefile project template allows you to use external build systems on a remote computer (make, gmake, CMake, bash scripts, and the like). It works like this: on the C ++ project properties page, you can configure local Intellisense paths , then add commands (separated by a semicolon) on the remote assembly properties page that initiate your assembly on a remote computer. In this example, we move to the remote directory where the build script is located, and run it.



Here, several bash scripts are combined that can generate a project makefile based on your source code files, taking into account the directory structure. In these scenarios, it is assumed that the source code on a Linux computer is in a directory that is mapped to Windows. They set a new flag in the project properties in order not to copy files remotely. It is unlikely that this option is suitable for all tasks, but it should be a good starting point for a large project.







Usage tips



The tips in this section will help you make better use of the extension.



Detailed build log



There were a lot of questions about exactly which arguments are transmitted by the GCC. The output we see when building does not provide the necessary information, but there are two ways to fix this. Search for the word “verbosity” in the quick entry window or select Tools → Options → Projects and Solutions → Build and Run . To configure MSBuild Project output verbosity, select the diagnostic value. Then in the output window during the assembly the most complete information will be displayed. This way you can find out exactly which arguments were passed to GCC when building the program. This information may be useful if the program does not work correctly.



Where to get the included files



Everyone loves IntelliSense, but we have not yet implemented the synchronization of the included files from your Linux system. All transfer and copy these files in their own way, and this is good. We will talk about a very simple way: just copy the entire folder to the local Windows system via PSCP .



 pscp -r root@192.168.2.15:/usr/include . 


Now open the project properties, go to the VC ++ Directories section and add a local path.



Manage copying remote files



At the file and project level, you can specify whether to copy the file remotely. Because of this, you can use existing build mechanisms by simply matching existing resources locally and adding them to your project for editing and debugging.







Redefining the path to the C / C ++ compiler



In the Property Pages window, you can override the compiler commands that will be used on the remote computer. So if necessary, you can choose specific versions of GCC or even another compiler - for example, clang. You can specify both full paths and available commands.







Build Events



In the Build Events section of the project properties, new event types are available - those that are run before building and assembling, as well as tools for copying arbitrary files for any build events. With these tools you can manage the process flexibly.







Debug Options



The extension supports not only gdbserver, but also gdb mode, which allows for increased compatibility if there are no suitable gdb client binaries for the remote target in Windows.







In addition, you can override the debugger command itself. This is useful for debugging external programs that were not built in VS.







On the Debugging page, you can specify additional gdb commands in the project properties that will be passed to the debugger before it starts. This can be useful, for example, when developing Azure IoT projects. When using the Azure IoT C SDK on the Raspberry Pi, when you start debugging, an incorrect exception may be generated. The reason is the interaction between libcrypto and gdb. A discussion of this issue is available here . You can simply continue debugging or avoid this problem altogether by sending a special instruction to the debugger - in this case “handle SIGILL nostop noprint”.







useful links



Source: https://habr.com/ru/post/319962/



All Articles