Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 632
Python_user Avatar asked Nov 21 '16 06:11

Python_user


2 Answers

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.

like image 174
Some programmer dude Avatar answered Sep 18 '22 12:09

Some programmer dude


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.

like image 34
khôi nguyễn Avatar answered Sep 19 '22 12:09

khôi nguyễn