组织较多的源文件,配置CMake
安装C/C++、CMake、CMakeTool插件
Ctrl+Shift+P 搜索cmake kits配置kits,搜索cmake config
camke set build taregt设置编译目标
F7开始编译
组织较少的源文件,无需配置CMake
Using C++ on Linux in VS Code
需要配置三个文件:
tasks.json (compiler build settings)
launch.json (debugger settings)
c_cpp_properties.json (compiler path and IntelliSense settings)
${file} //当前文件全名
${fileDirname} //当前文件路径
${fileBasenameNoExtension} //当前文件无后缀名
${workspaceFolder} //当前工作区路径
注意:在编译或运行时要将当前查看文件切换到cpp文件上,因为"${file}"指的是当前文件
// task.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-8 生成活动文件",
"command": "/usr/bin/g++-8",
"args": [
"-g",
// "${file}",
"${workspaceFolder}/*.cpp",
"-o",
"${fileDirname}/out/test.out"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: /usr/bin/g++-8"
}
]
}
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++-8 - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/out/test.out",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++-8 生成活动文件",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/g++-8",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}