Visual Studio的.sln和.vcproj文件中的宏/环境变量

时间:2023-01-06 12:07:39

I have two similar problems:

我有两个类似的问题:

a) I have a solution which includes several projects and I want to be able easily switch project location by setting some environment variable/macro. As example this project can be located in \SolutionDir\Dir1\ or \SolutionDir\Dir2\ So, I want to specify that it should be located in \SolutionDir\$(Var) and just set the variable.

a)我有一个包含多个项目的解决方案,我希望通过设置一些环境变量/宏来轻松切换项目位置。例如,这个项目可以位于\ SolutionDir \ Dir1 \或\ SolutionDir \ Dir2 \中。所以,我想指定它应该位于\ SolutionDir \ $(Var)中,只需设置变量即可。

Is there any build in Visual Studio way to do it?

是否有以Visual Studio方式构建的方法?

I know currently only two solutions - edit .sln file manual/programmatically to find this project and set correct path.

我知道目前只有两个解决方案 - 编辑.sln文件手动/编程以查找此项目并设置正确的路径。

I wasn't able to use environment variable in .sln file.

我无法在.sln文件中使用环境变量。

b) I have a project which includes resources (.rc and .h) files. I want to be able to set their location through other environment variable or macro.

b)我有一个包含资源(.rc和.h)文件的项目。我希望能够通过其他环境变量或宏设置它们的位置。

Something like \ProjectDir\$(Var2)\resource.rc

像\ ProjectDir \ $(Var2)\ resource.rc之类的东西

I found some promising info on property sheets, but Visual studio doesn't expand macros when I am using them in File tag in the .vcproj.

我在属性表上找到了一些有希望的信息,但是当我在.vcproj中的File标签中使用它时,Visual Studio不会扩展宏。

Thank you for any ideas how to solve this problem.

感谢您提出如何解决此问题的任何想法。

Regards, Victor

5 个解决方案

#1


Just use the environment variable in the relevant field:

只需在相关字段中使用环境变量:

OutputDirectory="$(MyEnvVariableName)\Bin"

One trick is that you need to restart the Visual Studio IDE each time you change the variable.

一个技巧是每次更改变量时都需要重新启动Visual Studio IDE。

There is an MSDN article precisely about this: How to: Use Environment Variables in a Build

有一篇关于此的MSDN文章:如何:在构建中使用环境变量

#2


I think I have the same goal than you: I want to use environment variables to locate some projects in a solution file (.sln) and to use some environment variables to locate some files within my projects.

我想我的目标与你相同:我想使用环境变量在解决方案文件(.sln)中找到一些项目,并使用一些环境变量来查找项目中的一些文件。

I found a way to do that and it works fine for me (with Visual Studio 2005): - edit the .sln file with a text editor and use environment variables with the following syntax %MyEnvironmentVariable% - edit the .vcproj files and replace the path to the desired files with some variables, with the following syntax $(MyEnvironmentVariable).

我找到了一种方法,它适用于我(使用Visual Studio 2005): - 使用文本编辑器编辑.sln文件并使用具有以下语法的环境变量%MyEnvironmentVariable% - 编辑.vcproj文件并替换使用一些变量获取所需文件的路径,使用以下语法$(MyEnvironmentVariable)。

Hope it helps... Cyrille

希望它有所帮助...... Cyrille

#3


The best way to achieve what you describe in b) is to use property sheets. Check out also this very similar question.

实现b)中描述的最佳方法是使用属性表。查看这个非常相似的问题。

I found some promising info on property sheets, but Visual studio doesn't expand macros when I am using them in File tag in the .vcproj.

我在属性表上找到了一些有希望的信息,但是当我在.vcproj中的File标签中使用它时,Visual Studio不会扩展宏。

I am not sure what version of VS you use. VS2008 lets you define for example an include directory like this: "$(OpenCVInclude)\cxcore\include". I use it all the time. OpenCVInclude is a macro defined in a property sheet.

我不确定你使用的是什么版本的VS. VS2008允许您定义例如这样的包含目录:“$(OpenCVInclude)\ cxcore \ include”。我用它所有的时间。 OpenCVInclude是属性表中定义的宏。

As for question a), I think there is no "clean" way to do what you want. As an alternative you could the configuration manager:

至于问题a),我认为没有“干净”的方式来做你想要的。作为替代方案,您可以配置管理器:

  • Include all the projects in the solution.
  • 包括解决方案中的所有项目。

  • Name the project differently, for example based on the OEM.
  • 以不同方式命名项目,例如基于OEM。

  • For each project define release and debug configurations in the solution
  • 对于每个项目,在解决方案中定义发布和调试配置

  • In "Build->Configuration Manager" You can check or uncheck the "Build" column for each configuration. Check "build" for the relevant project.
  • 在“Build-> Configuration Manager”中,您可以选中或取消选中每个配置的“Build”列。检查相关项目的“构建”。

#4


I am not sure if you are building just C++ projects or if you are also building C#\VB projects, but one of the great things about Visual Studio is all of the projects are really just MSBuild projects. If you edit a project in a text editor you will see that at the end of the project it imports a .targets file. If you track down and find follow the imports you will find that almost all of the VS projects import Microsoft.Common.Targets. Microsoft.Common.Targets imports Custom.Before.Microsoft.Common.Targets. Using this import you can import your own targets file with your own custom actions.

我不确定你是在构建C ++项目还是构建C#\ VB项目,但是Visual Studio的一个好处就是所有的项目都只是MSBuild项目。如果在文本编辑器中编辑项目,您将看到在项目结束时导入.targets文件。如果你追踪并找到跟随导入,你会发现几乎所有的VS项目都导入了Microsoft.Common.Targets。 Microsoft.Common.Targets导入Custom.Before.Microsoft.Common.Targets。使用此导入,您可以使用自己的自定义操作导入自己的目标文件。

I for example have a target file that has a common property defined across all projects in a solution and a custom post build event that processes at the end of each project building.

例如,我有一个目标文件,它具有在解决方案中的所有项目中定义的公共属性,以及在每个项目构建结束时处理的自定义后期构建事件。

Using this extension method and by creating custom configurations in the solution besides just the standard release\debug, you should be able to create as complex of a build configuration as you need.

除了标准的release \ debug之外,使用此扩展方法并在解决方案中创建自定义配置,您应该能够根据需要创建构建配置的复杂。

#5


(a) Cyrille gives a decent solution to what you asked for. Another way is to keep your changeable settings in one common place. Note that this won't work for non-msbuild files like *.sln and *.vcproj (until VS 2010).

(a)Cyrille为您所要求的提供了一个体面的解决方案。另一种方法是将可更改的设置保存在一个公共位置。请注意,这不适用于* .sln和* .vcproj等非msbuild文件(直到VS 2010)。

Project files: ... $(ChangeableDir)\foo.cs

项目文件:... $(ChangeableDir)\ foo.cs

Common.targets: ChangeThis ...

Common.targets:ChangeThis ...

However, I don't think this is a great way to do things. If the differences between what you're building are large pieces of functionality, you should consider creating a branch in your source control system. OTOH, if the differences are minor -- eg hardcoded strings -- then this leads to your second question...

但是,我认为这不是一个很好的做事方式。如果您正在构建的内容之间的差异是大部分功能,则应考虑在源代码管理系统中创建分支。 OTOH,如果差异很小 - 例如硬编码字符串 - 那么这会导致你的第二个问题......

(b) The kind of resource management you describe is essentially the same problem faced by people localizing their project into different languages. Luckily, direct support is built into Visual Studio since 2005. Check out previous questions like: Localization in Visual Studio 2008

(b)您描述的资源管理类型与人们将项目本地化为不同语言所面临的问题基本相同。幸运的是,自2005年以来,直接支持内置于Visual Studio中。请查看以前的问题,例如:Visual Studio 2008中的本地化

#1


Just use the environment variable in the relevant field:

只需在相关字段中使用环境变量:

OutputDirectory="$(MyEnvVariableName)\Bin"

One trick is that you need to restart the Visual Studio IDE each time you change the variable.

一个技巧是每次更改变量时都需要重新启动Visual Studio IDE。

There is an MSDN article precisely about this: How to: Use Environment Variables in a Build

有一篇关于此的MSDN文章:如何:在构建中使用环境变量

#2


I think I have the same goal than you: I want to use environment variables to locate some projects in a solution file (.sln) and to use some environment variables to locate some files within my projects.

我想我的目标与你相同:我想使用环境变量在解决方案文件(.sln)中找到一些项目,并使用一些环境变量来查找项目中的一些文件。

I found a way to do that and it works fine for me (with Visual Studio 2005): - edit the .sln file with a text editor and use environment variables with the following syntax %MyEnvironmentVariable% - edit the .vcproj files and replace the path to the desired files with some variables, with the following syntax $(MyEnvironmentVariable).

我找到了一种方法,它适用于我(使用Visual Studio 2005): - 使用文本编辑器编辑.sln文件并使用具有以下语法的环境变量%MyEnvironmentVariable% - 编辑.vcproj文件并替换使用一些变量获取所需文件的路径,使用以下语法$(MyEnvironmentVariable)。

Hope it helps... Cyrille

希望它有所帮助...... Cyrille

#3


The best way to achieve what you describe in b) is to use property sheets. Check out also this very similar question.

实现b)中描述的最佳方法是使用属性表。查看这个非常相似的问题。

I found some promising info on property sheets, but Visual studio doesn't expand macros when I am using them in File tag in the .vcproj.

我在属性表上找到了一些有希望的信息,但是当我在.vcproj中的File标签中使用它时,Visual Studio不会扩展宏。

I am not sure what version of VS you use. VS2008 lets you define for example an include directory like this: "$(OpenCVInclude)\cxcore\include". I use it all the time. OpenCVInclude is a macro defined in a property sheet.

我不确定你使用的是什么版本的VS. VS2008允许您定义例如这样的包含目录:“$(OpenCVInclude)\ cxcore \ include”。我用它所有的时间。 OpenCVInclude是属性表中定义的宏。

As for question a), I think there is no "clean" way to do what you want. As an alternative you could the configuration manager:

至于问题a),我认为没有“干净”的方式来做你想要的。作为替代方案,您可以配置管理器:

  • Include all the projects in the solution.
  • 包括解决方案中的所有项目。

  • Name the project differently, for example based on the OEM.
  • 以不同方式命名项目,例如基于OEM。

  • For each project define release and debug configurations in the solution
  • 对于每个项目,在解决方案中定义发布和调试配置

  • In "Build->Configuration Manager" You can check or uncheck the "Build" column for each configuration. Check "build" for the relevant project.
  • 在“Build-> Configuration Manager”中,您可以选中或取消选中每个配置的“Build”列。检查相关项目的“构建”。

#4


I am not sure if you are building just C++ projects or if you are also building C#\VB projects, but one of the great things about Visual Studio is all of the projects are really just MSBuild projects. If you edit a project in a text editor you will see that at the end of the project it imports a .targets file. If you track down and find follow the imports you will find that almost all of the VS projects import Microsoft.Common.Targets. Microsoft.Common.Targets imports Custom.Before.Microsoft.Common.Targets. Using this import you can import your own targets file with your own custom actions.

我不确定你是在构建C ++项目还是构建C#\ VB项目,但是Visual Studio的一个好处就是所有的项目都只是MSBuild项目。如果在文本编辑器中编辑项目,您将看到在项目结束时导入.targets文件。如果你追踪并找到跟随导入,你会发现几乎所有的VS项目都导入了Microsoft.Common.Targets。 Microsoft.Common.Targets导入Custom.Before.Microsoft.Common.Targets。使用此导入,您可以使用自己的自定义操作导入自己的目标文件。

I for example have a target file that has a common property defined across all projects in a solution and a custom post build event that processes at the end of each project building.

例如,我有一个目标文件,它具有在解决方案中的所有项目中定义的公共属性,以及在每个项目构建结束时处理的自定义后期构建事件。

Using this extension method and by creating custom configurations in the solution besides just the standard release\debug, you should be able to create as complex of a build configuration as you need.

除了标准的release \ debug之外,使用此扩展方法并在解决方案中创建自定义配置,您应该能够根据需要创建构建配置的复杂。

#5


(a) Cyrille gives a decent solution to what you asked for. Another way is to keep your changeable settings in one common place. Note that this won't work for non-msbuild files like *.sln and *.vcproj (until VS 2010).

(a)Cyrille为您所要求的提供了一个体面的解决方案。另一种方法是将可更改的设置保存在一个公共位置。请注意,这不适用于* .sln和* .vcproj等非msbuild文件(直到VS 2010)。

Project files: ... $(ChangeableDir)\foo.cs

项目文件:... $(ChangeableDir)\ foo.cs

Common.targets: ChangeThis ...

Common.targets:ChangeThis ...

However, I don't think this is a great way to do things. If the differences between what you're building are large pieces of functionality, you should consider creating a branch in your source control system. OTOH, if the differences are minor -- eg hardcoded strings -- then this leads to your second question...

但是,我认为这不是一个很好的做事方式。如果您正在构建的内容之间的差异是大部分功能,则应考虑在源代码管理系统中创建分支。 OTOH,如果差异很小 - 例如硬编码字符串 - 那么这会导致你的第二个问题......

(b) The kind of resource management you describe is essentially the same problem faced by people localizing their project into different languages. Luckily, direct support is built into Visual Studio since 2005. Check out previous questions like: Localization in Visual Studio 2008

(b)您描述的资源管理类型与人们将项目本地化为不同语言所面临的问题基本相同。幸运的是,自2005年以来,直接支持内置于Visual Studio中。请查看以前的问题,例如:Visual Studio 2008中的本地化