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

发布于 2024-07-20 02:12:09 字数 539 浏览 6 评论 0原文

我有两个类似的问题:

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

Visual Studio 中是否有任何构建方法可以做到这一点?

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

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

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

像 \ProjectDir\$(Var2)\resource.rc

我在属性表上发现了一些有希望的信息,但是当我在 .vcproj 中的文件标记中使用宏时,Visual Studio 不会扩展宏。

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

问候, 胜利者

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.

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

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

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

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.

Something like \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.

Thank you for any ideas how to solve this problem.

Regards,
Victor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

西瑶 2024-07-27 02:12:09

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

OutputDirectory="$(MyEnvVariableName)\Bin"

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

有一篇 MSDN 文章专门介绍了这一点:如何:在构建中使用环境变量< /a>

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.

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

软甜啾 2024-07-27 02:12:09

我想我和你有同样的目标:我想使用环境变量来定位解决方案文件(.sln)中的一些项目,并使用一些环境变量来定位我的项目中的一些文件。

我找到了一种方法来做到这一点,它对我来说效果很好(使用 Visual Studio 2005):
- 使用文本编辑器编辑 .sln 文件,并使用以下语法 %MyEnvironmentVariable% 的环境变量
- 编辑 .vcproj 文件并用一些变量替换所需文件的路径,语法如下 $(MyEnvironmentVariable)。

希望能帮助到你...
西里尔

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.

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).

Hope it helps...
Cyrille

醉梦枕江山 2024-07-27 02:12:09

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

我发现了一些有希望的信息
属性表,但 Visual Studio
当我使用时不展开宏
它们位于 .vcproj 中的 File 标记中。

不知道你用的VS是什么版本。 VS2008 允许您定义一个包含目录,如下所示:“$(OpenCVInclude)\cxcore\include”。 我用它所有的时间。 OpenCVinclude 是在属性表中定义的宏。

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

  • 包括解决方案中的所有项目。
  • 以不同的方式命名项目,例如根据 OEM。
  • 对于解决方案中的每个项目定义发布和调试配置,
  • 在“构建->配置管理器”中,您可以选中或取消选中每个配置的“构建”列。 检查相关项目的“构建”。

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

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.

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.

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

  • 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.
一片旧的回忆 2024-07-27 02:12:09

我不确定您是否只构建 C++ 项目,或者是否也在构建 C#\VB 项目,但 Visual Studio 的一大优点是所有项目实际上都只是 MSBuild 项目。 如果您在文本编辑器中编辑项目,您将看到在项目末尾导入了一个 .targets 文件。 如果你追踪并发现遵循导入,你会发现几乎所有 VS 项目都导入 Microsoft.Common.Targets。 Microsoft.Common.Targets 导入 Custom.Before.Microsoft.Common.Targets。 使用此导入,您可以使用自己的自定义操作导入自己的目标文件。

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

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

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.

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.

雾里花 2024-07-27 02:12:09

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

项目文件:

...
$(ChangeableDir)\foo.cs

共同目标:

改变这个
...

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

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

(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).

Project files:

...
$(ChangeableDir)\foo.cs

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...

(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

薄荷→糖丶微凉 2024-07-27 02:12:09

一种解决方案是在启动 VS 之前设置一些环境变量。 在我们的项目中,我们使用一个批处理文件,如下所示:

SET ROOT=D:/root
SET COMPILER_ROOT=C:/Program Files (x86)/Microsoft Visual Studio 12.0/Common7/IDE
start "Microsoft Visual Studio 12.0" /D"%COMPILER_ROOT%\" "%COMPILER_ROOT%/devenv.exe" "%ROOT%\trunk\OurSolution.sln"

One solution is to set some enviroment variables just before starting VS. In our projects we use a batch file which looks like this:

SET ROOT=D:/root
SET COMPILER_ROOT=C:/Program Files (x86)/Microsoft Visual Studio 12.0/Common7/IDE
start "Microsoft Visual Studio 12.0" /D"%COMPILER_ROOT%\" "%COMPILER_ROOT%/devenv.exe" "%ROOT%\trunk\OurSolution.sln"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文