简述
之前都是使用Clion写代码, 只是写写题目, 只有单文件. 虽然有插件自动添加add_executable()
语句到CMakeLists.txt
里. 但Clion还是太臃肿了, 文件数量一多, 启动和索引就要非常久.
后来发现VSCode这个轻量级的编辑器, 但是它只是个编辑器, 要想真正成为Clion那样的IDE还得折腾一番. 为了和Clion无缝过渡, 我打算使用CMake+MSVC作为VSCode的工具链.
配置文件
自动添加add_executable()
.vscode/AddExecutable.ps11 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| param( [string]$WorkspaceRoot = $(throw "Parameter missing: -WorkspaceRoot [Workspace root]"), [string]$SrcDir=$(throw "Parameter missing: -SrcDir [Relative path to the source file]"), [string]$SrcName=$(throw "Parameter missing: -SrcName [Name of the source file]") )
if ($SrcDir[-1] -ne '\') { $SrcDir = $SrcDir.Insert($SrcDir.Length, '\') } if ($WorkspaceRoot[-1] -ne '\') { $WorkspaceRoot = $WorkspaceRoot.Insert($WorkspaceRoot.Length, '\') }
if (!(Test-Path ($WorkspaceRoot + $SrcDir))) { throw "$($WorkspaceRoot + $SrcDir) doesn't exist!" }
if (!(Test-Path ($WorkspaceRoot + $SrcDir + $SrcName))) { throw "$($WorkspaceRoot + $SrcDir + $SrcName) doesn't exist!" }
$SrcNameWithoutExtention = $SrcName.split('.') if ($SrcNameWithoutExtention.Length -ge 2) { $SrcNameWithoutExtention = $SrcNameWithoutExtention[0..($SrcNameWithoutExtention.Length - 2)] } else { $SrcNameWithoutExtention = $SrcNameWithoutExtention[0] }
$SrcNameWithoutExtention = $SrcNameWithoutExtention -join '_' $ExecutableName = ($SrcDir + $SrcNameWithoutExtention) -Replace "\\", "_" echo $("Executable name: " + $ExecutableName) echo $("Source name without extention: " + $SrcNameWithoutExtention)
Add-Content -Path "$($WorkspaceRoot + $SrcDir)\CMakeLists.txt" -Value "`nadd_executable($ExecutableName $SrcName)" -Encoding "utf8" -NoNewline
|
.vscode/tasks.json1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "Add Executable", "command": "./AddExecutable.ps1", "args": [ "${workspaceFolder}", "${relativeFileDirname}", "${fileBasename}", ], "options": { "cwd": "${workspaceRoot}\\.vscode" }, "problemMatcher": [ "$msCompile" ], "group": { "kind": "build", "isDefault": true }, } ] }
|
打开某个*.cpp
文件后, 按下Ctrl + Shift + B
选择Add Executable
, 即可在CMakeLists.txt
文件看到add_executable()
语句了.
其实安装C/C++ Compile Run
插件更方便...