This post is about working on VS Code’s C/C++ development environment on a Mac. The whole process is actually very simple, mainly based on a video from B station and a huge hard official Doc.

Please write down the general steps of the video at station B. After all, the pace of the video is slow, and it is inconvenient to extract key points and reuse them (mainly inconvenient, after all, lazy cancer + procrastination patients).

  1. Download and install VS Code from the official website, nothing to say

  2. Add the code command to the environment variable (optional). Shift + Cmd + P call up the Command Palette and search for install to find the Shell Command: install ‘code’ Command in PATH. The effect is to open the specified directory in the workspace directly from the terminal with the code command. One step ahead, that’s all.

  3. Install C/C++ plug-in, Microsoft product.

  4. Install CodellDB to enable DEBUG by starting LLDB. This plugin may not support debugging very well. Win/Linux uses GDB, so this step is not required.

  5. On the Debug page, click Create a launch.json file and select the LLDB template.

    Change the default program value to ${workspaceFolder}/${fileBasenameNoExtension}

  6. Configuration tasks. Json

    Search tasks in the Command Palette for Tasks:Config Task, and select C/C++:clang(++) Build active file(select Clang/Clang ++ depending on the language of the project). Add the C/C++ standard “-std= C11 “or “-std= C++ 17” to args.

  7. Set the preLanuchTask

    The name preLanuchTask explains its purpose. Add the “preLaunchTask”: “clang Build active file” configuration for Coningurations in the launch.json file. The “clang Build active file” value comes from the “label” in tasks.json: “Clang Build active file”.

  8. Configure the global C/C++ standard.

    Click the gear icon Setting in the lower left corner of the UI, search for C Standard, and set corresponding standards in the displayed options.

Following this step configuration can already meet some simple coding requirements, such as writing a Hello World, debugging Leetcode topics and so on.

But there is a problem with following this step, including some videos or tutorials found on the Internet. Even if they are only useful for single-file C/C++ projects, such as a single main. C file for the entire project, a link error will be reported when developing multi-file C/C++ projects. Tasks. json: -g ${file} -o ${fileDirname}/${fileBasenameNoExtension} An error occurs when parsing functions or variables defined in other files.

After a quick search from StackOverflow to the official Doc, the problem was solved. The most important parts are as follows:

You can modify your tasks.json to build multiple C++ files by using an argument like ” CPP “insteadof workspaceFolder/ workspaceFolder}/*.cpp” insteadof workspaceFolder/ workspaceFolder}/*.cpp” insteadof{file}. This will build all .cpp files in your current folder. You can also modify the output filename by replacing ” fileDirname/{fileDirname}/fileDirname/{fileBasenameNoExtension}” with a hard-coded filename (for example “${workspaceFolder}/myProgram.out”).

Indeed, official documentation is the best tutorial.

Development environment toss good, can feel at ease to eat ashes to happily masturbate code ~