Objective
This article summarizes the environment variables that are common in Visual Studio and their role in organizational solutions and engineering.
Note: This article uses Visual Studio 2013, which is described in Visual C + + as a work environment configuration, because the author is primarily engaged in C + + development.
What is the environment variable for VS?
First look at the chart, the figure in the dollar sign $ $ + a pair of parentheses, so the reference is my so-called environment variables,
Several of the environment variables that appear in the diagram have the following meanings:
environment variable name |
meaning |
$ (SolutionDir) |
Solution directory: The path where the. sln file is located |
$ (Configuration) |
The current compilation configuration name, such as Debug, or release |
$ (ProjectName) |
The current project name, which is the game in the diagram |
In the Chinese version of VS, the environment variable is translated as "macro", in order to avoid mixing with macro in C + + language, I call it "vs environment variable" in this article, referred to as environment variable.
What is the use of environment variables?
Use environment variables to organize your project catalog
vs as an IDE, its vocation is to help developers organize projects, mainly including the project source files, library files organization. (Essentially, it provides a visual interface that allows developers to easily define the parameters of the compiler and linker.) The two most common directories used when using VS to organize a project catalog are:
The default header file search path in VS is:工程路径– that is,.vcxproj(different versions of VC + + suffix names are different, such as the path where the vs2010 suffix. vcproj) files. For example, the following directory structure:
–root/
--test.vcxproj
--hello.cpp
--hello.h
--world.cpp
--world.h
--main.cpp
--/subdir
--sub.h
The. vcxproj file is included in the root directory, so root is工程路径, in VS, the. h file under this directory can be included directly using include, such as in Main.cpp I can write:
#include "hello.h"#include "world.h"
But for sub.h, we can not directly write#include "sub.h", because工程路径the following cannot search for this file, I want to tell the compiler where this file, there are usually the following two ways:
Google's C + + programming style encourages the first approach, the advantage is that you can see the file relative complete path, if the header file search path only one root directory, then this path is the relative root directory of the file's physical path, convenient to locate the file.
If you think this is a hassle, and the path depth may have multiple layers, different depths of the path and usually contain a large number of files, then you can choose the second approach, each sub-directory is added to the search path, so that you can not take the path directly#include "filename.h"on it. How do you add a file containing directory specifically to vs? This leads to the answer to the question in this section: What is the use of environment variables? One of the uses is to write the search path for the header file.
I'm sure you all know how to add a header file search path to vs. here's a tip for Beginners: Project Properties-Configuration Properties-C/S-General-Edit the "Additional Include Directories" value on the right.
As shown in the following examples:
When you click Edit, the edit window appears as shown:
Here you can create new, delete include paths, and adjust the include order.
Click the New button or double-click the list to add a path, and when you edit the newly added path, you can see a browse button to the right of the list entry.
Click the button to select the path from the Open file dialog and click OK to see the newly added path name. Although browsing to locate a folder is easier, you can avoid writing it yourself, but you will find that the path added by browsing is an absolute path.
If you are the only developer of the project and only use this computer to develop it, then the absolute path is no big deal. But if this project is a team collaboration project, or you need to switch between several computers, then this contains the path if the absolute path is not flexible enough, if someone else's path configuration or other computer path configuration is different, then re-modify the include path.
A reasonable way to write a containing path is to use a relative path.
Who is it relative? Relative to the project root directory or the solution root directory.
Why is it? Because no matter what other people's computer has, no matter where the other person's project is placed, the files to be included can be calculated from the location of the project.
The premise, of course, is that project developers have previously agreed on the location of the included files relative to the project root directory. It is usually placed in a subdirectory of the project root directory (or the solution root).
How do you do it specifically? This will require the topic of this article: environment variables. The environment variables for the two relative directories mentioned earlier are shown in the following table:
Catalogue |
The corresponding environment variable name |
Project root directory |
$ (ProjectDir) |
Solution root Directory |
$ (SolutionDir) |
To solve the problem in the small case just now,
–root/
--test.vcxproj
--hello.cpp
--hello.h
--world.cpp
--world.h
--main.cpp
--/subdir
--sub.h
Notice that the. vcxproj directory is the project root directory, which is the value of $ (projectdir) equal to root/. So to put the subdir in the included directory, you can create a new one that contains the path:
$(ProjectDir)subdir
In this way, you can write directly in the Main.cpp#include "sub.h". Regardless of where the item is copied, the include path is not modified.
The above is a small example of the use of environment variables. The advantage of using environment variables to write file inclusion paths is that the include path is independent of the path where the project is located, no need to re-modify the include path, because the file inclusion path written using environment variables is a relative path.
Other VS Environment variables
How do I see all the environment variable values?
There are a lot of places can be viewed, such as just when the inclusion of the catalog, pop-up window, notice the bottom right, there is a "macro" button
Click on it to see all the "Macros" (that is, the value of the VS environment variable):
The input box above can be filtered.
The following table shows the meanings of the commonly used environment variables:
environment variable name |
meaning |
$(SolutionDir) |
Solution directory: The path where the. sln file is located |
$(ProjectDir) |
Project root directory: the path where the. vcxproj file is located |
$(Configuration) |
The current compilation configuration name, such as Debug, or release |
$(ProjectName) |
Current project Name |
$(SolutionName) |
Solution Name |
$(OutDir) |
Project output File directory |
$(TargetName) |
The project generates the target file, usually with the$(ProjectName)same name |
$(TargetExt) |
Project generation file suffix names, such as. exe,. lib Depending on project settings |
$(ProjectExt) |
The project file suffix name, such as. vcxproj |
In the next article, we will explain how to use these environment variables reasonably to organize VC + + projects.
Advanced Thinking
- How do I define and extend the environment variables for vs?
The level of the author is limited, the understanding of the relevant knowledge and summary is inevitably wrong, but also hope to give correct, thank you very much!
You can also see this article here: GitHub blog, csdn blog, Welcome to visit
Environment variables in Visual Studio (for example, Visual Studio 2013)