0

I was trying to read a file in compile time. My code is as below

#define STR(x) #x
const char *fileContent = STR(
       #include "config.txt"
);

The content of config.txt is

ABC

DEF

What my expected value of fileContent is

ABC

DEF

but I got the

#include "config.txt"

for fileContent. It seems the order of replacing symbol in visual c++ is #define then #include. Can I change the order to #include then #define ? Or any suggestion for reading file to char* in compile time ?

Community
  • 1
  • 1
Rick
  • 185
  • 7
  • 2
    There is the [std::embed proposal](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1040r0.html). – Jarod42 Jun 20 '19 at 12:24
  • 1
    @Jarod42 _"Every C and C++ programmer -- at some point -- attempts to #include large chunks of non-C++ source into their code. Of course, #include expects the format of the data to be source code, and thusly the program fails with spectacular lexer errors."_ Strange claim. I've certainly never tried to `#include` non-code into a source file expecting it to magically work, and I can't imagine that even "most" programmers have! Interesting proposal though... – Lightness Races in Orbit Jun 20 '19 at 12:27
  • 1
    @LightnessRacesinOrbit: including resources in exe allows to have stand alone executable and avoid possible relative path issue and in some extend might "hide" data for simple user. – Jarod42 Jun 20 '19 at 12:34
  • Since I have a server-client architecture project and I need to make sure that both of them are handling same struct define. What my proposal is computing the MD5 of struct header file and compare it before communication. So far, Im trying to compute file MD5 at compile time. – Rick Jun 20 '19 at 12:49
  • You can compute md5 in build process then. – Jarod42 Jun 20 '19 at 13:04
  • @Jarod42 Sorry, perhaps I wasn't clear. Bringing static data into a source file at build-time is completely reasonable and normal, and I've done it plenty of times (some ways to go about it are explored in the question I linked to from my answer) - what's _not_ reasonable and normal is "including large chunks of non-C++ source into code" in a place that expects C++ code, which is what the quoted text seems to bizarrely indicate that "every C and C++ programmer" has done. – Lightness Races in Orbit Jun 20 '19 at 14:49
  • Possible duplicate of [C/C++, can you #include a file into a string literal?](https://stackoverflow.com/questions/1246301/) – Remy Lebeau Jun 20 '19 at 16:15

1 Answers1

2

#include is a preprocessor directive.

#define is also a preprocessor directive.

You can't embed one inside the other. Tokens inside a macro argument don't get interpreted as further preprocessor directives. This has little to do with "order" and more about nesting.

You can't just bung an #include in a string literal, either, but there are ways to get what you want.

This may get easier in future versions of C++, if the std::embed proposal gets its way.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055