#include caching

Posted By: krial057

#include caching - 12/26/15 01:28

Today I foudn something new in lite-c. Apparently, multiple #inlcude's to the same file get cached in lite-c:

Example:

print.c file content:
Php Code:
printf("test"); 




main.c file content:
Php Code:
function main() {
  #include "print.c"
  #include "print.c"
} 




"test" will only get printed once in this scnario.

However changing the code to the following:
Php Code:
function main() {
  #include "print.c"
  #include "./print.c"
} 




"test" will get printed twice.

That's all I wanted to say... I know a lot about all the different things lite-c does, but I never knew about this one. Have a nice day!
Posted By: HeelX

Re: #include caching - 12/27/15 10:40

I didn't tested it, but I would strongly suggest to not rely on that. Good style is to always use #ifndef/#define/#endif guards around your code. You know what that is?
Posted By: krial057

Re: #include caching - 12/27/15 13:40

I know what include guards are. But I wanted to achieve exactly the opposite : include a file twice. Why? For X Macros :

Code:
// File: color_table.h
X(red, "red")
X(green, "green")
X(blue, "blue")

// File: main.c
#include <stdio.h>

#define X(a, b) a,
enum COLOR {
#include "color_table.h"
};
#undef X

#define X(a, b) b,
char *color_name[] = {
#include "color_table.h"
};
#undef X

int main() {
  enum COLOR c = red;
  printf("c=%s\n", color_name[c]);
  return 0;
}


Source :http://www.drdobbs.com/the-new-c-x-macros/184401387
Posted By: Kartoffel

Re: #include caching - 12/27/15 14:37

That's some weird and inconvenient way to write your code if you ask me...
Posted By: WretchedSid

Re: #include caching - 12/27/15 14:57

It's not unheard of, and can actually help a lot with decreasing the amounts of code you have to write. It's much easier to use the pre-processor to perform copy and paste than doing it yourself and trying to maintain it.

Personally I find it looks ugly, but I can see it merits as well.
© 2024 lite-C Forums