How to run Cmake in Visual Studio Code using tasks
Asked Answered
K

3

9

I'm trying to run cmake with ctrl+shift+B like so:

{
"version": "2.0.0",
"tasks": [
    {
        "label": "cmake",
        "type": "shell",
        "options": {
            "cwd": "${workspaceRoot}/build"
        },
        "command": "cmake ${workspaceRoot} -G \"MinGW Makefiles\"",
        (...)
    },
    {
        "label": "make",
        "type": "shell",
        "command": "mingw32-make.exe",
        "options": {
            "cwd": "${workspaceRoot}/build"
        },
       (...),
        "dependsOn":["cmake"]
    },
    {
        "label": "build",
        "type": "shell",
        "options": {
            "cwd": "${workspaceRoot}/build"
        },
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "dependsOn": ["make"]
    }
]
}

But no matter what I do It runs on ${workspaceRoot} instead of the ${workspaceRoot}/build one:

Executing task in folder cpppractice: cmake C:\workspace\cpp\cpppractice -G "MinGW Makefiles"

Is there anyhting wrong with this approach? As far as I understand the cwd variable in the options item should work.

Kight answered 31/3, 2018 at 5:19 Comment(1)
why do this when the CMake Tools extension exists?Maggy
K
7
{
    "label": "cmake",
    "type": "shell",
    "options": {
        "cwd": "${workspaceFolder}/build"
    },
    "command": "cmake \"MinGW Makefiles\"  ${workspaceFolder}",
},

This works, It seems that the "Executing task in folder cpppractice:" is not accurate and it is executing it in the correct place, as for why it did not work previously I think it's parsing the args incorrectly? I can't confirm, but here is the output:

Executing task in folder cpppractice: cmake "MinGW Makefiles"  
C:\workspace\cpp\cpppractice <

-- Configuring done
-- Generating done
-- Build files have been written to: C:/workspace/cpp/cpppractice/build

Where previously it was compalining about not being able Use the generator "MinGW" which means that it separating the argument "MinGW Makefiles". After some tinkering I found out that this is also an answer:

{
        "label": "cmake",
        "type": "shell",
        "options": {
            "cwd": "${workspaceFolder}/build"
        },
        "command": "cmake",
        "args": [
            "-G",
            "'MinGW Makefiles'",
            "./.."
        ],
        ...
    },

I actually find the second approach a little bit cleaner but both work the same, So in order to pass an argument as a string you have to use single quotes like so:

...
"command":"echo",
"args": [
        "'Za Warudo!'",
    ],
...
Kight answered 31/3, 2018 at 17:36 Comment(0)
S
3

You pass arguments to cmake wrongly. The whole string cmake ${workspaceRoot} -G "MinGW Makefiles" is treated as a command name. Arguments must be listed in the args array.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "cmake",
            "type": "shell",
            "options": {
                "cwd": "${workspaceRoot}/build"
            },
            "command": "cmake",
            "args": [
                "${workspaceRoot}",
                "-G",
                "\"MinGW Makefiles\""
            ]
        },
        ...
    ]
}
Susannahsusanne answered 31/3, 2018 at 5:38 Comment(1)
I had already tried this, but the output is the same: Executing task in folder cpppractice: cmake C:\workspace\cpp\cpppractice -G "MinGW Makefiles"Kight
B
1

cmake now has a -B option that you can use to specify a build directory. No need to change directory before calling it. It will even create the build dir for you.

Bisutun answered 15/12, 2023 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.