c++windowscmakeg++

Force VSCode+MSYS2 UCRT64 using g++ instead of vs cl.exe


I installed VSCode, MSYS2 UCRT64GCC and cmake in it. I specified the compiler with the following code:

CMakelists.txt:

cmake_minimum_required(VERSION 3.10)
set(CMAKE_C_COMPILER "D:/msys64/ucrt64/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "D:/msys64/ucrt64/bin/g++.exe")
project(MyCppProject)

tasks.json:

            "label": "cmake-configure",
        "type": "shell",
        "command": "cmake",
        "args": [
            "-S", "${workspaceFolder}",
            "-B", "${workspaceFolder}/build",
            "-DCMAKE_C_COMPILER=D:/msys64/ucrt64/bin/gcc.exe",
            "-DCMAKE_CXX_COMPILER=D:/msys64/ucrt64/bin/g++.exe"
        ],

If I use Ctrl+Shift+P to call the CMake: Configure and CMake: Build, it will auto detect the vs cl.exe compiler rather than g++.

enter image description here

enter image description here

Only when I manually execute cmake -S . -B build and cmake --build build in the cppdbg terminal, it could compile with g++. enter image description here

Although I'm glad with this manual command calling, in some other PC, I can't even find this cppdbg terminal — there is only the powershell. How can I open it on the other PC?


Solution

  • I find the trick, the default `build` schema is there:

    C:\Users\username\AppData\Roaming\Code\User\tasks.json

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "build",
                "type": "shell",
                "command": "cl.exe",
                "args": [
                    "/EHsc",
                    "/Zi",
                    "${file}",
                    "/Fe:${fileDirname}\\${fileBasenameNoExtension}.exe"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "problemMatcher": ["$msCompile"]
            }
        ]
    }