【C++】vscode+cmake+MinGW开发环境搭建

msys2和mingw适用于windows系统,其余vscode配置可通用win和linux系统

安装vscode

安装cmake

配置环境变量
(略)

MSYS2

安装MSYS2

msys2 exe下载地址
清华镜像站

替换软件源

  1. 打开Msys2-MinGW64终端
  2. 执行如下命令,将MSYS2软件源换成清华国内源
sed -i "s#mirror.msys2.org/#mirrors.tuna.tsinghua.edu.cn/msys2/#g" /etc/pacman.d/mirrorlist*

安装MinGW64环境

  1. 打开MSYS2-MinGW64终端
  2. 执行如下命令,安装完整的MinGW64环境
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
  1. 终端执行
gcc -v
g++ -v
gdb -v

如果显示了完整的版本信息,则说明安装成功
参考链接



创建vscode的c++空间

编写CMakeLists.txt


CMakeLists.txt简易教程

编写代码

配置vscode

预备环境

  1. 目录结构
${workspaceFolder} ##vscode打开的目录
 ├ .vscode ##vscode配置目录
 │    ├ tasks.json
 │    └ launch.json
 ├ build  ##cmake构建目录
 ├ CMakeLists.txt
 ├ main.cpp
 ├ src.cpp
 └ src.h
  1. CMakeLists.txt内容
project(prj)
aux_source_directory(${CMAKE_SOURCE_DIR} CPP_FILE)
add_executable(out ${CPP_FILE})

CMakeLists.txt需要根据实际情况进行编写

  1. 生成编译步骤
  • 清理build目录
  • 在build目录中执行cmake … -G “MinGW Makefiles”。指定CMake按MinGW模式生成Makefile
  • 使用MinGW64的make命令进行编译

配置tasks.json文件

{
    "tasks": [
        {
            "type": "shell",
            "detail": "清理build目录",
            "label": "Clear Build",
            "command": "rm",
            "windows": {
                "args": [
                    "-r",
                    "*"
                ],
                "options": {
                    "cwd": "${workspaceFolder}/build",
                }
            },
            "linux": {}
        },
        {
            "dependsOn":[
                "Clear Build"
            ],
            "type": "shell",
            "detail": "执行CMake命令",
            "label": "CMake",
            "command": "cmake",
            "args": [
                "..",
                "-G",
                "MinGW Makefiles"
            ],
            "options": {
                "cwd": "${workspaceFolder}/build"
            },
        },
        {
            "dependsOn":[
                "CMake"
            ],
            "type": "shell",
            "detail": "执行Make",
            "label": "Run Make",
            "command": "mingw32-make.exe",
            "args": [
                "-j4"
            ],
            "options": {
                "cwd": "${workspaceFolder}/build",
            }
        }
    ],
    "version": "2.0.0"
}
  1. 配置项项说明
  • task节点下,一共创建了3个json object,代表了3个步骤
  • label: 任务名称
  • type:任务执行的类型,shell表示当前任务是在终端中执行
  • detail:说明,可省略
  • command: 命令
  • args: command命令的参数列表
  • options: 选项
  • options/cwd: 执行命令的路径
  • dependOn:前置任务(填写对应任务label)

  1. 实际分析
    2.1 tasks.json中设置了3个任务,分别是"Clear Build",“CMake”,“Run Make”
    2.2 在"Clear Build"中,部分配置项写在了"windows" 内,为的是进行操作系统的区分
    2.3 dependOn ,通过设置前置任务,可以用来设置任务的执行顺序。案例中的顺序就是"Clear Build" -> “CMake” -> “Run Make”

配置launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Start",
            "type": "cppdbg",
            "request": "launch",
            "cwd": "${workspaceFolder}/build",
            "program": "${workspaceFolder}/build/out.exe",
            "preLaunchTask": "Run Make"
        }
    ]
}

配置项说明:

  • name:启动项目名称
  • type:启动类型,cppdbg调试c++启动。(cppbuild等)
  • cwd:启动执行路径
  • program:启动的二进制文件路径
  • preLaunchTask:启动前对应执行的tasks.json配置的任务label

按键F5启动

你可能感兴趣的:(c++,vscode,开发语言)