Hello,
I have a simple question. Say I have two files in a folder "Cpp Projects". One file is called "main.h", and the other is inside another folder, say, "Cpp Files", and it is called "main.cpp". Thus, their filenames would be:
"...\Cpp Projects\main.h"
"...\Cpp Projects\Cpp Files\main.cpp"
Is there a way to #include main.h in main.cpp without having to enter the entire filename (for example, "C:\Users\me\Documents\Cpp Projects\main.h")?

Thanks in advance.

If a file is up one level from the current directory you can use ".." to access its parent directory.

So you can write #include "../main.h".

You can add all the paths that you want to the include-paths of the compiler (check your IDE or compiler documentation for how to do that). Then, you can include header files by name only, dropping the folder completely. Of course, this assumes that the header files don't have conflicting names. If the names conflict, then you have no choice do as you do or as sfuo suggested.

It is often typical to put all the header files in one "include" folder, possibly with sub-folders in it. Say I create a math library, I could have a folder structure like this:

/trunk
  /include
    /root_finding
    /interpolation
    /optimization
  /source
    /root_finding
    /interpolation
    /optimization
  /lib
  /bin

Where all the cpp files are in the sub-folders of the "source" folder, and all the header files are in the sub-folders of the "include" folder. And, all the libraries that I compile go into the "lib" folder and all the executables that are created go into the "bin" folder. Now, if someone else needs to use that library all he needs is the "include" folder and the "lib" folder (with compiled libraries, no source). And, the only important part is the "include" and "source" folders because the rest is generated by the compiler. Now, in the compiler options, you would add the "trunk/include" as an include-path (search directory for header files), and in all your source files you would include specific headers relatively to the "include" folder, as in #include "root_finding/newton_method.h" or #include "interpolation/cubic_spline.h" and so on. With this, you have a consistent and unambiguous way to include your headers, and it is easy to maintain because all you have to do is keep your headers in sub-folders under that "include" folder (and that can be automated with a simple script) and direct the compiler to that single include-path.

N.B.: Another popular folder-structure is to merge the "include" and the "source" folders into one, often called "src" folder (with a mix of headers and sources). Typically, you use a build-system (like make or cmake, or others) to help with that. You can also easily make a script that looks through all the sub-folders to extract only the header files to produce one clean "include" folder with only header files in it (useful if you want to "deploy" your library as code for other people to use, with pre-compiled sources).

This article has been dead for over six months. Start a new discussion instead.