Skip to content

使用CMake和VSCode构建跨平台C++开发环境

准备

  • VSCode
  • CMake
  • CMake插件

项目模板

shell
/------
|-.vscode
|  |-tasks.json
|  |-launch.json
|-build
|-src
|  |-main.cpp
|-CMakeLists.txt
  • CMakeLists.txt

    cmake
    cmake_minimum_required(VERSION 3.10)
    set(PROJECT_NAME Demo)
    project(${PROJECT_NAME})
    add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/source-charset:utf-8>")
    file(GLOB_RECURSE SRC_FILES src/*.cpp)
    add_executable(${PROJECT_NAME} ${SRC_FILES})
  • task.json

    json
    {
    	"version": "2.0.0",
    	"tasks": [
    		{
    			"label": "mkbuild",
    			"type": "shell",
    			"command": "mkdir",
    			"args": [
    				"-p",
    				"build"
    			],
    			"windows":{
    				"command": "cmd",
    				"args": [
    					"/C",
    					"if not exist build mkdir build"
    				]
    			},
    			"group": "build",
    			"presentation": {
    				"echo": true,
    				"reveal": "always",
    				"focus": false,
    				"panel": "shared",
    				"showReuseMessage": true,
    				"clear": true
    			},
    			// Use the standard MS compiler pattern to detect errors, warnings and infos
    			"problemMatcher": "$msCompile",
    		},
    		{
    			"label": "cmake",
    			"type": "shell",
    			"command": "cmake",
    			"args": [
    				".."
    			],
    			"options": {
    				"cwd": "build"
    			},
    			"group": "build",
    			"presentation": {  // 一些控制台显示配置
    				"echo": true,
    				"reveal": "always",  
    				"focus": false,
    				"panel": "shared",
    				"showReuseMessage": true,
    				"clear": true
    			},
    			"problemMatcher": "$msCompile",
    			"dependsOn":["mkbuild"]
    		},
    		{
    			"label": "compile",
    			"type": "shell",
    			"command": "cmake",
    			"args": [
    				"--build",
    				"./",
    				"--",
    			],
    			"options": {
    				"cwd": "${workspaceFolder}/build"
    			},
    			"group": "build",
    			"presentation": {
    				// Reveal the output only if unrecognized errors occur.
    				"reveal": "always",
    				"clear": true
    			},
    			"problemMatcher": "$msCompile"
    		},
    		{
    			"label": "run",
    			"type": "shell",
    			"command": "./Demo",
    			"group": "build",
    			"presentation": {
    				"echo": true,
    				"reveal": "always",
    				"focus": false,
    				"panel": "shared",
    				"showReuseMessage": true,
    				"clear": true
    			},
    			"options": {
    				"cwd": "build" 
    			},
    			"windows":{
    				"command": ".\\Demo.exe",
    				"options": {
    					"cwd": "build/Debug"
    				},
    			},
    			"problemMatcher": "$msCompile",
    			"dependsOn":["compile"]
    		}
    	],
    }
  • launch.json

    json
    {
    	// Use IntelliSense to learn about possible attributes.
    	// Hover to view descriptions of existing attributes.
    	// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    	"version": "0.2.0",
    	"configurations": [
    		{
    			"name": "Debug",
    			"type": "cppdbg", // 调试类型,除使用msvc进行调试外,均为该类型
    			"request": "launch",
    			"program": "${workspaceFolder}/build/Debug/Demo", // 指定C/C++程序位置
    			"args": [], // 指定运行参数
    			"stopAtEntry": false,
    			"cwd": "${workspaceFolder}/build", // 指定工作目录
    			"preLaunchTask": "compile", // 在调试前会先调用这个task编译构建程序
    			"environment": [],
    			"externalConsole": false,
    			"osx": { // macOS的特定配置
    				"MIMode": "lldb" // 指定使用lldb进行调试
    			},
    			"linux": { // linux的特定配置
    				"MIMode": "gdb", // 指定使用gdb调试
    				"setupCommands": [
    					{
    						"description": "Enable pretty-printing for gdb",
    						"text": "-enable-pretty-printing",
    						"ignoreFailures": true
    					}
    				]
    			},
    			"windows": { // windows的特定配置
    				"type": "cppvsdbg", // 指定使用msvc进行调试
    				"program": "${workspaceFolder}/build/Debug/Demo.exe", // 指定C/C++程序位置
    			}
    		}
    	]
    }

快捷键

Ctrl-Shift-B: 执行Task

参考

  1. https://www.cnblogs.com/iwiniwin/p/13705456.html