Visual Studio ソリューション / 複数のプロジェクト:複数の C++ プロジェクト間でプロジェクト プロパティを効果的に伝播する方法

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

質問

複数のプロジェクト (約 30) を含む Visual Studio 2005 C++ ソリューションを使用しています。私の経験に基づくと、プロジェクトのすべてのプロパティ (つまり、パス、ライブラリ パス、リンクされたライブラリ、コード生成オプションなど) を維持するのは面倒になることがよくあります。これは、プロジェクトをすべてクリックする必要があることが多いからです。それらを変更します。複数の構成 (デバッグ、リリース、64 ビットのリリースなど) がある場合、状況はさらに悪化します。

実際の例:

  • 新しいライブラリを使用する必要があり、このライブラリへのインクルード パスをすべてのプロジェクトに追加する必要があるとします。プロジェクトごとにプロパティを編集する手間を省くにはどうすればよいでしょうか?
  • 新しいバージョンのライブラリ (バージョン 2.1 ベータなど) をテストして、一連のプロジェクトのインクルード パス/ライブラリ パス/リンクされたライブラリをすぐに変更する必要があると仮定します。

ノート:

  • 一度に複数のプロジェクトを選択し、右クリックして「プロパティ」を選択できることを認識しています。ただし、この方法は、異なるプロジェクトですでに完全に同一であるプロパティに対してのみ機能します。異なるインクルード パスを使用していた一連のプロジェクトにインクルード パスを追加するためにこれを使用することはできません。
  • また、環境オプション (ツール/オプション/プロジェクトとソリューション/ディレクトリ) をグローバルに変更できることも知っていますが、SCM に統合できないため、それほど満足のいくものではありません。
  • また、ソリューションに「構成」を追加できることも知っています。別のプロジェクトプロパティセットを維持する必要があるため、役に立ちません。
  • codegear C++ Builder 2009 は、いくつかのプロジェクトに継承できる、いわゆる「オプション セット」を通じて、このニーズに対する実行可能な答えを提供していることを知っています (私は Visual Studio と C++ Builder の両方を使用していますが、比較すると特定の点で C++ Builder が優れていると今でも思っています) Visual Studio に)
  • 誰かが CMake などの「autconf」を提案してくれると思いますが、vcproj ファイルをそのようなツールにインポートすることは可能ですか?
役に立ちましたか?

解決

プロパティファイルを調査する必要があると思います。 *.vsprops (年上)または *.props (最新)

あなた する 各プロジェクトにプロパティ ファイルを手動で追加する必要がありますが、それが完了すると、複数のプロジェクトが存在しますが、.[vs]props ファイルは 1 つだけになります。プロパティを変更すると、すべてのプロジェクトが新しい設定を継承します。

他のヒント

私は静的ランタイム ライブラリにリンクしているため、同様のことを行う必要があることがよくあります。私はそれを行うためのプログラムを書きました。基本的に、指定したパスのすべてのサブディレクトリをスキャンし、見つかった .vcproj ファイルの ID を取得します。次に、それらを 1 つずつ開いて変更し、保存します。滅多に使わないのでパスはハードコードしてありますが、お好みで調整できると思います。

もう 1 つのアプローチは、Visual Studio プロジェクト ファイルが単なる XML ファイルであり、好みの XML クラスで操作できることを認識することです。C#を使って何かをしたことがある 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 は、Studio プロジェクト ファイルを生成できる最高のツールです (私は実際にすべて試してみました)。

既存の .vcproj ファイルを CMakeLists.txt に変換するという問題もありました。 Rubyスクリプト これにより、変換の大部分が処理されます。このスクリプトはビルド後の手順などを処理しないため、多少の調整が必要ですが、.vcproj ファイルからすべてのソース ファイル名を取得する手間が省けます。

*.vcxproj ファイルは msbuild ファイルです。したがって、すべてのプロジェクト ファイル内で不要なプロパティを取得して削除するだけです。次に、それをプロパティシートに入力します。次に、すべてのプロジェクト ファイルがそのプロパティ シートを適切にインポートしていることを確認します。

何百ものファイルがある場合、これは非常に面倒な作業になる可能性があります。これをインタラクティブにするツールを書きました。

https://github.com/chris1248/MsbuildRefactor

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top