A question about including headers…..

Hi guys!

A question about including headers…..

I have been looking all over for information about this and have had no luck.

Is it possible to use a macro PLUS text to include a header?

For example….

-----

#define myMacro “C:/myfolder/”

#include myMacro + “mysecondfolder/myfile.h”

-----

That second line obviously does not work but you can clearly see what I am trying to do. But I can not make it work. Is there a way to do this?

Thank you in advance.
There might be some string concatenation methods: https://gcc.gnu.org/onlinedocs/cpp/Concatenation.html


However, IMHO your approach is bad. If you move your project to different path, then you have to edit sources.

You could instead add "C:/myfolder/" to the list of paths that the compiler includes from. That would be in Makefile (or equivalent). If you do parametrize that, you don't even need to edit any files, just pass correct value to build tools.
I'm not sure if that's possible, at least not without some awful macro stringify hacks. You most likely do not want fragile macro systems to support directory hierarchies of #includes. Rather, you should specify to the compiler to search for additional include directories, and have C:/myfolder be an additional include directory. Then, your code just becomes #include <mysecondfolder/myfile.h> (or #include <myfolder/mysecondfolder/myfile.h>, or #include <myfile.h> depending on how you set it up). You usually don't want absolute paths in the source code itself, as this makes it less portable.

In GCC/clang, this is the -I option. For Visual Studio it's /I, see https://docs.microsoft.com/en-us/cpp/build/reference/i-additional-include-directories (also available in GUI project settings).
Last edited on
you can make a symbolic link back out if need a nasty hack or in-code answer. then you can do ./fakefolder/filename type thing in the code where fakefolder can be anywhere you can make a link to.
Topic archived. No new replies allowed.