Search code examples
c++visual-studioheaderheader-files

Including a header file in VS 2017 for "portable" projects (C++)


I decided to make a header file(s) that includes all the relatively simple methods I frequently use,to simply include said file into any project I'm working on instead of having to copy methods from one project to the other every-time I need them,I'm also going to be updating this file(s) as I go,now,I know there are multiple ways to go about including said file,either by adding it's file path to the #include directive itself,something like: #include"C:\\Projects\\MyProgram\\Files\\MyHeader.h"

Or by adding the folder containing the header file(s) to the Additional Include Directories in the project's properties,which is what I'm currently doing,and it's working just fine.

However,I'm a bit worried about the fact the header file is not included INSIDE the project folder,so in the event I had to switch computers or wipe my hard drive,I'd have to make sure that this header file(s) is placed in the same exact file path,otherwise all of the projects that include it will simply fall apart...

And it goes without saying,I'm not making a copy of the header file to place in each project folder,for obvious reasons.

So I'd like to know if there anyway around this?

How about the ability to set an Additional Include Directory for ALL of my projects,so in the event of a wipe,a new PC or simply the old directory becoming inconvenient,all I have to do is set a new directory and all of my projects will start referring to that one?

If not,is my only choice is to build the header file(s) into a custom library? Because I know absolutely nothing about that,and I'd appreciate if someone would direct me to where I can learn to do that.

Thanks in advance.


Solution

  • You must use relative path. Do this:
    1- Create a new subfolder in your solution. Let's call it include:

    enter image description here
    2- Put your shared headers in this subfolder. Example : myCommonFunctions.h

    enter image description here

    First solution: Use relative include path (see ../ at begining)

    #include "../include/myCommonFunctions.h"  
    

    Second solution: Use relative path in Addtional include directories (see ../ at begining)

    enter image description here

    Now, you can write :

    #include "myCommonFunctions.h"  
    

    By doing this, you don't depend from an absolute path C:\\Projects\\MyProgram\\... and You will no longer need to copy files manually if you change computers