前言
作爲一個快捷、跨平臺的IDE除了資源佔用比較大,其他真沒啥硬傷。比較適合方便輕量級開發,如以工程形式打開文件夾:
這樣就不必爲了一個“hello world”新建工程了。而且,它的智能提示和對調試器的集成真的很不錯。雖然vs也可以,甚至做的更好,但不能跨平臺。關於VS Code完整的介紹請參考官方文檔,這裏只介紹本人在使用過程中遇到的關鍵的問題點。總結一下對IDE的“關鍵”要求:
- 智能提示 (c_cpp_properties.json)
- 編譯 (tasks.json)
- 調試 (launch.json)
開發環境配置(settings.json)
- 如果已經安裝了Visual studio,那麼真的沒必要使用vs code,後者除了體積小,也沒其他拿得出手的優點了。而且,你不會想着拿vs code做MFC開發吧?
- 如果使用Linux系統,也可以跳過這節。就是Windows下使用集成gcc的開發環境需要折騰一番。
在Windows下常見的第三方集成了GCC的開發環境有MSYS2、Cygwin、Windows Subsystem for Linux(後面簡稱WSL),對於這類開發環境,需要將其集成到VS code的終端窗口中。我本人是使用WSL多一些,偶爾使用MSYS2,多年未再使用Cygwin,所以就以WSL、MSYS2爲例來介紹。
不建議修改全局配置,我們在.vscode下新建settings.json文件(也可以通過快捷鍵Ctrl+, ,然後選擇workspace settings來生成)
- WSL:
{
"terminal.integrated.shell.windows": "bash.exe"
}
上面的"bash.exe"用不着全路徑的,因爲它一般就在windows/system32這個系統路徑下
bash.exe不在系統路徑下,就要補全它的路徑,但它一般都在windows/system32路徑下。
- MSYS2,使用下面的方式集成,可參考How do I integrate MSYS2 shell into Visual studio code on Window?:
"terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\bash.exe",
"terminal.integrated.shellArgs.windows": ["--login", "-i"],
"terminal.integrated.env.windows":
{
"MSYSTEM": "MINGW64",
"CHERE_INVOKING":"1"
}
智能提示 (c_cpp_properties.json)
從個人使用IDE的經驗(visual slickedit、visual studio、codelite、codeblock之類),只要將頭文件路徑配置好就行了。但是VS Code的配置方法跟之前的IDE有點區別,只提供配置文件(做的小也有不方便的地方)。如果文件夾下的.vscode目錄下沒有c_cpp_properties.json這個文件,從View菜單或者快捷鍵(Ctrl+Shift+P)打開命令面板,選擇:C/Cpp: Edit configurations,會新建一個。我們要做的就是把需要包含的頭文件路徑配置上。
配置文件是json格式的,下面拿windows 10 下 WSL (Windows Subsystem for Linux) 的配置爲示範:
{
"configurations": [
{
"name": "WSL",
"includePath": [
"${workspaceFolder}/**",
"C:/Users/[yourname]/AppData/Local/Packages/CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc/LocalState/rootfs/usr/include/**"
],
"defines": ["__linux__", "__x86_64__" ],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"browse": {
"path": ["C:/Users/[yourname]/AppData/Local/Packages/CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc/LocalState/rootfs/usr/include/**"],
"limitSymbolsToIncludedHeaders": false
}
}
],
"version": 4
}
compilerPath將會爲智能提示引擎提供編譯器內置的頭文件路徑,比如標準庫:
**“includePath”**配置項就是智能提示的非編譯器指定包含路徑的頭文件搜索路徑
- /** 表示遞歸該路徑
- "C:/Users/[yourname]/AppData/Local/Packages/CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc/LocalState/rootfs"是安裝的ubuntu18.04(WSL, Windows Subsystem For Linux)的系統路徑。
也可以參考官方的WSL集成介紹
但我覺得沒必要那麼麻煩。但如果你只使用C,請老老實實按照文檔介紹過濾掉C++的頭文件。如果安裝的WSL不是ubuntu 18.04,該文檔也給出來了查找方法,使用powershell:
PS R:\> ($(get-appxpackage).PackageFamilyName | findstr /i 'SUSE Ubuntu') -replace '^', "$`{localappdata`}/Packages/"
${localappdata}/Packages/CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc
${localappdata}/Packages/46932SUSE.openSUSELeap42.2_022rs5jcyhyac
${localappdata}/Packages/46932SUSE.SUSELinuxEnterpriseServer12SP2_022rs5jcyhyac
關於該配置項更詳細的介紹,請參考c_cpp_properties.json Reference Guide
編譯 (tasks.json)
編譯的默認快捷鍵是ctrl+shift+b,vs code通過task.json配置來執行編譯過程,配置的要點就是找到能運行的命令。個人將其分爲兩大類:
- 本機編譯器,特點是輸入路徑就能運行,最簡單的情況就是Linux下的GCC
- 虛擬編譯器,需要進入環境才能運行,如WSL
命令不僅僅指gcc、g++、cl這些編譯器,vs code只是提供了一個執行命令的模板,事實上它不關係你執行什麼命令。開發者可以自己編寫Makefile,使用make當作模板中的命令參數。你也可以給自己配置個先播放一段音樂再執行編譯的task.json。
如果安裝了cl(visual studio,微軟本家的C++編譯器),默認的c_cpp_properties.json內容大致如下(vs2017):
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath":
"C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
- 先介紹下MSYS2環境下編譯工具鏈的安裝(我自己也老忘記)。
- 從sourceforge下載安裝包,一路next
- 安裝 gcc、gdb 、make
安裝: pacman -S mingw-w64-x86_64-gcc
查找GCC的相關軟件包:pacman -Sl |grep 軟件包名稱:如gcc
- WSL ( windows subsystem for linux )
當在寫這篇文章的時候,VS Code的C++插件已經支持WSL了,我們只需要按照前一節《集成終端窗口》將bash集成到vs code,剩下的事就跟Linux下直接使用gcc沒多大區別了,可以參考: 官方解惑
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "wsl build",
"type": "shell",
"command": "g++",
"args": [
"hello_vscode.cpp", "-g",
"-o",
"hello"
],
"group": {
"isDefault": false,
"kind": "build"
},
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$gcc"
}
]
}
如果使用GCC編譯器,Linux以及Windows下的WSL和Msys2的編譯配置沒什麼不同,都可以使用上述配置爲模板
調試 (launch.json)
如果沒有配置調試,會提示你新建一個。
先從Linux系統下的配置入手,因爲它最爲簡單,下面就是程序自動生成的模板,通常我們只需要替換program字段就可以了:
{
// 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": "enter program name, for example ${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
相比智能提示、編譯,Windows下使用GDB調試的配置要麻煩一些。調試器要根據符號表中的路徑去尋找對應源文件,使用msys2或者wsl,就要注意他們對路徑的表示是不同的。
MSYS2:/[盤符]/[路徑], 如c:\test,表示爲:/c/test
WSL(ubuntu): /mnt/[盤符]/,對於c:\test,表示爲:/mnt/c/test
- MSYS2 環境配置文件
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/test.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [ ],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:/msys64/usr/bin/gdb.exe",
"launchCompleteCommand": "exec-run",
"sourceFileMap": {
"/d/": "d:/"
},
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
- WSL 環境配置文件(Windows SubSystem for Linux)
{
// 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": "/mnt/d/code/cpp_blog/hello",
"args": [],
"stopAtEntry": false,
"cwd": "/mnt/d/code/cpp_blog",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"pipeTransport": {
"pipeCwd": "",
"pipeProgram": "c:\\windows\\system32\\bash.exe",
"pipeArgs": ["-c"],
"debuggerPath": "/usr/bin/gdb"
},
"sourceFileMap": {
"/mnt/d/": "d:\\"
}
}
]
}
vs code的調試界面集成度很高,可以在調試控制檯執行調試命令,界面看着也舒服
- sourceFileMap:它將編譯時的路徑映射爲本地路徑。比如,這個工程目錄爲:d:\code\cpp_msys2,源文件爲:hello_vscode.cpp,如果沒有這個配置,會出現下面的錯誤:Unable to open ‘hello_vscode.cpp’: File not found (file:///d/code/cpp_msys2/hello_vscode.cpp).參考Debug error “Unable to ‘…’: File not Found”
- miDebuggerPath:如果把調試器納入系統路徑,那麼是不需要指定全路徑的
- externalConsole:因爲前面已經把bash集成到了控制檯,所以這個選項要給false,否則會有如下類似的錯誤:ERROR: Unable to start debugging. Unexpected GDB output from command “-exec-run”. During startup program exited with code 0xc0000135.
- 對於調試配置文件的完整介紹可參考Configuring launch.json for C/C++ debugging
總結一下
VS Code跨平臺、體積小、插件豐富、功能強大但配置也麻煩
VS Code 體積小,但佔用資源可不比任何IDE落後(clion除外)
VS Code的智能提示的強大、穩定、速度只是比VS差點,但比其他IDE不差(比如:codelite、codeblock、slickedit、clion)
負責VS Code的發佈程序員心情一直不好,因爲會出現Linux下32位系統安裝程序給你安裝64位插件集、Windows下自動升級刪除原程序後又提示找不到程序的低級BUG,而且長期就這德行