Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Cmake in Visual Studio Code using tasks

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.

like image 694
Immac Avatar asked Mar 31 '18 05:03

Immac


People also ask

How do I run CMake from VS Code?

Install the C/C++ extension by searching for 'c++' in the Extensions view (Ctrl+Shift+X). CMake Tools extension for VS Code. Install the CMake Tools extension by searching for 'CMake tools' in the Extensions view (Ctrl+Shift+X). You'll also need to install CMake, a compiler, a debugger, and build tools.

How do I run a task in VS Code?

Tip: You can run your task through Quick Open (Ctrl+P) by typing 'task', Space and the command name.

How do I open CMake in Visual Studio?

On the Visual Studio main menu, choose File > Open > CMake. Navigate to the CMakeLists. txt file in the root of the bullet3 repo you just downloaded. As soon as you open the folder, your folder structure becomes visible in the Solution Explorer.


2 Answers

{
    "label": "cmake",
    "type": "shell",
    "options": {
        "cwd": "${workspaceRoot}/build"
    },
    "command": "cmake \"MinGW Makefiles\"  ${workspaceRoot}",
},

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": "${workspaceRoot}/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!'",
    ],
...
like image 197
Immac Avatar answered Nov 09 '22 00:11

Immac


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\""
            ]
        },
        ...
    ]
}
like image 31
273K Avatar answered Nov 09 '22 00:11

273K