以下来自CMake Source(2.8.4版:cmake.cxx:起始行2039):
// Try to find the newest VS installed on the computer and
// use that as a default if -G is not specified
std::string vsregBase =
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\";
struct VSRegistryEntryName
{
const char* MSVersion;
const char* GeneratorName;
};
VSRegistryEntryName version[] = {
{"6.0", "Visual Studio 6"},
{"7.0", "Visual Studio 7"},
{"7.1", "Visual Studio 7 .NET 2003"},
{"8.0", "Visual Studio 8 2005"},
{"9.0", "Visual Studio 9 2008"},
{"10.0", "Visual Studio 10"},
{0, 0}};
for(int i =0; version[i].MSVersion != 0; i++)
{
std::string reg = vsregBase + version[i].MSVersion;
reg += ";InstallDir]";
cmSystemTools::ExpandRegistryValues(reg);
if (!(reg == "/registry"))
{
installedCompiler = version[i].GeneratorName;
}
}
cmGlobalGenerator* gen
= this->CreateGlobalGenerator(installedCompiler.c_str());
if(!gen)
{
gen = new cmGlobalNMakeMakefileGenerator;
}
this->SetGlobalGenerator(gen);
std::cout << "-- Building for: " << gen->GetName() << "\n";
CMake 似乎会查看 Windows 注册表以确定要使用的生成器。它在 [HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\ 中的 Visual Studio 注册表子项(6.0、7.0 等)中搜索名为 InstallDir 的条目。如果找到,则使用相应的生成器。 (它将使用可用的最新版本的 Visual Studio。)否则,它将使用 NMake 生成器。
请注意,InstallDir 条目并不总是存在,即使安装了特定版本的 Visual Studio。这可能与安装设置或 Visual Studio 的特定版本有关(例如,Visual C++ 的“Express”版本似乎没有添加此条目。)
当然,可以通过在 CMake 命令末尾附加 -G {Generator Name} 来覆盖默认设置。