Win 7 下 C++ 編譯環(huán)境 MSYS2 MinGW 64-bit + Visual Studio Code

1. MSYS2

國(guó)內(nèi)使用中科大的源,詳見:https://lug.ustc.edu.cn/wiki/mirrors/help/msys2

pacman -S mingw-w64-x86_64-toolchain

1.1 踩過的坑

初始直接安裝

pacman -S gcc

而gcc -version 后發(fā)現(xiàn)是 7.4 而非 8.2 的。
后使用指令后版本變成 8.2 了。

pacman -S mingw-w64-x86_64-gcc

但后來 gdb 時(shí)還需要手動(dòng)安裝一下。若空間足夠,可以手動(dòng)安裝。

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++17\n";
    else if(tag == 201402L) std::cout << "C++14\n";
    else if(tag == 201103L) std::cout << "C++11\n";
    else if(tag == 199711L) std::cout << "C++98\n";
    else std::cout << "pre-standard C++\n";
    
    return 0;
}

makefile

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

結(jié)果:

$ ./test.exe
C++17

3. 使用 Visual Studio Code

3.1 安裝擴(kuò)展: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
            }
        }
    ]
}

用于運(yùn)行加載的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

鼠標(biāo)的用法:
先選中 test.cpp
再Terminal -- Run Task -- g++ -- Continue...
Terminal 中會(huì)有編譯成功的消息。

5. 運(yùn)行 F5

會(huì)出現(xiàn)輸出結(jié)果
C++17

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容