cincludec-preprocessorstringification

Can I split a long #include directive into two lines?


I wish there was a way to split a #include directive across two lines, so that my code can conform to 80 characters per line, despite the necessity of a very long include path.

Other than expanding the search path of the compiler, how can I manage this? Is there a way to split my very long path string into two lines?

"#define" macro expansion apparently happens after #include expansion, so these don't work:

#define BIGPATH "..."
#include BIGPATH ## "/foo.c"
#include "BIGPATH/foo.c"
#include BIGPATH"/foo.c"

I've also tried

#include "foo" ##
         "bar"

and

#include "foo" \
         "bar"

To no avail. Perhaps what I want is impossible? Help me, stackoverflow kenobi, you're my only hope.

ANSWER: building on the suggested answer below, here's what actually worked for me:

#define STRINGIFY(x) #x
#define PATH(path) STRINGIFY(/my/very/long/path)
#include PATH(foo.h)
#undef PATH
#undef STRINGIFY

Solution

  • I do not like the idea of this, I just wanted to mention this possibility. The best is to go the way Daniel Fischer mentioned. This solution is a bit quirky and will not work under all circumstances but this compiled here:

    #define PATH(FILE) </path/to/FILE>
    #include PATH(file.h)
    

    And just to name some of the obvious limitations:

    Feel free to add to this list.

    Edit
    Just for better readability I will post the solution from Jonathans comment below in the style of my example:

    #define STRINGIFY(x) #x 
    #define PATH(FILE) STRINGIFY(/path/to/FILE) 
    #include PATH(foo.h)
    

    This version mitigates the "locality problem" of the #include <> version, as it maps to #include ""