11

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.

Python_user
  • 1,378
  • 3
  • 12
  • 25

2 Answers2

28

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Hi, I am actually using double quotes. However, the preprocessor replies that it cannot find the (shortcut) folder. If I copy my original folder to the location of my source code, it works fine. However, to avoid redundancy, I do not want that – Python_user Nov 21 '16 at 06:58
  • 1
    @Python_user Without knowing your exact disk directory structure it's really impossible to give you a definitive answer, But I updated my answer with an example that hopefully helps you. – Some programmer dude Nov 21 '16 at 07:02
  • I have my "original" files in some location. I do not want to copy those files into my new project to avoid redundancy. So, I created shortcuts to them, copied those shortcuts into my project and tried to include the relative path to the shortcuts in my code. That's where I am getting an error. – Python_user Nov 21 '16 at 07:05
  • However, if I include the relative path to my original source code, it works fine. But, I dont want that since I want to make my project "self-contained". – Python_user Nov 21 '16 at 07:06
  • @Python_user Then I *really* recommend you add the original path to your project settings. – Some programmer dude Nov 21 '16 at 07:08
  • Thanks, in the worst case I will be doing that. But is there any way to make them work with shortcuts (thats what my doubt is!) since I want make my project "self-contained" (in some sense)? – Python_user Nov 21 '16 at 07:10
  • Or rather. I want to make my project "appear" self-contained. – Python_user Nov 21 '16 at 07:25
  • @Someprogrammerdude, is the inconsistency between `include` and `Include` in your example intentional? I think it is not, but I cannot correct only two characters. – Enlico Nov 21 '19 at 10:43
  • @EnricoMariaDeAngelis Just a typo. Thanks for pointing it out – Some programmer dude Nov 21 '19 at 13:23
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.

Community
  • 1
  • 1
khôi nguyễn
  • 626
  • 6
  • 15
  • 1
    Thanks, is it possible for you to refer to the conversation in the answer given by @Some programmer dude? (since it would avoid restating my query again) – Python_user Nov 21 '16 at 07:13