環(huán)境
Win + MinGW + CMake + Git
單文件工程
# 1 指定最小版本號(hào)
cmake_minimum_required(VERSION 3.10)
# 2 指定工程名
project(Tutorial)
# 3 設(shè)置編譯器路徑
set(CMAKE_C_COMPILER "D:/ProgramPackage/mingw64/mingw64/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "D:/ProgramPackage/mingw64/mingw64/bin/g++.exe")
# 4 屏蔽 git/sh.exe 的影響
# For MinGW make to work correctly sh.exe must NOT be in your path.
# Run cmake from a she11 that does not have sh.exe in your PATH.
set ( DCMAKE_SH= "CMAKE_SH-NOTFOUND")
# 5 設(shè)置工程版本號(hào)
project(Tutorial VERSION 1.0)
# 6 設(shè)置編譯語(yǔ)言標(biāo)準(zhǔn)
set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD_REQUIRED True)
# 7 (可選)配置編譯文件,可通過(guò)in文件替換代碼中的宏定義
configure_file(TutorialConfig.h.in TutorialConfig.h)
# 8 設(shè)置可執(zhí)行文件對(duì)應(yīng)源文件
add_executable(Tutorial tutorial.cxx)
# 9 包含頭文件路徑
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}" )
編譯指令
mkdir build
cd build
cmake -G "MinGW Makefiles" (Win環(huán)境下使用 "cmake .." 命令與MinGW不兼容)
cmake --build . 或 mingw32-make
參考資料
官方教程1: https://cmake.org/cmake/help/latest/guide/tutorial/A%20Basic%20Starting%20Point.html
官方教程2:
https://cmake.org/cmake/help/book/mastering-cmake/cmake/Help/guide/tutorial/index.html
官方練習(xí)材料1:https://github.com/Kitware/CMake.git 文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-670035.html
官方練習(xí)材料2:https://github.com/Kitware/CMake/blob/master/Help/guide/tutorial/Step1/tutorial.cxx文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-670035.html
到了這里,關(guān)于CMake學(xué)習(xí)筆記-VSCode使用Cmake編譯C++工程的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!