C++ & VS Code & CMake & Mac OS
阿新 • • 發佈:2020-06-24
整體環境
- 作業系統:Mac OS
- 編譯器:clang/clang++
- 偵錯程式:lldb
- IDE: VS Code
- builder:cmake
安裝VS Code
和cmake
VS Code
cmake
# 如果沒換源,並且沒梯子,先進行下一節brew源設定
brew update
brew install cmake
複製程式碼
brew 源設定
替換現有上游:
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
複製程式碼
復原:
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
複製程式碼
替換Homebrew Bottles源:
# bash 使用者
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
# zsh使用者
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc
複製程式碼
VS Code 外掛
附加外掛說明:- Chinese(Simplified): vscode的中文擴充套件
- Markdown *: 編寫Readme.md等markdown檔案用
- vscode-icons:圖示包
複製程式碼
Project 目錄結構
說明
- .vscode:用來儲存vscode配置
- launch.json: 常用來配置程式的啟動
- task.json: 常用來配置程式啟動前的編譯任務
- build:用來儲存執行
cmake
命令所生成的編譯所需的檔案- Makefile: 由
cmake
自動生成,在該目錄下執行make
即可完成專案編譯
- Makefile: 由
- include: 標頭檔案目錄
- src:原始檔目錄
- CMakeLists.txt:cmake所需檔案
- 注意:build目錄中的檔案不需要手動建立,我們只需要建立build這個目錄就行,其他的交給
cmake
自動建立
專案原始碼
學習《accelerated c++》過程中寫的程式碼,並學習cmake使用方法,構成的模板專案
專案描述:輸入姓名、期中成績、期末成績、[平時作業成績],按照一定的規則計算其最終成績(0.2*midterm+0.4*final+0.4(平時成績的中值))
CMakeLists.txt
# Minimum version of CMake required to build this project
cmake_minimum_required(VERSION 3.0)
# Name of the project,可以任意指定
project(Project)
# 掃描 src/ 下的所有原始檔,並將檔名存入 DIR_SRCS 中
aux_source_directory(src/ DIR_SRCS)
# Add all the source files needed to build the executable
# ${DIR_SRCS}:所有原始檔
# main:將生成的可執行檔名
add_executable(main ${DIR_SRCS})
複製程式碼
上面這個是最簡單的 CMakeLists.txt 的例子,隨著專案逐漸龐大,需要越來越多的特性,逐步學習
launch.js
用來執行除錯的啟動配置檔案
{
"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"
}
]
}
複製程式碼
說明
- "program":可執行檔案的位置,對應於cmakelists.txt中生成檔案的名字
- "externalConsole":開啟外部terminal
- "preLaunchTask": 執行該檔案之前需要執行的任務(常為編譯過程)
task.js
用來執行除錯前編譯的配置檔案
{
"version": "2.0.0","tasks": [
{
"label": "make","type": "shell","options": {
"cwd": "${workspaceRoot}/build"
},"command": "make","group": {
"kind": "build","isDefault": true
}
}
]
}
複製程式碼
上面的程式碼等價於
cd build
make
複製程式碼
說明:其實在執行上面的make
之前,需要執行下面的命令
mkdir build
cd build
cmake ..
複製程式碼
來建立編譯專案所需的makefile檔案及其他相關檔案上面這步命令我們不需要自己執行:外掛 cmake tools
會替我們執行
按鍵:command+shift+p,在輸入框中輸入cmake,執行clean rebuild
總結
在完成上述所有檔案的配置後,按下面的操作即可:
command+shift+p -> cmake:clean rebuild
- 執行除錯:
cd build
make
./main
複製程式碼
- 系統會彈出一個 terminal 視窗,供我們除錯