I started coding in C++ few days ago. I am using windows as my OS for writing code. I have been keeping all of my "well-written" codes in a single location. Now, I am working on a project that requires the use of those codes. So, I planned to include those files that I require as header files in my project. However, to make my project "self-contained", I created shortcuts of those folders that I require and kept them in the source folder of my new project and decided to use relative paths to the shortcuts in the "include
" statements in my project.
几天前我开始用C ++编写代码。我使用Windows作为我的操作系统来编写代码。我一直把所有“写得很好”的代码保存在一个地方。现在,我正在开发一个需要使用这些代码的项目。所以,我打算在项目中包含我需要的那些文件作为头文件。但是,为了使我的项目“自包含”,我创建了我需要的那些文件夹的快捷方式,并将它们保存在我的新项目的源文件夹中,并决定使用相对路径来访问项目中“include”语句中的快捷方式。
However, I am getting an error. Is there any way to use relative (or, in general, absolute) paths to shortcuts in the include
statements of C++ in windows?
但是,我收到了一个错误。有没有办法在Windows的C ++的include语句中使用相对(或通常是绝对的)快捷方式的路径?
Thanks.
2 个解决方案
#1
5
It really depends on how you include the header files.
这实际上取决于您如何包含头文件。
If you include with double-quotes, like e.g.
如果您使用双引号,例如
#include "some_header_file.h"
Then the relative path is from the current files location.
然后相对路径来自当前文件位置。
If you include using angle-brackets, like e.g.
如果您使用角括号,例如
#include <some_header_file.h>
Then the relative path is based on the system include paths.
然后相对路径基于系统包含路径。
You can always add a path to the system include path. How to do it depend on your environment and compiler. If you're using Visual Studio you go into the project properties dialog, and in the "C/C++" / "General" tab there is a field called "Additional Include Directories" where you can add directories. (This is for VS 2015, might be a little different on other versions.)
您始终可以添加系统包含路径的路径。如何做取决于您的环境和编译器。如果您正在使用Visual Studio,则进入项目属性对话框,在“C / C ++”/“常规”选项卡中,有一个名为“Additional Include Directories”的字段,您可以在其中添加目录。 (这适用于VS 2015,在其他版本上可能略有不同。)
Regarding double quotes inclusion. Lets say your project hierarchy looks like this (on disk!):
关于双引号包含。让我们说你的项目层次结构看起来像这样(在磁盘上!):
Project |-- Include |-- Source | `-- MoreSource `-- Other
In Project/Source
you have your source files, and if one of them want to include a header file from Project/Include
, then it will look something like
在Project / Source中,您有源文件,如果其中一个想要包含Project / Include中的头文件,那么它看起来就像
#include "../include/header.h"
Now if you have a source file in Project/Source/MoreSource
that want to include the same header file it will be
现在,如果您在Project / Source / MoreSource中有一个源文件,它想要包含相同的头文件
#include "../../include/header.h"
It could be useful to add the Project/Include
directory to the system header search path. You can still use double-quotes to include the files, since if they are not found then the preprocessor will search the system paths as well, but you don't need the "full" relative path. If you add Project/Include
to the system header path, you could write just
将Project / Include目录添加到系统头搜索路径可能很有用。您仍然可以使用双引号来包含文件,因为如果找不到它们,那么预处理器也将搜索系统路径,但您不需要“完整”相对路径。如果将Project / Include添加到系统标题路径,则可以只编写
#include "header.h"
Or
#include <header.h>
Be careful though, if you have a header file with the same name as an actual system header file you might have some trouble.
但要小心,如果您的头文件与实际系统头文件同名,则可能会遇到一些麻烦。
#2
1
From https://superuser.com/questions/253935/what-is-the-difference-between-symbolic-link-and-shortcut
You can't include folder shortcut since it's a file, not a folder.
您不能包含文件夹快捷方式,因为它是文件,而不是文件夹。
You can read the guide to create symbolic link on windows.
您可以阅读指南以在Windows上创建符号链接。
#1
5
It really depends on how you include the header files.
这实际上取决于您如何包含头文件。
If you include with double-quotes, like e.g.
如果您使用双引号,例如
#include "some_header_file.h"
Then the relative path is from the current files location.
然后相对路径来自当前文件位置。
If you include using angle-brackets, like e.g.
如果您使用角括号,例如
#include <some_header_file.h>
Then the relative path is based on the system include paths.
然后相对路径基于系统包含路径。
You can always add a path to the system include path. How to do it depend on your environment and compiler. If you're using Visual Studio you go into the project properties dialog, and in the "C/C++" / "General" tab there is a field called "Additional Include Directories" where you can add directories. (This is for VS 2015, might be a little different on other versions.)
您始终可以添加系统包含路径的路径。如何做取决于您的环境和编译器。如果您正在使用Visual Studio,则进入项目属性对话框,在“C / C ++”/“常规”选项卡中,有一个名为“Additional Include Directories”的字段,您可以在其中添加目录。 (这适用于VS 2015,在其他版本上可能略有不同。)
Regarding double quotes inclusion. Lets say your project hierarchy looks like this (on disk!):
关于双引号包含。让我们说你的项目层次结构看起来像这样(在磁盘上!):
Project |-- Include |-- Source | `-- MoreSource `-- Other
In Project/Source
you have your source files, and if one of them want to include a header file from Project/Include
, then it will look something like
在Project / Source中,您有源文件,如果其中一个想要包含Project / Include中的头文件,那么它看起来就像
#include "../include/header.h"
Now if you have a source file in Project/Source/MoreSource
that want to include the same header file it will be
现在,如果您在Project / Source / MoreSource中有一个源文件,它想要包含相同的头文件
#include "../../include/header.h"
It could be useful to add the Project/Include
directory to the system header search path. You can still use double-quotes to include the files, since if they are not found then the preprocessor will search the system paths as well, but you don't need the "full" relative path. If you add Project/Include
to the system header path, you could write just
将Project / Include目录添加到系统头搜索路径可能很有用。您仍然可以使用双引号来包含文件,因为如果找不到它们,那么预处理器也将搜索系统路径,但您不需要“完整”相对路径。如果将Project / Include添加到系统标题路径,则可以只编写
#include "header.h"
Or
#include <header.h>
Be careful though, if you have a header file with the same name as an actual system header file you might have some trouble.
但要小心,如果您的头文件与实际系统头文件同名,则可能会遇到一些麻烦。
#2
1
From https://superuser.com/questions/253935/what-is-the-difference-between-symbolic-link-and-shortcut
You can't include folder shortcut since it's a file, not a folder.
您不能包含文件夹快捷方式,因为它是文件,而不是文件夹。
You can read the guide to create symbolic link on windows.
您可以阅读指南以在Windows上创建符号链接。