To prepare
To install clang, you need to install it first. There are two ways to install clang on a Mac:
- Install it on the AppStore
Xcode
.clang
Will be inXcode
Automatic installation during installation - Execute on the command line terminal
xcode-select --install
To install
After the installation is complete, run the clang -v command on the CLI to verify the installation
$ clang -v
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Copy the code
Install VS Code
- Download VS Code from the VS Code website
- Double-click to install
VS Code plug-in installed
To run C++ programs, you need to install themC/C++
Plug-in, openVS Code
To enter the plug-in extension (shortcut keysCommand + Shift + X
), the searchC/C++
Plug-in installationIf you don’t understand English, you can install a Chinese plug-inChinese (Simplified) Language Pack for Visual Studio Code
, installation method and installationC/C++
As a plugin, replace the search term withChinese
It is ok
After the plug-in is installed, restart VS Code for it to take effect
Build and run the first C++ program
Use the following steps to create a project directory, firstProject, and open it using VS Code
$ mkdir firstProject
$ cd firstProject
$ code .
Copy the code
createmain.cpp
The file code is as follows
#include <iostream>
using namespace std;
int main(a)
{
cout << "hello world!" << endl;
return 0;
}
Copy the code
According to theCommand + Shift + P
Open the command line panel and entertasks
, the choice ofTasks:Configure Task
generatetasks.json
The configuration fileChoose hereC/C++:clang build active file
Template, it doesn’t matter if you choose another template, you can overlay it underneath VS Code
Automatically in.vscode
Directory generationtasks.json
The configuration fileChange the file content to
{
"version": "2.0.0"."tasks": [{"label": "build"."command": "clang++"."args": [
"-o"."main".// Execute file name
"main.cpp".// The source file to execute
"-g"."-v"]."type": "shell"."presentation": {
"echo": true."reveal": "always"."panel": "shared"
},
"problemMatcher": {
"owner": "cpp"."fileLocation": [
"relative"."${workspaceRoot}"]."pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$"."file": 1."line": 2."column": 3."severity": 4."message": 5}}}]}Copy the code
Then press Command + Shift + B to compile the source file. After compiling, the main file will be generated in the project path./main can be used to execute the file
$ ./main
hello world!
Copy the code
Project commissioning
If you need to debug your project, create a launch.json file and configure it accordingly
- According to the
Command + Shift + D
The running page is displayed
- Click on the
Create the launch.json file
, the choice ofC++(GDB/LLDB)
Again, it’s going to be in.vscode
Create a folderlaunch.json
file
Modify thelaunch.json
The content of the file is
{
"version": "2.0.0"."configurations": [{"name": "C++ Launch"."type": "cppdbg"."request": "launch"."program": "${workspaceRoot}/main".// Main is the same as the execution file name in tasks.json
"preLaunchTask": "build"."internalConsoleOptions": "openOnSessionStart"."logging": {
"moduleLoad": false."programOutput": true."trace": false
},
"showDisplayString": false."args": []."stopAtEntry": false."cwd": "${workspaceRoot}"."environment": []."externalConsole": false.// set true to enable keyboard input
"osx": {
"MIMode": "lldb"}}}]Copy the code
incout
Statement on the breakpoint, and then click the run button to proceeddebug
Reference:Build and Debug C++ on Visual Studio Code for Mac
At this point, the construction of the C++ project environment using VS Code is complete
If feel helpful to you can pay attention to my public numberhuangxy
And share some technical articles from time to time