visual-studio Visual Studio:自定义项目变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4249844/
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
Visual Studio: custom project variables
提问by insipid
Visual studio provides a lot of $ variables that can be used inside the IDE such as $(SolutionDir). How can one create their own variables such as $(MY_INCLUDES_DIR) (that can be set by users of a project to point to their headers)? Is it even possible?
Visual Studio 提供了很多可以在IDE 内部使用的$ 变量,例如$(SolutionDir)。如何创建自己的变量,例如 $(MY_INCLUDES_DIR)(可以由项目用户设置以指向其标题)?甚至有可能吗?
采纳答案by Nathan Pitman
You can create environment variables in Project Properties->Debugging->Environment. Even though they don't show up in the list of macros (since they're custom and not inbuilt), you can still use them.
您可以在项目属性->调试->环境中创建环境变量。即使它们没有出现在宏列表中(因为它们是自定义的而不是内置的),您仍然可以使用它们。
回答by AaronMK
I know this works for VS 2010:
我知道这适用于 VS 2010:
If you have property sheets, there is a section called "User Macros" under the "Common Properties" tree. You can create your own $(MyNamedVariable) macro, and even define it in terms of other $(SomeExistingMacro). If you have not played with property sheets before, look for the "Property Manager" under the view menu, and it will allow you to create and edit them.
如果您有属性表,则在“通用属性”树下有一个名为“用户宏”的部分。您可以创建自己的 $( MyNamedVariable) 宏,甚至可以根据其他 $( SomeExistingMacro)定义它。如果您以前没有使用过属性表,请在视图菜单下查找“属性管理器”,它允许您创建和编辑它们。
Any project configuration that inherits the property sheet will see your macro as if VS had defined it itself, ie it will show up in the list of macros. User of your project can just go to some base property sheet, and edit the MY_INCLUDES_DIR (to use your example) in a single place.
任何继承属性表的项目配置都会看到你的宏,就像 VS 自己定义它一样,即它会显示在宏列表中。您项目的用户只需转到某个基本属性表,然后在一个地方编辑 MY_INCLUDES_DIR(以使用您的示例)。
Hope this helps.
希望这可以帮助。
(How to create property sheets? See http://msdn.microsoft.com/en-us/library/5k4a0033.aspx. View->Other Windows->Property Manger)
(如何创建属性表?请参阅http://msdn.microsoft.com/en-us/library/5k4a0033.aspx。查看->其他窗口->属性管理器)
回答by Nick
I created a property style sheet, to specify which Python (and SCons), now in my vcxproj, I can include the Python include dir by adding $(PythonIncludeDir) to the additional includes property.
我创建了一个属性样式表,以指定哪些 Python(和 SCons),现在在我的 vcxproj 中,我可以通过将 $(PythonIncludeDir) 添加到附加包含属性来包含 Python 包含目录。
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros">
<PythonDir>C:\Python27_64</PythonDir>
<PythonExe>$(PythonDir)\python.exe</PythonExe>
<PythonIncludeDir>$(PythonDir)\include</PythonIncludeDir>
<PythonLibDir>$(PythonDir)\libs</PythonLibDir>
<SCons>$(PythonExe) $(PythonDir)\Scripts\scons.py</SCons>
</PropertyGroup>
<ItemDefinitionGroup />
<ItemGroup>
<BuildMacro Include="PythonDir">
<Value>$(PythonDir)</Value>
<EnvironmentVariable>true</EnvironmentVariable>
</BuildMacro>
<BuildMacro Include="PythonExe">
<Value>$(PythonExe)</Value>
<EnvironmentVariable>true</EnvironmentVariable>
</BuildMacro>
<BuildMacro Include="PythonIncludeDir">
<Value>$(PythonIncludeDir)</Value>
<EnvironmentVariable>true</EnvironmentVariable>
</BuildMacro>
<BuildMacro Include="PythonLibDir">
<Value>$(PythonLibDir)</Value>
<EnvironmentVariable>true</EnvironmentVariable>
</BuildMacro>
<BuildMacro Include="SCons">
<Value>$(SCons)</Value>
<EnvironmentVariable>true</EnvironmentVariable>
</BuildMacro>
</ItemGroup>
</Project>
回答by Sagar Gupta
You can follow any solution as described below.
您可以按照以下描述的任何解决方案进行操作。
Set the environment variable in a batch file. Create a batch file and insert code like the following:
set MY_INCLUDES_DIR=D:\MyIncludes call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat" "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe" D:\MySolution.sln
在批处理文件中设置环境变量。创建一个批处理文件并插入如下代码:
set MY_INCLUDES_DIR=D:\MyIncludes call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat" "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe" D:\MySolution.sln
Then start the Visual Studio solution by double-clicking this file. An advantage of this approach is that all projects can use the same environment variable.
然后通过双击此文件启动 Visual Studio 解决方案。这种方法的一个优点是所有项目都可以使用相同的环境变量。
- Define an xml tag in a Visual Studio project file within a PropertyGroup. One preexisting PropertyGroup that you can use for this purpose is the one with the label "UserMacros", which is empty by default.
- 在 PropertyGroup 内的 Visual Studio 项目文件中定义 xml 标记。可以用于此目的的一个预先存在的 PropertyGroup 是带有标签“UserMacros”的,默认情况下为空。
<PropertyGroup Label="UserMacros">
<!-- Add the following line. -->
<MY_INCLUDES_DIR>D:\MyIncludes\</MY_INCLUDES_DIR>
</PropertyGroup>
Create a system environment variable named
MY_INCLUDES_DIR.Go to View(menubar) -> Property Manager. Select project, select platform and configuration, right click on Microsoft.Cpp.x64.userthen go to User MacrosTab and click on Add Macros.
创建一个名为
MY_INCLUDES_DIR.转到View(menubar) -> Property Manager。选择项目,选择平台和配置,右键单击Microsoft.Cpp.x64.user然后转到User Macros选项卡并单击Add Macros。
Whether you choose option 1, 2, 3, or 4, you can then refer to $(MY_INCLUDES_DIR)in your Visual Studio project files.
无论您选择选项 1、2、3 还是 4,您都可以$(MY_INCLUDES_DIR)在 Visual Studio 项目文件中引用。
回答by Jon Eldridge
Just had to do the same thing. I have three solutions which include a common project. Each of the solutions needs to build the common project with slightly different settings.
只好做同样的事情。我有三个解决方案,其中包括一个共同的项目。每个解决方案都需要使用稍微不同的设置来构建公共项目。
Discovered this can be easily done using Project Property Sheets. Create a Property Sheet file for the settings you want to change and save it in the solution directory. Open the Project file in a text editor (or in VS with Open With \ Source Code Text Editor) and find the directory path of the Property Sheet. Modify it so that the path used is '$(SolutionDir)'.
发现这可以使用项目属性表轻松完成。为要更改的设置创建一个属性表文件,并将其保存在解决方案目录中。在文本编辑器中(或在 VS 中使用 Open With \ Source Code Text Editor)打开 Project 文件,找到属性表的目录路径。修改它,使使用的路径为'$(SolutionDir)'。
You now have a Visual Studio Project that loads solution specific Property Sheets.
您现在拥有一个加载解决方案特定属性表的 Visual Studio 项目。

