C语言 如何在 Visual Studio 项目中定义相对路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6895457/
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
How to define relative paths in Visual Studio Project?
提问by Ali Ahmed
I have a library and a console application that uses a library. The library has a folder with source and header files.
我有一个库和一个使用库的控制台应用程序。该库有一个包含源文件和头文件的文件夹。
My project is in a child/inner directory but that library directory that I want to include is in a parent/upper directory.
我的项目位于子/内部目录中,但我想要包含的库目录位于父/上层目录中。
My project directory:
我的项目目录:
H:\Gmail_04\gsasl-1.0\lib\libgsaslMain
Includes files are here:
包含文件在这里:
H:\Gmail_04\gsasl-1.0\src
How can I use paths relative to the project directory, to include folders that are in a parent/upper directory?
如何使用相对于项目目录的路径来包含父/上级目录中的文件夹?
采纳答案by MByD
If I get you right, you need ..\..\src
如果我猜对了,你需要 ..\..\src
回答by eckes
Instead of using relative paths, you could also use the predefined macros of VS to achieve this.
除了使用相对路径,您还可以使用 VS 的预定义宏来实现这一点。
$(ProjectDir)points to the directory of your .vcprojfile, $(SolutionDir)is the directory of the .slnfile.
$(ProjectDir)指向你的.vcproj文件$(SolutionDir)所在的目录,就是文件所在的目录.sln。
You get a list of available macros when opening a project, go to
Properties → Configuration Properties → C/C++ → General
and hit the three dots:
打开项目时,您会获得可用宏的列表,转到
属性 → 配置属性 → C/C++ → 常规
并点击三个点:


In the upcoming dialog, hit Macrosto see the macros that are predefined by the Studio (consult MSDNfor their meaning):
在即将出现的对话框中,点击宏以查看 Studio 预定义的宏(请参阅MSDN了解其含义):


You can use the Macros by typing $(MACRO_NAME)(note the $and the roundbrackets).
您可以通过键入使用宏$(MACRO_NAME)(注意$和圆括弧)。
回答by yvan vander sanden
I have used a syntax like this before:
我以前使用过这样的语法:
$(ProjectDir)..\headers
or
或者
..\headers
As other have pointed out, the starting directory is the one your project file is in(vcprojor vcxproj), not where your main code is located.
正如其他人指出的那样,起始目录是您的项目文件所在的目录(vcproj或vcxproj),而不是您的主要代码所在的位置。
回答by Michael Haephrati
By default, all paths you define will be relative. The question is: relative to what? There are several options:
默认情况下,您定义的所有路径都是相对的。问题是:相对于什么?有几种选择:
- Specifying a file or a path with nothing before it. For example: "mylib.lib". In that case, the file will be searched at the Output Directory.
- If you add "..\", the path will be calculated from the actual path where the .sln file resides.
- 指定前面没有任何内容的文件或路径。例如:“ mylib.lib”。在这种情况下,将在输出目录中搜索文件。
- 如果添加“..\”,路径将根据.sln 文件所在的实际路径计算。
Please note that following a macro such as $(SolutionDir) there is no need to add a backward slash "\". Just use $(SolutionDir)mylibdir\mylib.lib. In case you just can't get it to work, open the project file externally from Notepad and check it.
请注意,在 $(SolutionDir) 等宏之后无需添加反斜杠“\”。只需使用$(SolutionDir)mylibdir\mylib.lib。如果您无法让它工作,请从记事本外部打开项目文件并检查它。

