The overall environment

  • Operating system: Mac OS
  • Compiler: clang/ Clang ++
  • Debugger: LLDB
  • IDE: VS Code
  • Builder: cmake

The installationVS Codeandcmake

VS Code

Vs code website

cmake

If there is no source change and no ladder, proceed to the next brew source setup
brew update
brew install cmake
Copy the code

The brew source set

Replace existing upstream:

git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git

brew update
Copy the code

Recovery:

git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git

git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git

git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask.git

brew update
Copy the code

Replace Homebrew Bottles:

# bash user
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
# ZSH user
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc
Copy the code

VS Code plug-in

- Chinese(Simplified): extension to vscode- Markdown *: write Markdown files such as readme. md using - vscode- ICONS: ICONS packageCopy the code

Project directory structure

instructions

  • . Vscode: saves the VScode configuration
    • Launch. json: Used to configure the startup of an application
    • Task. json: used to configure the compilation task before the program starts
  • Build: Saves executioncmakeCommand to generate the files required for compilation
    • The Makefile:cmakeAutomatically generated and executed in this directorymakeComplete project compilation
  • Include: header file directory
  • SRC: indicates the source file directory
  • Cmakelists. TXT: files required by cmake
  • Note: files in the build directory do not need to be created manually, we just need to create the build directory and do the restcmakeAutomatically created

Program source code

Learn how to write code for accelerated c++ and learn how to use cmake as a template project

Project Description: Input name, midterm score, final score, [daily homework score], calculate the final score according to certain rules (0.2*midterm+0.4*final+0.4(the median of daily score))

Making the address

CMakeLists.txt

# Minimum version of CMake required to build this project
cmake_minimum_required(VERSION 3.0)

# Name of the project
project(Project)

# scan all source files under SRC/and store the file names in DIR_SRCS
aux_source_directory(src/ DIR_SRCS)

# Add all the source files needed to build the executable
# ${DIR_SRCS}: all source files
# main: The executable file name to be generated
add_executable(main ${DIR_SRCS})

Copy the code

The one above is the simplest example of cmakelists.txt. As the project gets bigger and needs more and more features, learn gradually

launch.js

The startup configuration file used to perform debugging

{
    "version": "0.2.0"."configurations": [{"name": "(lldb) launch debug"."type": "cppdbg"."request": "launch"."program": "${workspaceFolder}/build/main"."args": []."stopAtEntry": false."cwd": "${workspaceFolder}"."environment": []."externalConsole": true."MIMode": "lldb"."preLaunchTask": "make"}}]Copy the code

instructions

  • “Program “: The location of the executable, corresponding to the name of the generated file in cmakelists.txt
  • “ExternalConsole “: enable external terminal
  • “PreLaunchTask “: tasks (usually compilation) that need to be performed before the file can be executed

task.js

A configuration file that is compiled before debugging

{
    "version": "2.0.0"."tasks": [{"label": "make"."type": "shell"."options": {
                "cwd": "${workspaceRoot}/build"
            },
            "command": "make"."group": {
                "kind": "build"."isDefault": true}}}]Copy the code

The above code is equivalent to

cd build
make
Copy the code

Note: Before executing make above, you need to execute the following command

mkdir build
cd build
cmake ..
Copy the code

We don’t need to execute this command ourselves: the plugin cmake Tools does it for us

Key: Command +shift+ P, enter cmake in the input box, and run clean rebuild

conclusion

After you have configured all the above files, do the following:

  1. command+shift+p -> cmake:clean rebuild
  2. Perform debugging:

cd build
make
./main
Copy the code
  1. The system will pop up a Terminal window for us to debug