Search code examples
c++compilationincludefile-handlingpreprocessor-directive

How to read a file at compile time in C++?


I have been trying to look for a way to store files as const char* at compile time.

I tried doing this:

#define STRINGIFY(...) #__VA_ARGS__ // va args deals with commas
#define INCLUDE(path) #include path // error: '#' is not followed by a macro parameter

constexpr const char* source = STRINGIFY(INCLUDE("source.txt"));

but unfortunately you can't place include directives inside a macro expansion. Is there any way around this problem? Or any other way to get the contents of a file at compile time?


Solution

  • the classic way to do this is to write an awk or perl script that reads the data file and generates a .H file