# Using Clang in Visual Studio Code
## Run C++ code with vscode
### Step1 -- Build your code
From the main menu, choose <font color="#b00">Terminal > Configure Default Build Task</font>. A dropdown will appear listing various predefined build tasks for the compilers that VS Code found on your machine. Choose <font color="#b00">C/C++ clang++ build active file</font> to build the file that is currently displayed (active) in the editor.
### Step2 -- Replace the contents of <font color="#b00">tasks.json</font> with the following:
```json=
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
```
### Step3 -- Running the build
Go back to the file you want to build. It is important that this file be the one that is active in the editor for the next step.
press <font color="#b00">⇧⌘B</font> or from the <font color="#b00">Terminal</font> main menu choose <font color="#b00">Run Build Task</font>.
You can run it in the terminal by typing <font color="#b00">./{fileName}</font>.
## Debug
### Step1 --
From the main menu, choose <font color="#b00">Run > Add Configuration...</font> and then choose <font color="#b00">C++ (GDB/LLDB)</font>.
You'll then see a dropdown for predefined debugging configurations. Choose <font color="#b00">clang++ build and debug active file</font>.
### Step2 --
In launch.json file, active filename <font color="#b00">${fileBasenameNoExtension}</font>, which if <font color="#b00">helloworld.cpp</font> is the active file will be <font color="#b00">helloworld</font>.
### Step3 --
By default, the C++ extension won't add any breakpoints to your source code and the <font color="#b00">stopAtEntry</font> value is set to false.
Change the <font color="#b00">stopAtEntry</font> value to <font color="#b00">true</font> to cause the debugger to stop on the main method when you start debugging.
### Step4 --(Optional)
(for macOS)
若需要輸入,則把<font color="#b00">externalConsole</font>調成<font color="#b00">true</font>,並在tasks.json加入以下參數:
```json=
{
"label": "Open Terminal",
"type": "shell",
"command": "osascript -e 'tell application \"Terminal\"\ndo script \"echo hello\"\nend tell'",
"problemMatcher": []
}
```
Then run this specific task using <font color="#b00">Command + Shift + p</font>. Type <font color="#b00">Tasks</font> and look for <font color="#b00">Tasks: Run Tasks</font> then select <font color="#b00">Open Terminal</font>.
### Step4 --
Go back to <font color="#b00">helloworld.cpp</font> so that it is the active file in the editor. This is important because VS Code uses the active file to determine what you want to debug.
### Step5 --
Press <font color="#b00">F5</font> or from the main menu choose <font color="#b00">Run > Start Debugging</font>.