本地系统是windows,远程服务器是Linux,如何远程编译和调试服务器端的c/c++程序?
一种比较好的方式是:
vs code + remote tools + CMake

1 安装VS CODE

2 在本地的VS CODE安装remote - SSH插件
vs code + CMake 远程编译和调试c/c++程序 - 图1

3 通过remote-ssh远程连接到服务器,然后在远程服务器上的VS CODE安装如下插件:
vs code + CMake 远程编译和调试c/c++程序 - 图2

4 打开远程服务器上的文件夹
菜单”File”->”Open Folder”,选择test,单击”OK”
vs code + CMake 远程编译和调试c/c++程序 - 图3

5 添加源文件
在test新建一个文件”main.c”,如下:

  1. #include <stdio.h>
  2. int main(int argc, char *argv[])
  3. {
  4. printf("Hello world!\n");
  5. return 0;
  6. }

6 添加CMakeLists.txt文件
在test中添加CMakeLists.txt文件,如下:

  1. cmake_minimum_required(VERSION 2.6)
  2. project(test_cmake)
  3. set(CMAKE_CXX_FLAGS, "${CMAKE_CXX_FLAGS} -g") #debug
  4. set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR})
  5. add_executable(test_cmake main.cc)

左侧side bar底部多出了一个CMake, 单击它切换到CMake。
左侧side bar如果没有CMake,可在side bar上右键单击,在弹出菜单中勾选CMake。
image.png

单击该右侧的Configuation(配置所有项目)按钮,等待配置完毕。在该窗口出现了test_cmake…
vs code + CMake 远程编译和调试c/c++程序 - 图5

切换到explorer, 发现多了build文件夹,展开build,发现多了很多文件,但是还没生成文件test_cmake
vs code + CMake 远程编译和调试c/c++程序 - 图6

切换到CMake, 点击build
vs code + CMake 远程编译和调试c/c++程序 - 图7
此时再去build目录,发现生成了test_cmake文件。

7 状态栏CMake Tool
CMake:Select a kit
vs code + CMake 远程编译和调试c/c++程序 - 图8
如上图,vs code的底部状态栏,第5个区域显示的是”No Kit Selected”,表示没有选择 CMake kit,单击该项,或者F1,CMake:Select a kit. select a kit
vs code + CMake 远程编译和调试c/c++程序 - 图9

8 create a launch.json file
单击左侧的dubug, click “create a launch.json file”, or click key F5
vs code + CMake 远程编译和调试c/c++程序 - 图10

select environment
vs code + CMake 远程编译和调试c/c++程序 - 图11

select a configuration
vs code + CMake 远程编译和调试c/c++程序 - 图12

then a file “launch.json” is created in folder “.vscode”, make some change as below:

  1. {
  2. // Use IntelliSense to learn about possible attributes.
  3. // Hover to view descriptions of existing attributes.
  4. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  5. "version": "0.2.0",
  6. "configurations": [
  7. {
  8. "name": "g++-7 build and debug active file",
  9. "type": "cppdbg",
  10. "request": "launch",
  11. "program": "${command:cmake.launchTargetPath}", //modify
  12. "args": [],
  13. "stopAtEntry": false,
  14. "cwd": "${workspaceFolder}",
  15. "environment": [],
  16. "externalConsole": false,
  17. "MIMode": "gdb",
  18. "setupCommands": [
  19. {
  20. "description": "Enable pretty-printing for gdb",
  21. "text": "-enable-pretty-printing",
  22. "ignoreFailures": true
  23. }
  24. ],
  25. "preLaunchTask": "g++ test", //the task label in task.json
  26. "miDebuggerPath": "/usr/bin/gdb"
  27. }
  28. ]
  29. }

9 create a file task.json
按F5,弹出提示框,选择”Configure Task”
vs code + CMake 远程编译和调试c/c++程序 - 图13
then a file “task.json” is created in folder “.vscode”, make some change as below:

  1. {
  2. // See https://go.microsoft.com/fwlink/?LinkId=733558
  3. // for the documentation about the tasks.json format
  4. "version": "2.0.0",
  5. "tasks": [
  6. {
  7. "type": "shell",
  8. "label": "g++ test",
  9. "command": "cmake --build ${workspaceFolder}/build --config Debug --target all -- -j 50",
  10. "args": [],
  11. "options": {
  12. "cwd": "/usr/bin"
  13. },
  14. "problemMatcher": [
  15. "$gcc"
  16. ],
  17. "group": "build"
  18. }
  19. ]
  20. }

编译和调试

编译 快捷键:F7
调试 快捷键:F5
如果断点无效,就删除上述非源文件,从第6步开始,重新做一遍即可。