Windows中CMake的默认生成器是什么?

在一台PC上运行CMake时,CMake默认生成NMake文件。 另一方面,它生成一个Visual Studio项目。

我知道我可以通过在我的CMake语句的末尾添加-G "NMake Makefiles"来覆盖默认值,但我想知道为什么它默认为一个上的Visual Studio项目和另一个上的NMake文件。

以下内容来自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生成器。

请注意,即使安装了特定版本的Visual Studio,也不会始终显示InstallDir条目。 这可能与安装设置或特定版本的Visual Studio有关(例如,似乎Visual C ++的“Express”版本不会添加此条目。)

当然,可以通过将-G {Generator Name}附加到CMake命令的末尾来覆盖默认设置。