1. 安裝VS Code
2. 打開VSCode 按快捷鍵shift+command+X,並在搜索框輸入c/c++, 安裝C/C++組件
3.在Finder中創建空文件夾並在VS Code中打開, 並File => Save workspace As 將工作區間另存為
4.新建Hello.c文件並保存
5. 接下來需要配置3個JSON文件c_cpp_properties.json、tasks.json、launch.json
6. c_cpp_properties.json : 使用快捷鍵command+shift+p打開命令行面板(Command Palette)
輸入edit configurations,在彈出的列表中選擇帶JSON的C/C++:Edit Configurations(JSON)
此時會自動新增.vscode文件夾,並新建c_cpp_properties.json文件
配置字段 includePath
"includePath": [ "${workspaceFolder}/**", "/Library/Developer/CommandLineTools/usr/include/c++/v1", "/usr/local/include", "/Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/include", "/Library/Developer/CommandLineTools/usr/include" ],
你可能需要注意
"/Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/include",
中的版本號,可以進入Finder,按快捷鍵command+shift+G輸入
/Library/Developer/CommandLineTools/usr/lib/clang/ 以此來查看版本號
7. tasks.json : 在打開.c文件的情況下(比如我這里的hello.c)打開命令行面板command+shift+P,輸入tasks:configure task,選擇Tasks:Configure Task
點擊C/C++:gcc build active file
自動生成tasks.json文件並打開
這里我們需要配置args字段
"args": [ "-g",//編譯命令 "${workspaceFolder}/hello.c",//當前workspace下的hello.c文件 "-o",//輸出文件命令 "${workspaceFolder}/hello.o"//生成的可執行文件的路徑及名稱 ],
8. launch.json :
- 打開命令行面板command+shift+P,輸入launch,選擇Open launch.json
-
選擇環境為C++(GDB/LLDB)
-
自動生成launch.json文件並打開
配置 program字段
這個字段是要運行的文件路徑,寫你生成的可執行文件的路徑即可,比如我這里是
"program": "${workspaceFolder}/hello.o",
9. Shift + Command + B 構建
10. F5 調試