Win 7 下 C++ 编译环境 MSYS2 MinGW 64-bit + Visual Studio Code

发布时间:2019-08-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Win 7 下 C++ 编译环境 MSYS2 MinGW 64-bit + Visual Studio Code脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

1. MSys2

国内使用中科大的,详见:https://lug.ustc.edu.cn/wiki/...

pacman -s mingw-w64-x86_64-toolchain

1.1 踩过的坑

初始直接安装

pacman -S gcc

而gcc -version 后发现是 7.4 而非 8.2 的。
后使用指令后版本变成 8.2 了。

pacman -S mingw-w64-x86_64-gcc

但后来 GDB 时还需要手动安装一下。若空间足够,可以手动安装。

pacman -S mingw-w64-x86_64-gdb

2. 使用 make 跑一下

test.cpp

#include<iostream>

int main(void) {
    long int tag = __cplusplus;
    if(tag == 201703L) std::cout << "C++17n";
    else if(tag == 201402L) std::cout << "C++14n";
    else if(tag == 201103L) std::cout << "C++11n";
    else if(tag == 199711L) std::cout << "C++98n";
    else std::cout << "PRe-standard C++n";
    
    return 0;
}

makefile

CPPFLAGS=-Wall -std=c++17
all:test
clean:
    rm test.exe

结果:

$ ./test.exe
C++17

3. 使用 Visual Studio Code

3.1 安装扩展:C/C++

3.2 新建文件夹

File -> Open Folder, Create a new Folder 'test'
create a new file test.cpp, the same as 2.

3.3 配置文件

create a dir .vscode wITh 3 files.
c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "c:\msys64\mingw64\include",
                "c:\msys64\mingw64\lib\gcc\x86_64-w64-mingw32\8.2.1\include",
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\msys64\mingw64\bin\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "IntelliSenSEMode": "clang-x64"
        }
    ],
    "version": 4
}

用于编译的 tasks
tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "g++",
            "type": "shell",
            "command": "g++",
            "args": [
                "${file}", "-g", "-o", "${fileDirname}/${fileBasenameNoExtension}.exe", "-std=c++17"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

用于运行加载的launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "c:\msys64\mingw64\bin\gdb.exe",
            "SETUPCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++"
        }
    ]
}

4. 编译

快捷键:ctrl+shift+B

鼠标的用法:
先选中 test.cpp
再Terminal -- Run Task -- g++ -- Continue...
Terminal 中会有编译成功的消息。

5. 运行 F5

会出现输出结果
C++17

脚本宝典总结

以上是脚本宝典为你收集整理的Win 7 下 C++ 编译环境 MSYS2 MinGW 64-bit + Visual Studio Code全部内容,希望文章能够帮你解决Win 7 下 C++ 编译环境 MSYS2 MinGW 64-bit + Visual Studio Code所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。