Using (relative) paths to shortcut in include statements in C++

浪子不回头ぞ 提交于 2020-01-14 14:03:53

问题


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.

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?

Thanks.


回答1:


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.)


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

#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

#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

#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:


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.



来源:https://stackoverflow.com/questions/40714365/using-relative-paths-to-shortcut-in-include-statements-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!