Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

c - What is the default generator for CMake in Windows?

When running CMake on one PC, CMake generates NMake files by default. On another, it generates a Visual Studio project.

I know I can override the default by adding -G "NMake Makefiles" to the end of my CMake statement, but I want to know why it defaults to Visual Studio projects on one and NMake files on another.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following is from the CMake Source (version 2.8.4: cmake.cxx: starting line 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() << "
";

It appears that CMake looks at the Windows Registry to determine which generator to use. It searches the Visual Studio registry subkeys (6.0, 7.0, etc) in [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\ for an entry called InstallDir. If one is found, it uses the corresponding generator. (It will use the newest version of Visual Studio available.) Otherwise, it uses the NMake generator.

Note that the InstallDir entry is not always present, even when a particular version of Visual Studio is installed. This may have to do with installation settings or a particular version of Visual Studio (e.g. it seems that the "Express" versions of Visual C++ do not add this entry.)

It is, of course, possible to override the default setting by appending -G {Generator Name} to the end of your CMake command.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...