How to make visual studio code run vcvarsx86_arm64.bat instead of vcvarsall.bat directly?
Asked Answered
C

2

1

I was trying to build my c++ program using MSVC using VSC. I used to do this by executing the vcvarsx86_arm64.bat and then use the command.

cl /EHsc /Z7 /W4 /Fe: ${fileDirname}.exe *.cpp

this works just fine. But I do wanna try to do it in the VSC way. I checked the Microsoft document and built the c_cpp_properties as below.

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.18362.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}

I was using the following build task for testing purpose:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "cl.exe build active file",
            "command": "echo hi",
            "group": "build"
        }
    ]
}

And here is the error message I got

> Executing task: echo hi <

[ERROR:vcvarsall.bat] Invalid argument found : /d
[ERROR:vcvarsall.bat] Invalid argument found : /c
[ERROR:vcvarsall.bat] Invalid argument found : echo
[ERROR:vcvarsall.bat] Invalid argument found : hi
[ERROR:vcvarsall.bat] Error in script usage. The correct usage is:
Syntax:
    vcvarsall.bat [arch] [platform_type] [winsdk_version] [-vcvars_ver=vc_version] [-vcvars_spectre_libs=spectre_mode]
where :
    [arch]: x86 | amd64 | x86_amd64 | x86_arm | x86_arm64 | amd64_x86 | amd64_arm | amd64_arm64     
    [platform_type]: {empty} | store | uwp
    [winsdk_version] : full Windows 10 SDK number (e.g. 10.0.10240.0) or "8.1" to use the Windows 8.1 SDK.
    [vc_version] : {none} for latest installed VC++ compiler toolset |
                   "14.0" for VC++ 2015 Compiler Toolset |
                   "14.xx" for the latest 14.xx.yyyyy toolset installed (e.g. "14.11") |
                   "14.xx.yyyyy" for a specific full version number (e.g. "14.11.25503")
    [spectre_mode] : {none} for libraries without spectre mitigations |
                     "spectre" for libraries with spectre mitigations

The store parameter sets environment variables to support Universal Windows Platform application
development and is an alias for 'uwp'.

For example:
    vcvarsall.bat x86_amd64
    vcvarsall.bat x86_amd64 10.0.10240.0
    vcvarsall.bat x86_arm uwp 10.0.10240.0
    vcvarsall.bat x86_arm onecore 10.0.10240.0 -vcvars_ver=14.0
    vcvarsall.bat x64 8.1
    vcvarsall.bat x64 store 8.1

Please make sure either Visual Studio or C++ Build SKU is installed.

I've seen the same error message when I call vcvarsall.bat directly. So I think vcvarsall.bat is being called somewhere. But I don't know how I can fix it. I did some research but did not find anything.

Cropland answered 23/11, 2019 at 21:34 Comment(0)
B
2

I've stumbled on a similar variant of this problem recently and the only way I found to circumvent it was to configure the shell options on the tasks.json. Like this:

{
    "version": "2.0.0",
    "windows": {
        "options": {
            "shell": {
                "executable": "cmd.exe",
                "args": [
                    "/d", "/c", "Your\\Path\\To\\vcvarsx86_arm64.bat", "&" 
                ]
            }
        }
    },
    "tasks": [
        {
            "label": "Build",
            "type": "shell",
            "command": "echo write your build command here"
        }
    ]
}
Brethren answered 9/5, 2020 at 0:13 Comment(1)
Nice, most likely the path will contain spaces so don't forget to surround with escaped quotes: "\"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat\""Ugo
D
0

I'd like to add to Ikeike's answer above by noting that, if you don't want to override the shell that is used for all tasks in your file, you can adapt the method to just call cmd.exe from your current shell (in my case, Powershell). For example, my task looks like:

    "label": "Configure (CMake, Ninja)",
    "type": "shell",
    "options":
    {
        "cwd": "${workspaceFolder}\\build"
    },
    "command": "cmd.exe",
    "args":
    [
        "/d",
        "/c",
        "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Professional\\VC\\Auxiliary\\Build\\vcvars64.bat",
        "'&'",
        "cmake",
        "-G", "Ninja",
        ".."
    ]
}

Powershell required that I quoted the &, but the rest was straightforward.

Dicarlo answered 19/7, 2021 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.