Article directory
1. Which project templates can be selected
There are two easy-to-use ways to use visual studio to develop Linux projects remotely, one is only suitable for Linux system projects , and the other is cross-platform C++ projects .
2. C++ projects only applicable to Linux systems
This method is relatively simple. We use this method because we value the powerful debugging function of visual studio.
Proceed as follows:
- To create a new project
template, the platform must be selected as Linux , here we choose an empty project.
- Configure Remote Server
Tools->Options->Cross-Platform->Connection Manager->Add
- Configuration Properties
Configure Remote Build Directory
- Breakpoint debugging
Next, you can happily add breakpoints in vs for debugging.
You can see that the corresponding process and gdb process will also run on the remote Linux server.
If it is a large-scale multi-threaded project and has not been used, I don't know if there is a pit!
3. Cross-platform c++ project (cmake)
Use cmake for project builds instead of relying on Microsoft's sln for builds. It can be debugged in both local windows and remote Linux, and it is very convenient to do cross-platform development. The premise is to use cmake~~. Here is a good CMake tutorial
"CMake Tutorial"
The steps are as follows:
- Create new project
SelectCMake项目
, this kind of project will not be generated again.sln
or.vcxproj
. - Manage Configuration
Configuration->Manage Configuration, click to openCMakePresets.json
the file.
The Linux-debug item is the attribute configuration related to our remote debugging.
The example is as follows
{
"name": "linux-debug", #会在调试框中显现的名称
"displayName": "Linux Debug",
"generator": "Unix Makefiles",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cmakeExecutable": "/usr/bin/cmake", #填写远程的CMake路径
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Linux"
},
"vendor": {
"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
//"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
"sourceDir": "/var/tmp" #本机代码复制到远程的目录 如果不设置默认用上一行的路径,也就似乎 ${HOME}下的.vs目录,此例中是改为了/var/tmp目录
}
}
- Remote operation
Write code to judge the platform type, introduce different header files, and run different functions to achieve the effect of process dormancy.
Windows platform can use the Sleep function in windosw.h;
Linux platform can use the sleep function in unistd.h.
You can also use the sleep function in c++11 to achieve a cross-platform effect and save the judgment of the platform.
"C++ cross-platform judges the current operating system windows, linux and compiler through macro definition"
- Local Execution
Local execution is self-explanatory.
The local project is also built through cmake.
The mastery of cmake is the key.