我记得在旧的日子里,让人们在Windows上编译任何东西之前运行vcvarsall.bat
。假设默认安装位置,我在哪里可以找到cl.exe
,link.exe
和朋友;如果它不在PATH
中呢
尝试:
#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;
}
使用一个简单的入口点:
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;
}
我也会检测架构,所以我会先寻找与当前架构匹配的编译器,然后退回到其他架构。
2条答案
按热度按时间r9f1avp51#
您可以在Visual Studio安装中找到
cl.exe
和link.exe
。这将位于Program Files (x86) -> Microsoft Visual Studio -> <year> -> BuildTools -> VC -> Tools -> MSVC -> <version> -> bin -> Hostx64 -> x64
中将此处的和替换为适当的年份和版本,Hostx 64和x64可能会改为Hostx 86和/或x86,具体取决于您使用的是32位还是64位系统,但在2022年,您确实应该使用64位系统。
8wtpewkr2#
当前路径为
C:\Program Files\Microsoft Visual Studio\<year>\Community\VC\Tools\MSVC\<version>\bin\Hostx<bitness>\x<bitness>
。例如,在运行MSVS 2022和MSVC 14.34.31933的64位系统上,该路径应为C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x64