cmake: How to set visual studio intermediate directory?

I googled this question, these’s no easy way. Cmake does not provide any variable or property to modify visual studio intermediate dierectory. So I modified cmake source code to implement the feature.I’m using VS2005, and I test the modification under VS2005. It just work.

NOTE: New property VS_INTERMEDIATE_DIRECTORY_(CONFIG) can be used to change VS intermediate directory. VS_INTERMEDIATE_DIRECTORY global variable is not supported.

  • Download source code

    Download the source code cmake-3.2.1, unzip to a folder.

  • Modify source code
    • file:: cmLocalVisualStudio7Generator.cxx
    • function:: void cmLocalVisualStudio7Generator::WriteConfiguration
    • modification::

      // The intermediate directory name consists of a directory for the
      // target and a subdirectory for the configuration name.
      std::string intermediateDir = this->GetTargetDirectory(target);
      #if 1 //modify for change intermediate directory
      std::string vsIntDirProp = std::string("VS_INTERMEDIATE_DIRECTORY_") + cmSystemTools::UpperCase(configName);
      const char* vsIntermediateDir = target.GetProperty(vsIntDirProp.c_str());
      if (vsIntermediateDir)
          intermediateDir = vsIntermediateDir;
          std::string tgtDir = target.GetName();
              intermediateDir += "/";
              intermediateDir += tgtDir;
           intermediateDir += "/";
           intermediateDir += configName;
      intermediateDir += "/";
      intermediateDir += configName;
  • Build source code

    Follow the cmake build guidline. Use your favorate compiler to build cmake binary. Of course, there must be a installed cmake version on your PC.

    cd E:\code\cmake-3.2.1
    mkdir vs8
    cd vs8
    cmake -G "Visual Studio 8 2005" ..

    Open Cmake.sln with vs2005 and build cmake project. Download my build work: cmake.exe (vc8-win32) Replace the installed version.


    foreach (config ${CMAKE_CONFIGURATION_TYPES})
        string (TOUPPER ${config} config)
        set_target_properties(${_project} PROPERTIES
            RUNTIME_OUTPUT_DIRECTORY_${config} ${bin_path}
            ARCHIVE_OUTPUT_DIRECTORY_${config} ${lib_path}
            LIBRARY_OUTPUT_DIRECTORY_${config} ${lib_path}
            PDB_OUTPUT_DIRECTORY_${config} ${sym_path}
            VS_INTERMEDIATE_DIRECTORY_${config} ${tmp_path}
    endforeach ()
  • References