2

Google is full of C-ish answers. I'm looking for std::string way to accomplish this task

  • You can't read a file at compile time in c++ yet. – cigien Aug 20 '20 at 18:00
  • @cigien But I have seen answer here https://stackoverflow.com/questions/19638557/read-a-file-into-a-string-at-compile-time which say that C++ can do this with pre-processor but I'm not sure how. The answer author is James Kanze. –  Aug 20 '20 at 18:04
  • 1
    Even if you preprocess a file and put the result in a `std::string foo = "file content";` you can't make `foo` `constexpr` since that's only allowed for literal types. – Ted Lyngmo Aug 20 '20 at 18:06
  • 1
    Why do you need this? What is the actual problem you need to solve? – Some programmer dude Aug 20 '20 at 18:07
  • You can create an object whose class constructor reads your file at run time before any code in main() runs. You can #include files that have a quoted string constant to initialize a string variable, as a MACRO, global or static-global variable. You can hang a script writing your file with wrapper code to a named pipe that looks like an include file, which you #include. But otherwise, compilers do not read other files, just each c/c++ source file and its #include's. – David G. Pickett Aug 20 '20 at 18:24

1 Answers1

2

Google is full of C-ish answers.

Those answers will work in C++ as well. C++ doesn't have features that would make this easier... yet 1.

An overview of options that are available:

  • Copy the content into a compiled object file. This is not portable across language implementations. May be achieved with a tool such as objcopy.
  • Include the file with a linker script. This is not portable across language implementations.
  • Generate a source file (based on the input file) which initialises an array of bytes that have the content. xxd tool (from the vim editor) can be used to do this. This is portable, although there may be implementation specific limits to how large files can be included like this.

It is also worth it to re-consider whether importing text file at compile time is even a good idea for your use case. Often, it is more useful to read the file at runtime because it allows the file to easily be changed without requiring re-compilation.

1 There is a proposal to add such feature to a future standard.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    Do you have any experience with this lib https://github.com/ThePhD/embed ? –  Aug 20 '20 at 18:45
  • Ok, I found that one can use patched compiler that has std::embed feature but that doesn't seem fit to my use case. Well, I guess I have to go with C style. –  Aug 20 '20 at 18:49
  • @codespeare No. It appears to use language extensions that are not in all popular compilers. – eerorika Aug 20 '20 at 18:49
  • 1
    Quoting from another answer on Stackoverflow "if you are interested in exploring the feature, http://godbolt.org has versions of gcc and clang trunk with the std::embed patches available for online use." –  Aug 20 '20 at 18:50
  • 1
    LOL: `constexpr std::span data = phd::embed("/dev/urandom", 1); static_assert((data[0] % 2) == '\0', "You lose.");` :-) – Ted Lyngmo Aug 20 '20 at 19:52
  • 1
    @TedLyngmo Another idea: `std::embed("/proc/self/cmdline")` to automatically record the compilation command – eerorika Aug 20 '20 at 20:13