Visual Studio解决方案/多个项目:如何有效地传播项目的性质在若干C++的项目

StackOverflow https://stackoverflow.com/questions/111631

我的工作与Visual Studio2005C++的解决方案,包括多个项目(大约30).根据我的经验,它常常变得令人讨厌维持的所有属性项目(i。e包括道路、lib的道路,联libs,代码生成的选择,...),因为你经常要点击每一个项目,以便修改他们。情况变得甚至更糟的是当你拥有多个配置("调试",释放,释放了64位,...).

现实生活的例子:

  • 假设你要用一个新的图书馆,你需要添加对包括路径图书馆的所有项目。你将如何避免有编辑性质的每一个每一个项目?
  • 假设你要试驾一个新版本的图书馆(说2.1版beta)所以,你需要迅速改变包括道路/路径图书馆/联图书馆的一组项目?

注:

  • 我知道,它可能选择多个项目在一段时间,然后作出正确的点击和选择"属性"。然而,这种方法只适用性已经完全相同的不同项目:你不能用它为了增加一个包括路径的一组项目,包括使用不同的路径。
  • 我也知道,它可能修改全球环境选项(工具/办法/项目和方案的/目录),但是这并不是说令人满意,因为它不能被综合成一个单
  • 我也知道一个可以添加"构造"一个解决方案。它没有帮助,因为它使得另一组项目的性质来维持
  • 我知道,codegear出现C++生成器2009年提供一个可行的回答这需要通过所谓的"选集",这可以继承的几个项目(I使用Visual Studio和C++的建设者,和我仍然认为C++建设者的岩石上的某些方面相比Visual Studio)
  • 我期待会有人建议"autconf"如CMake,但是它可能导vcproj文件这样的工具?
有帮助吗?

解决方案

我认为你需要调查的性质的文件,即 *.vsprops (老年)或 *.道具 (新)

你的 需要添加的属性文件手对每一个项目,但一旦这样做了,你有多个项目,而是一个。[vs]道具的文件。如果你改变的性质,所有项目的继承新设置。

其他提示

我经常需要做类似的东西因为我链接到静态的运行时的图书馆。我写了个程序来为我做的。它基本上扫描的所有子目录的任何路径你把它和ids任。vcproj文件,它的发现。然后一个接一个,它将打开它们进行修改和保存他们。因为我只有用它很少,道路是艰难的编码,但我认为你将能够调整它如何你喜欢的。

另一种办法是实现那些工作室的项目文件只是XML文件和可以操纵你最喜欢的XML类。我已经做了一些事情,使用C#'s XmlDocument 用于更新包括目录当有 很多 包括目录,我不想类型。:)

我是包括两个例子。你将需要修改它们自己的需要,但是这些应该让你开始。

这是C++版本:

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <boost/filesystem/convenience.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/regex.hpp>
#include <boost/timer.hpp>

using boost::regex;
using boost::filesystem::path;
using namespace std;

vector<path> GetFileList(path dir, bool recursive, regex matchExp);
void FixProjectFile(path file);
string ReadFile( path &file );
void ReplaceRuntimeLibraries( string& contents );
void WriteFile(path file, string contents);

int _tmain(int argc, _TCHAR* argv[])
{
    boost::timer stopwatch;
    boost::filesystem::path::default_name_check(boost::filesystem::native);
    regex projFileRegex("(.*)\\.vcproj");
    path rootPath("D:\\Programming\\Projects\\IPP_Decoder");

    vector<path> targetFiles = GetFileList(rootPath, true, projFileRegex);
    double listTimeTaken = stopwatch.elapsed();

    std::for_each(targetFiles.begin(), targetFiles.end(), FixProjectFile);

    double totalTimeTaken = stopwatch.elapsed();
    return 0;
}

void FixProjectFile(path file) {
    string contents = ReadFile(file);
    ReplaceRuntimeLibraries(contents);
    WriteFile(file, contents);
}

vector<path> GetFileList(path dir, bool recursive, regex matchExp) {
    vector<path> paths;
    try {
        boost::filesystem::directory_iterator di(dir);
        boost::filesystem::directory_iterator end_iter;
        while (di != end_iter) {
            try {
                if (is_directory(*di)) {
                    if (recursive) {
                        vector<path> tempPaths = GetFileList(*di, recursive, matchExp);
                        paths.insert(paths.end(), tempPaths.begin(), tempPaths.end());
                    }
                } else {
                    if (regex_match(di->string(), matchExp)) {
                        paths.push_back(*di);
                    }
                }
            }
            catch (std::exception& e) {
                string str = e.what();
                cout << str << endl;
                int breakpoint = 0;
            }
            ++di;
        }
    }
    catch (std::exception& e) {
        string str = e.what();
        cout << str << endl;
        int breakpoint = 0;
    }
    return paths;
}

string ReadFile( path &file ) {
//  cout << "Reading file: " << file.native_file_string() << "\n";
    ifstream infile (file.native_file_string().c_str(), ios::in | ios::ate);
    assert (infile.is_open());

    streampos sz = infile.tellg();
    infile.seekg(0, ios::beg);

    vector<char> v(sz);
    infile.read(&v[0], sz);

    string str (v.empty() ? string() : string (v.begin(), v.end()).c_str());

    return str;
}

void ReplaceRuntimeLibraries( string& contents ) {
    regex releaseRegex("RuntimeLibrary=\"2\"");
    regex debugRegex("RuntimeLibrary=\"3\"");
    string releaseReplacement("RuntimeLibrary=\"0\"");
    string debugReplacement("RuntimeLibrary=\"1\"");
    contents = boost::regex_replace(contents, releaseRegex, releaseReplacement);
    contents = boost::regex_replace(contents, debugRegex, debugReplacement);
}

void WriteFile(path file, string contents) {
    ofstream out(file.native_file_string().c_str() ,ios::out|ios::binary|ios::trunc); 
    out.write(contents.c_str(), contents.length());
}

这是C#版本。享受...

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace ProjectUpdater
{
    class Program
    {
        static public String rootPath = "D:\\dev\\src\\co\\UMC6\\";
        static void Main(string[] args)
        {
            String path = "D:/dev/src/co/UMC6/UMC.vcproj";
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(fs);
            XmlNodeList oldFiles = xmldoc.GetElementsByTagName("Files");
            XmlNode rootNode = oldFiles[0].ParentNode;
            rootNode.RemoveChild(oldFiles[0]);

            XmlNodeList priorNode = xmldoc.GetElementsByTagName("References");
            XmlElement filesNode = xmldoc.CreateElement("Files");
            rootNode.InsertAfter(filesNode, priorNode[0]);

            DirectoryInfo di = new DirectoryInfo(rootPath);
            foreach (DirectoryInfo thisDir in di.GetDirectories())
            {
                AddAllFiles(xmldoc, filesNode, thisDir.FullName);
            }


            List<String> allDirectories = GetAllDirectories(rootPath);
            for (int i = 0; i < allDirectories.Count; ++i)
            {
                allDirectories[i] = allDirectories[i].Replace(rootPath, "$(ProjectDir)");
            }
            String includeDirectories = "\"D:\\dev\\lib\\inc\\ipp\\\"";
            foreach (String dir in allDirectories) 
            {
                includeDirectories += ";\"" + dir + "\"";
            }

            XmlNodeList toolNodes = xmldoc.GetElementsByTagName("Tool");
            foreach (XmlNode node in toolNodes)
            {
                if (node.Attributes["Name"].Value == "VCCLCompilerTool") {
                    try
                    {
                        node.Attributes["AdditionalIncludeDirectories"].Value = includeDirectories;
                    }
                    catch (System.Exception e)
                    {
                        XmlAttribute newAttr = xmldoc.CreateAttribute("AdditionalIncludeDirectories");
                        newAttr.Value = includeDirectories;
                        node.Attributes.InsertBefore(newAttr, node.Attributes["PreprocessorDefinitions"]);
                    }

                }
            }
            String pathOut = "D:/dev/src/co/UMC6/UMC.xml";
            FileStream fsOut = new FileStream(pathOut, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
            xmldoc.Save(fsOut);

        }
        static void AddAllFiles(XmlDocument doc, XmlElement parent, String path) {
            DirectoryInfo di = new DirectoryInfo(path);
            XmlElement thisElement = doc.CreateElement("Filter");
            thisElement.SetAttribute("Name", di.Name);
            foreach (FileInfo fi in di.GetFiles())
            {
                XmlElement thisFile = doc.CreateElement("File");
                String relPath = fi.FullName.Replace(rootPath, ".\\");
                thisFile.SetAttribute("RelativePath", relPath);
                thisElement.AppendChild(thisFile);
            }
            foreach (DirectoryInfo thisDir in di.GetDirectories())
            {
                AddAllFiles(doc, thisElement, thisDir.FullName);
            }
            parent.AppendChild(thisElement);
        }
        static List<String> GetAllDirectories(String dir)
        {
            DirectoryInfo di = new DirectoryInfo(dir);
            Console.WriteLine(dir);

            List<String> files = new List<String>();
            foreach (DirectoryInfo subDir in di.GetDirectories())
            {
                List<String> newList = GetAllDirectories(subDir.FullName);
                files.Add(subDir.FullName);
                files.AddRange(newList);
            }
            return files;
        }
        static List<String> GetAllFiles(String dir)
        {
            DirectoryInfo di = new DirectoryInfo(dir);
            Console.WriteLine(dir);

            List<String> files = new List<String>();
            foreach (DirectoryInfo subDir in di.GetDirectories())
            {
                List<String> newList = GetAllFiles(subDir.FullName);
                files.AddRange(newList);
            }
            foreach (FileInfo fi in di.GetFiles())
            {
                files.Add(fi.FullName);
            }
            return files;
        }
    }
}

作为建议,你应该看看酒店床单(又名。vsprops文件)。
我写了一个非常简短介绍到这个功能 在这里,.

是的,我肯定会议使用 CMake.CMake是最好的工具(我认为我已经试过所有的),这可以产生工作室的项目文件。

我也有问题的转换现有的。vcproj到的文件CMakeLists.txt和我写了一个 红宝石-脚本 这需要照顾的大部分转换。脚本不会处理的事情,就像后建立的步骤和这样,因此某些调整是必要的,但它将保存你的麻烦拉的所有源文件的名称。vcproj文件。

*.vcxproj文件msbuild文件。所以你只是把一个你不想在所有项目文件并将其删除。然后把它放在你的酒店片。然后确定所有的项目文件正常进口的财产表。

这可以令人难以置信的繁琐为数以百计的文件。我写的一个工具,使这种互动:

https://github.com/chris1248/MsbuildRefactor

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top