vscode windows cmake mingw

软件准备

codeblocks https://jaist.dl.sourceforge....
cmake 【下载解压版】 https://cmake.org/download/
cmake官方下载非常非常的慢,可以下载我百度网盘的3.3.1版
链接: https://pan.baidu.com/s/1sMGO... 密码: dt96
vscode 百度一下

mingw安装

把 codeblocks的安装文件的后缀名改为 .rar,然后解压,得到 MinGW
把 MinGW 复制到非中文无空格目录
修改 MinGW 为小写
把 mingw32-make.exe 复制一份,改名为 make.exe

把 mingw/bin 添加到环境变量

验证

gcc -v
make -v
gdb -v
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\Administrator>gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=D:/program/cpp/mingw/bin/../libexec/gcc/mingw32/5.1.0/lto-wrapper.exe
Target: mingw32
Configured with: ../../../src/gcc-5.1.0/configure --build=mingw32 --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable
-libgomp --enable-lto --enable-graphite --enable-libstdcxx-debug --enable-threads=posix --enable-version-specific-runtime-libs -
-enable-fully-dynamic-string --enable-libstdcxx-threads --enable-libstdcxx-time --with-gnu-ld --disable-werror --disable-nls --d
isable-win32-registry --disable-symvers --enable-cxx-flags='-fno-function-sections -fno-data-sections -DWINPTHREAD_STATIC' --pre
fix=/mingw32tdm --with-local-prefix=/mingw32tdm --with-pkgversion=tdm-1 --enable-sjlj-exceptions --with-bugurl=http://tdm-gcc.td
ragon.net/bugs
Thread model: posix
gcc version 5.1.0 (tdm-1)

C:\Users\Administrator>make -v
GNU Make 3.82.90
Built for i686-pc-mingw32
Copyright (C) 1988-2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

C:\Users\Administrator>gdb -v
GNU gdb (GDB) 7.9.1
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
.
Find the GDB manual and other documentation resources online at:
.
For help, type "help".
Type "apropos word" to search for commands related to "word".

C:\Users\Administrator>

cmake 安装

解压cmake
把 cmake/bin 添加到环境变量

验证

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\Administrator>cmake --version
cmake version 3.3.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

C:\Users\Administrator>

创建项目

非中文无空格目录

目录结构

F:\code\vscode\chello>tree /f
卷 code 的文件夹 PATH 列表
卷序列号为 0FDD-169A
F:.
│  build.bat
│  CMakeLists.txt
│
├─.vscode
│      launch.json
│      tasks.json
│
├─include
│      tool.h
│
└─src
        CMakeLists.txt
        main.c
        tool.c


F:\code\vscode\chello>

源码

顶层CMakeLists.txt

# 指定最小版本
cmake_minimum_required (VERSION 3.3)
# 指定工程名和语言类型
project (hello C)
# debug
set(CMAKE_BUILD_TYPE "Debug")
# c99
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -g")
# 添加子目录
add_subdirectory(src)

子目录 CMakeLists.txt

# 头文件目录
include_directories(${PROJECT_SOURCE_DIR}/include)
# 源文件列表
aux_source_directory(. SRCS)
# 可执行文件目录
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
# 可执行文件
add_executable(hello ${SRCS})

tool.h

#ifndef TOOL_H
#define TOOL_H

int sum( int a, int b);

#endif

tool.c

#include "../include/tool.h"

int sum( int a, int b){
    return a + b;

    
}

main.c

#include 

#include "../include/tool.h"


int main(int argc, char const *argv[])
{
    int a = 1;


    int b = 2;
    printf("%d + %d = %d\n",a,b,sum(a,b));
    a = 3;
    a = 4;
    a = 5;
    a = 6;
    
    a = 7;
    a = 8;
    getchar();
    return 0;
}

vscode 配置文件

launch.json

{
    "version": "0.2.0",
    "configurations": [

        {
            "name": "C++ Launch (GDB)",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/bin/hello", // 可执行文件位置
            "miDebuggerPath":"D:/program/cpp/mingw/bin/gdb.exe", // gdb 位置
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "preLaunchTask": "compile", // task.json 中的 label
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "compile",
            "type": "shell",
            "command": "build.bat",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

build.bat

if not exist build md build
cd build
cmake -G "MinGW Makefiles" ..
make

使用

F5即可一键构建、运行[调试]

参考

https://www.2cto.com/kf/20160...

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Launch (GDB)",                 // 配置名称,将会在启动配置的下拉菜单中显示
            "type": "cppdbg",                           // 配置类型,这里只能为cppdbg
            "request": "launch",                        // 请求配置类型,可以为launch(启动)或attach(附加)
            "launchOptionType": "Local",                // 调试器启动类型,这里只能为Local
            "targetArchitecture": "x86",                // 生成目标架构,一般为x86或x64,可以为x86, arm, arm64, mips, x64, amd64, x86_64
            "program": "${file}.exe",                   // 将要进行调试的程序的路径
            "miDebuggerPath":"a:\\MinGW\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
            "args": ["blackkitty",  "1221", "# #"],     // 程序调试时传递给程序的命令行参数,一般设为空即可
            "stopAtEntry": false,                       // 设为true时程序将暂停在程序入口处,一般设置为false
            "cwd": "${workspaceRoot}",                  // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录
            "externalConsole": true,                    // 调试时是否显示控制台窗口,一般设置为true显示控制台
            "preLaunchTask": "g++"                    // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
        }
    ]
}

你可能感兴趣的:(vscode windows cmake mingw)