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 百度一下html

mingw安裝

把 codeblocks的安裝文件的後綴名改成 .rar,而後解壓,獲得 MinGW
把 MinGW 複製到非中文無空格目錄
修改 MinGW 爲小寫
把 mingw32-make.exe 複製一份,更名爲 make.exec++

把 mingw/bin 添加到環境變量redis

驗證shell

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 <http://gnu.org/licenses/gpl.html>
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 <http://gnu.org/licenses/gpl.html>
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:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".

C:\Users\Administrator>

cmake 安裝

解壓cmake
把 cmake/bin 添加到環境變量json

驗證架構

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>

建立項目

非中文無空格目錄app

目錄結構

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 <stdio.h>

#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便可一鍵構建、運行[調試]ui

參考

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

launch.jsonspa

{
    "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
        }
    ]
}
相關文章
相關標籤/搜索