Is it possible to read a file at compile time?

Building on teivaz’s idea, I wonder if the usual “stringize after expansion” trick will work:

#define STRINGIZE(...) #__VA_ARGS__
#define EXPAND_AND_STRINGIZE(...) STRINGIZE(__VA_ARGS__)

constexpr std::string shader_source = EXPAND_AND_STRINGIZE(
#include "~/.foo.glsl"
);


Still, I would go for a conventional extern const char[] declaration resolved to the content by the linker. The article “Embedding a File in an Executable, aka Hello World, Version 5967” has an example:

# objcopy --input binary \
          --output elf32-i386 \
          --binary-architecture i386 data.txt data.o

Naturally you should change the --output and --binary-architecture commands to match your platform. The filename from the object file ends up in the symbol name, so you can use it like so:

#include <stdio.h>

/* here "data" comes from the filename data.o */
extern "C" char _binary_data_txt_start;
extern "C" char _binary_data_txt_end;

main()
{
    char*  p = &_binary_data_txt_start;

    while ( p != &_binary_data_txt_end ) putchar(*p++);
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)