8

I remember in the ol' days getting people to run vcvarsall.bat before compiling anything on Windows. Assuming default install location, where do I find cl.exe, link.exe, and friends; if it's not in the PATH?

Attempt:

#include <stdlib.h>
#include <stdio.h>

#include <Windows.h>

const inline LPCWSTR find_cl() {
    /* Most of this list derived from 
       https://gitlab.kitware.com/cmake/cmake/-/blob/417b765f/Modules/GetPrerequisites.cmake#L670 */
    static const LPCSTR cl_paths[12] = {
        "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin",
        "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\bin",
        "C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\VC\\bin",
        "C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\bin",
        "C:\\Program Files\\Microsoft Visual Studio 9.0\\VC\\bin",
        "C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\VC\\bin",
        "C:\\Program Files\\Microsoft Visual Studio 8\\VC\\BIN",
        "C:\\Program Files (x86)\\Microsoft Visual Studio 8\\VC\\BIN",
        "C:\\Program Files\\Microsoft Visual Studio .NET 2003\\VC7\\BIN",
        "C:\\Program Files (x86)\\Microsoft Visual Studio .NET 2003\\VC7\\BIN",
        "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.25.28610\\bin\\Hostx86\\x64",
        "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.25.28610\\bin\\Hostx86\\x86",
    };

    size_t i;
    for (i = 0; i < sizeof cl_paths / sizeof cl_paths[0]; i++)
        if (_access(cl_paths[i], 0) == 0)
            return cl_paths[i];
    return NULL;
}

With a trivial entrypoint:

int main(void) {
    const LPCWSTR cl_path = find_cl();

    if (cl_path == NULL)
        return EXIT_FAILURE;

    printf("Found cl_path of: \"%s\"\n", cl_path);
    return EXIT_SUCCESS;
}

I'll detect the architecture also, so will look for the compiler matching current architecture first, then fallback to other architectures. I'm assuming there's a list of 30+ variants of these somewhere?

Samuel Marks
  • 1,611
  • 1
  • 20
  • 25
  • 2
    I'm curious about why you need this? What is the actual problem you need to solve? If you need a command prompt setup for development using Visual Studio, why not use the "Developer command prompt" shortcut? – Some programmer dude May 02 '20 at 04:45
  • There should be links to open command prompts with the correct paths in the MSVC start menu entry. – Shawn May 02 '20 at 04:45
  • @Someprogrammerdude I'm querying the system to see `if` it has any MSVC version installed, on the `else` it will install the latest community MSVC version. Then it uses `cl.exe` and `link.exe` to do some other tasks (later). – Samuel Marks May 02 '20 at 04:59
  • The start menu entries are simple batches doing what `vcvarsall.bat` did, with some differences according to the target. – the busybee May 02 '20 at 07:11
  • So you're suggesting I look through the registry? – Samuel Marks May 02 '20 at 07:17
  • 2
    https://learn.microsoft.com/en-us/visualstudio/extensibility/locating-visual-studio?view=vs-2019 – Hans Passant May 02 '20 at 14:29
  • @HansPassant They don't support older versions, but seem useful for newer versions. Maybe if I ask on [their github](https://github.com/microsoft/vswhere) they'll have a nice big list of registry keys and paths to give me. – Samuel Marks May 04 '20 at 00:39

3 Answers3

4

You can find cl.exe and link.exe with your Visual Studio install. This will be in Program Files (x86) -> Microsoft Visual Studio -> <year> -> BuildTools -> VC -> Tools -> MSVC -> <version> -> bin -> Hostx64 -> x64

Replace <year> and <version> here with the appropriate year and version and Hostx64 and x64 might instead be Hostx86 and/or x86 depending on whether you're working on 32 or 64 bit, but in 2022, you really should be working with 64 bits systems.

Kelvin Bouma
  • 179
  • 1
  • 2
  • 12
1

The current path is <install location>\Microsoft Visual Studio\<year>\Community\VC\Tools\MSVC\<version>\bin\Hostx<bitness>\x<bitness>.

For example, on a 64 bit system running MSVS 2022 with MSVC 14.34.31933 with MSVS installed to Program Files (which is the default location) under C: it should be C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x64.

Aaron Liu
  • 361
  • 2
  • 13
0

vswhere

Since VisualStudio 2015.2, the vswhere tool is installed and can be used to find the information.

The tool is

installed in

%ProgramFiles(x86)%\Microsoft Visual Studio\Installer

(use %ProgramFiles% in a 32-bit program prior to Windows 10).

This is a fixed location that will be maintained.

https://github.com/Microsoft/vswhere/wiki/Installing

Now, having located VisualStudio, use one of these two scripts to give you the dev environment in a shell

vsdevcmd.bat

https://renenyffenegger.ch/notes/Windows/dirs/Program-Files-x86/Microsoft-Visual-Studio/version/edition/Common7/Tools/VsDevCmd_bat

VsDevCmd -arch=x64

vcvarsall.bat

https://renenyffenegger.ch/notes/Windows/dirs/Program-Files-x86/Microsoft-Visual-Studio/version/edition/VC/Auxiliary/Build/vcvarsall_bat

This looks to be practically identical to vsdevcmd.bat, but it has many more command line options which may be hard to figure out and the command is more obscure. So I suggest using vsdevcmd and not this.

user7610
  • 25,267
  • 15
  • 124
  • 150