Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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
like image 984
Jonathan Mayer Avatar asked Oct 04 '12 19:10

Jonathan Mayer


People also ask

Is it OK to split a long run?

Splitting up your long run not only allows you to complete your mileage, but it can help you run with better form throughout the distance. We all have a fatigue threshold at which our endurance ability fades and our form starts to deteriorate.

Can I do two long runs in a row?

The (Almost) Long Run Long runs are damaging, both physically and mentally. Without recovery, you'll never absorb their training benefits and boost your endurance. This is why most runners never do two long runs in a single week.

How do you break up a marathon?

The 10/10/10 approach to the marathon calls for splitting the race into three separate sections: the first 10 miles, the second 10 miles, and the final 10K.

What is chunking a long run?

“Chunking” is a mental strategy in which a person mentally subdivides a tasks into smaller components. The most common method in running is to break up a run into shorter distances.


1 Answers

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:

  • the search path of these includes is not "local"
  • FILE must not contain "," (rather uncommon)
  • the PATH define can still exceed 80 characters

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 ""

like image 173
Nobody moving away from SE Avatar answered Sep 26 '22 08:09

Nobody moving away from SE