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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/840472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:28:15  来源:igfitidea点击:

Macros/Environment variable in .sln and .vcproj files for Visual studio

visual-studioprojectpathsolution

提问by

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

问候, 维克多

回答by Rom

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 文章:How to: Use Environment Variables in a Build

回答by Rom

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

回答by Dani van der Meer

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.
  • 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.
  • 包括解决方案中的所有项目。
  • 以不同的方式命名项目,例如根据 OEM。
  • 对于每个项目,在解决方案中定义发布和调试配置
  • 在“Build->Configuration Manager”中,您可以选中或取消选中每个配置的“Build”列。选中相关项目的“构建”。

回答by mageos

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.

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

回答by Richard Berg

(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 中的本地化