部分配置参数
${workspaceFolder} - 当前工作目录(根目录)
${workspaceFolderBasename} - 当前文件的父目录
${file} - 当前打开的文件名(完整路径)
${relativeFile} - 当前根目录到当前打开文件的相对路径(包括文件名)
${relativeFileDirname} - 当前根目录到当前打开文件的相对路径(不包括文件名)
${fileBasename} - 当前打开的文件名(包括扩展名)
${fileBasenameNoExtension} - 当前打开的文件名(不包括扩展名)
${fileDirname} - 当前打开文件的目录
${fileExtname} - 当前打开文件的扩展名
${cwd} - 启动时task工作的目录 Current Working Directory
${lineNumber} - 当前激活文件所选行
${selectedText} - 当前激活文件中所选择的文本
${execPath} - vscode执行文件所在的目录
${defaultBuildTask} - 默认编译任务(build task)的名字
首先在Debug/Run中创建一个.vscode文件.
Windows下VsCode单文件调试配置
https://www.jianshu.com/p/ba6f4740c746
Ubuntu下VsCode单文件调试配置
工作目录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(Launch)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for 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": "build",
"type": "shell",
"command": "g++",
"args": ["-g", "${workspaceFolder}/src/${fileBasename}", "-std=c++11", "-o", "${workspaceFolder}/build/${fileBasenameNoExtension}"]
},
]
}
Ubuntu VsCode 多文件编译运行
我是做一个TCP Server和Client的通信,下面是我的文件树
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build server",//在Launch里要用到
"type": "shell",
"command": "g++",
"args": ["-g", "${workspaceFolder}/src/server.cpp", "-std=c++11", "-o", "${workspaceFolder}/build/server.out"]//生成out文件到build文件夹
},
{
"label": "build client",
"type": "shell",
"command": "g++",
"args": ["-g", "${workspaceFolder}/src/client.cpp", "-std=c++11", "-o", "${workspaceFolder}/build/client.out"]
}
]
}
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": "Server",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/server.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"preLaunchTask": "build server",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "Client",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/client.out",
"args": ["127.0.0.1"],//参数列表,没有可不填
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"preLaunchTask": "build client",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
],
"compounds": [
{
"name": "Server/Client",
"configurations": ["Server", "Client"],//运行这两个config
}
]
}
还有一种是makefile运行
因为我不会makefile,所以就把makefile都放在src下了,本来需要分成release/build什么的,有空研究一下makefile怎么写.文件树中的c_cpp_properties.json不需要.
文件树
makefile
all:server client
server:server.o
g++ -g -o server server.o
client:client.o
g++ -g -o client client.o
server.o:server.cpp
g++ -g -c server.cpp
client.o:client.cpp
g++ -g -c client.cpp
clean:all
rm all
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",
"options": {
"cwd": "${workspaceFolder}/src"
},
"type": "shell",
"command": "make"
}
]
}
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": "Server",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/src/server",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "Client",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/src/client",
"args": ["127.0.0.1"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
],
"compounds": [
{
"name": "Server/Client",
"configurations": ["Server", "Client"],
"preLaunchTask": "make",
}
]
}
主要是理解compunds的使用
可以一起run也可以分开run
参考
https://blog.csdn.net/leon_zeng0/article/details/107438624
网友评论