Windows上使用VSCode結合mingw編譯和調試C程序
目標是在 Win7上, 用 VSCode使用mingw調試C 程序. 要設置終端爲 bash.exe.
Windows7 已經安裝了 VSCode(及相關插件), MSYS64, Cygwin 等. Windows系統環境變量如下:
MSYS64_HOME=C:\DEVPACK\msys64
Path=...;C:\DEVPACK\msys64\usr\bin\;C:\DEVPACK\MicrosoftVSCode\bin\
獲取最後代碼:
https://github.com/pepstack/vscdebug
1. vscdebug 項目設置
創建一個項目目錄: vscdebug/, 包含全部如下內容:
vscdebug/
.vscode/
c_cpp_properties.json
launch.json
tasks.json
vscdebug.c
mingw64-make.sh
Makefile
各文件內容如下:
-
c_cpp_properties.json
{ "configurations": [ { "name": "Win32", "windowsSdkVersion": "10.0.18362.0", "compilerPath": "${env:MSYS64_HOME}/mingw64/bin/gcc", "includePath": [ "${env:MSYS64_HOME}/mingw64/include" ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE" ], "intelliSenseMode": "gcc-x64" } ], "version": 4 }
-
launch.json
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) debug", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/vscdebug.exe", "targetArchitecture": "x64", "args": [], "stopAtEntry": true, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "gdb.exe", "preLaunchTask": "make", "setupCommands": [ { "description": "爲 gdb 啓用整齊打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
-
tasks.json
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "make", "type": "shell", "command": "/C/Users/cheungmine/Workspace/github.com/vscdebug/mingw64-make.sh", "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true } } ] }
-
mingw64-make.sh
#/usr/bin/bash _file=$(readlink -f $0) _cdir=$(dirname $_file) _name=$(basename $_file) echo ${_cdir} cd ${_cdir} && make
- Makefile
# Makefile CC=gcc all: vscdebug.exe vscdebug.exe: ./vscdebug.c $(CC) -g ./vscdebug.c -o $@ .PHONY: all clean clean: rm -rf ./vscdebug.exe
-
vscdebug.c
#include <stdio.h>
/**
* cmd as administrator
* add system env:
* MSYS64_HOME=C:\DEVPACK\msys64
* Path=...;C:\DEVPACK\msys64\usr\bin
* mklink /J "/C" "C:\"
*/
int main(int argc, char *argv[])
{
int a = 25;
printf("hello vscode %d.\n", a);
return 0;
}
2. 全局設置終端爲 msys64的bash.exe
參考:
用戶偏好全局設定(VSCode->Ctrl+Shift+P, Settings json)
-
settings.json
{ "java.home": "C:\\DEVPACK\\Java\\jdk1.8.0_162", "java.help.firstView": "gettingStarted", "remote.SSH.remotePlatform": { "pc": "linux", "vm-cent7": "linux" }, "java.configuration.checkProjectSettingsExclusions": false, "java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx1G -Xms100m -javaagent:\"/home/cl/.vscode-server/extensions/gabrielbb.vscode-lombok-1.0.1/server/lombok.jar\"", "editor.suggestSelection": "first", "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue", "window.zoomLevel": 1, "git.ignoreLegacyWarning": true, "editor.formatOnSave": true, "terminal.integrated.shell.windows": "${env:MSYS64_HOME}/usr/bin/bash.exe", "terminal.integrated.shellArgs.windows": ["--login"], "terminal.integrated.env.windows": { "MSYSTEM": "MINGW64", "CONTITLE": "MinGW x64", "C_INCLUDE_PATH": "/mingw64/include", "CPLUS_INCLUDE_PATH": "/mingw64/include" } }
-
創建目錄鏈接
因爲 vscdebug項目在C盤, 所以爲了調試, 還需要創建驅動器鏈接. 以管理員運行 cmd, 輸入下面的命令:
C:\Windows\system32>cd \
C:\>mklink /J "/C" "C:\"
爲 /C <<===>> C:\ 創建的聯接
如果代碼在 E:\ 則上面的改爲:
C:\>mklink /J "/E" "E:\"
3. 開始調試源代碼
直接用vscode打開vscdebug目錄, 按 F5 調試.
或者用 cygwin, 切換到 vscdebug目錄, 輸入命令: code .
啓動項目, 按F5調試. 如下圖:
4. 遠程調試 vscdebug 項目
4.1 準備工作
1)需要 Windows ssh 登陸到 Linux (centos7及以上)支持免密. 將Windows用戶證書添加到Linux的: ~/.ssh/authorized_keys.
Windows用戶證書(msys64) :
$ cd $USERPROFILE
$ pwd
/c/Users/cheungmine
$ ssh-keygen
將下面文件的內容追加到Linux的文件(~/.ssh/authorized_keys)
/c/Users/cheungmine/.ssh/id_rsa.pub
2)Windows 的VSCode 需要安裝一些插件:
-
Remote Development
4.2 遠程調試
將 vscdebug 下載到遠程 Linux (centos7) 上。然後在 Windows 上遠程調試。配置的過程比 Windows 上本地調試簡單。需要注意的是,vscdebug/.vscode/下面的3個json文件的配置。每個文件新增的部分如下:
-
c_cpp_properties.json
{ "name": "Linux", "compilerPath": "/usr/bin/gcc", "includePath": [ "/usr/include", "/usr/local/include" ], "defines": [ "_DEBUG" ], "intelliSenseMode": "gcc-x64" }
-
launch.json
{
"name": "linux-gdb",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/vscdebug.exe",
"targetArchitecture": "x64",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"preLaunchTask": "linux-make",
"setupCommands": [
{
"description": "爲 gdb 啓用整齊打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
- tasks.json
{
"label": "linux-make",
"type": "shell",
"command": "cd ${workspaceFolder} && make",
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
區別是 launch.json,Windows本地調試代碼,設置爲:
"externalConsole": true
從Windows遠程調試Linux上的代碼,務必設置爲:
"externalConsole": false
運行截圖: