C ++ - 在Visual Studio 2010的属性表中使用环境变量

时间:2021-06-15 23:20:32

I want to set up a project which uses OpenCV. So basically, I have to add a new property sheet using the property manager from Visual Studio 2010.

我想建立一个使用OpenCV的项目。所以基本上,我必须使用Visual Studio 2010中的属性管理器添加一个新的属性表。

Basically, I have to add the path to my include and lib folders in order to make OpenCV functional. I want to put my project in a git repository and I don't want to change the paths in my property sheets every time. Is there a way of using a system variable in my property sheet with the path to the build folder of OpenCV? If there exist a way of doing this, can I assign the value of that variable directly from the code? Like a #define or a similar assignment?

基本上,我必须添加我的include和lib文件夹的路径,以使OpenCV功能。我想将我的项目放在git存储库中,我不想每次都更改属性表中的路径。有没有办法在我的属性表中使用系统变量和OpenCV的build文件夹的路径?如果存在这样做的方法,我可以直接从代码中分配该变量的值吗?像#define或类似的作业?

2 个解决方案

#1


2  

A plenty of useful macros/properties can be used, to construct paths that are relative to location of your project, solution, like $(SolutionPath) etc. (They are listed when you edit a text field in configuration.

可以使用大量有用的宏/属性来构建与项目位置相关的路径,解决方案,如$(SolutionPath)等。(在配置中编辑文本字段时会列出它们)。

The usual place to define machine, installation or user-specific stuff is the users.props file (Microsoft.cpp..users.props) located at $(USERPROFILE)\appdata\local\microsoft\msbuild\v4.0 directory.

定义机器,安装或用户特定内容的通常位置是users.props文件(Microsoft.cpp..users.props),位于$(USERPROFILE)\ appdata \ local \ microsoft \ msbuild \ v4.0目录中。

If you open a .vcxproj you can see it included close to top. Use it to define some directory base paths as properties, and you can use those like the built-in ones later. Certainly you can also use that to locate a .props file and import that.

如果您打开.vcxproj,您可以看到它包含在顶部附近。使用它将某些目录库路径定义为属性,稍后可以使用内置路径。当然你也可以使用它来定位.props文件并导入它。

Or you can use it to add additional entries to include item directly.

或者您可以使用它添加其他条目以直接包含项目。

#2


2  

The "macros" available in property settings are actually environment variables added by Visual Studio. If you set the environment variable in your user profile you can add use them in your property sheet the same way you use the "built-in" macros VS provides.

属性设置中可用的“宏”实际上是Visual Studio添加的环境变量。如果在用户配置文件中设置环境变量,则可以在属性表中添加使用它们,就像使用VS提供的“内置”宏一样。

For example, I have an networked hard drive with a folder called LIBS containing Boost, Google Test, Google Mock, Tiny XML and a few dozen other libraries. I mount it as drive letter Z and I have an environment variable set in my user profile called XTPLIBRARIES which points to Z:\LIBS. In my property sheet I've configured the include paths like below.

例如,我有一个网络硬盘驱动器,其中包含一个名为LIBS的文件夹,其中包含Boost,Google Test,Google Mock,Tiny XML和其他几十个库。我将它作为驱动器号Z挂载,我在我的用户配置文件中设置了一个环境变量,名为XTPLIBRARIES,它指向Z:\ LIBS。在我的属性表中,我已经配置了如下所示的包含路径。

$(XTPLIBRARIES)\boost_1_53_0\boost
$(XTPLIBRARIES)\gtest-1.6.0\include
$(XTPLIBRARIES)\tinyxml2

$(XTPLIBRARIES)\ boost_1_53_0 \ boost $(XTPLIBRARIES)\ gtest-1.6.0 \ include $(XTPLIBRARIES)\ tinyxml2

Now any time I create a new project I just add the property sheet and everything works as expected - as long as the environment variable is set of course.

现在,每当我创建一个新项目时,我只需添加属性表,一切都按预期工作 - 只要设置环境变量即可。

#1


2  

A plenty of useful macros/properties can be used, to construct paths that are relative to location of your project, solution, like $(SolutionPath) etc. (They are listed when you edit a text field in configuration.

可以使用大量有用的宏/属性来构建与项目位置相关的路径,解决方案,如$(SolutionPath)等。(在配置中编辑文本字段时会列出它们)。

The usual place to define machine, installation or user-specific stuff is the users.props file (Microsoft.cpp..users.props) located at $(USERPROFILE)\appdata\local\microsoft\msbuild\v4.0 directory.

定义机器,安装或用户特定内容的通常位置是users.props文件(Microsoft.cpp..users.props),位于$(USERPROFILE)\ appdata \ local \ microsoft \ msbuild \ v4.0目录中。

If you open a .vcxproj you can see it included close to top. Use it to define some directory base paths as properties, and you can use those like the built-in ones later. Certainly you can also use that to locate a .props file and import that.

如果您打开.vcxproj,您可以看到它包含在顶部附近。使用它将某些目录库路径定义为属性,稍后可以使用内置路径。当然你也可以使用它来定位.props文件并导入它。

Or you can use it to add additional entries to include item directly.

或者您可以使用它添加其他条目以直接包含项目。

#2


2  

The "macros" available in property settings are actually environment variables added by Visual Studio. If you set the environment variable in your user profile you can add use them in your property sheet the same way you use the "built-in" macros VS provides.

属性设置中可用的“宏”实际上是Visual Studio添加的环境变量。如果在用户配置文件中设置环境变量,则可以在属性表中添加使用它们,就像使用VS提供的“内置”宏一样。

For example, I have an networked hard drive with a folder called LIBS containing Boost, Google Test, Google Mock, Tiny XML and a few dozen other libraries. I mount it as drive letter Z and I have an environment variable set in my user profile called XTPLIBRARIES which points to Z:\LIBS. In my property sheet I've configured the include paths like below.

例如,我有一个网络硬盘驱动器,其中包含一个名为LIBS的文件夹,其中包含Boost,Google Test,Google Mock,Tiny XML和其他几十个库。我将它作为驱动器号Z挂载,我在我的用户配置文件中设置了一个环境变量,名为XTPLIBRARIES,它指向Z:\ LIBS。在我的属性表中,我已经配置了如下所示的包含路径。

$(XTPLIBRARIES)\boost_1_53_0\boost
$(XTPLIBRARIES)\gtest-1.6.0\include
$(XTPLIBRARIES)\tinyxml2

$(XTPLIBRARIES)\ boost_1_53_0 \ boost $(XTPLIBRARIES)\ gtest-1.6.0 \ include $(XTPLIBRARIES)\ tinyxml2

Now any time I create a new project I just add the property sheet and everything works as expected - as long as the environment variable is set of course.

现在,每当我创建一个新项目时,我只需添加属性表,一切都按预期工作 - 只要设置环境变量即可。