目标是在 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
创建一个项目目录: 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
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
/**
* 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;
}
参考:
vscode 集成git bash, mingw, mintty 的terminal
在Windows平台使用msys2、mingw64和vscode编写和调试C/C++代码
vscode基于Linux和Windows下c/c++的多文件编译与连接
linux+vsCode+makefile – 调试C
用户偏好全局设定(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:\"
直接用vscode打开vscdebug目录, 按 F5 调试.
或者用 cygwin, 切换到 vscdebug目录, 输入命令: code .
启动项目, 按F5调试. 如下图:
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
参考:Windows使用VSCode远程Linux
将 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
}
]
}
{
"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
https://github.com/pepstack/vscdebug