How can I get the contents of a file at build time into my C++ string?

后端 未结 5 1472
猫巷女王i
猫巷女王i 2021-01-14 00:05

I have a file I am compiling in C++ in which I wish to have a string whose value is the contents of a file at the time of compilation.

In other words, I\'d like to #

相关标签:
5条回答
  • 2021-01-14 00:42

    You can use xxd -i /tmp/your_file > /tmp/your_file_as_string.c

    Which will define a string literal for you:

    $ cat /tmp/your_file_as_string.c
    unsigned char _tmp_inc[] = {
      0x2e, 0x2f, 0x42, 0x61, 0x74, 0x63, 0x68, 0x2f, 0x6e, 0x65, 0x78, 0x74,
      0x4f, 0x6f, 0x6c, 0x73, 0x2f, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x5f,
      ...
      0x74, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x6f, 0x72, 0x2e, 0x65, 0x63,
      0x0a
    };
    unsigned int _tmp_inc_len = 1513;
    

    Now you can use that defined string.

    0 讨论(0)
  • 2021-01-14 00:42

    An obvious (if somewhat naive) way is to make a system call and invoke cpp -x c++ file.cpp, and then read the contents of the stream directly into a string just like you would any other file-read operation.

    This invokes the C preprocessor on file.cpp in C++ mode, which is exactly identical to running #include ... on it. This value will then be stored in the string object.

    0 讨论(0)
  • 2021-01-14 00:43

    To correctly create a header with the correct null termination in the string use sed:

    xxd -i ${INPUT_FILE_NAME} | sed s/}\;/,0x00}\;/ > ${INPUT_FILE_PATH}.h
    

    See http://gamesfromwithin.com/quick-tip-working-with-shaders-on-ios for details.

    0 讨论(0)
  • 2021-01-14 00:53

    The only way I can think of is to have another file generated off your file as a pre-compile step. You can build a small utility which will do it for you. This utility can take care of double-quotes and you can call it before compilation in your build file

    0 讨论(0)
  • 2021-01-14 01:05

    Try to see if your compiler/linker suite has a program called something like bin2obj. If it exists, it will usually take any file and generate a .o or .obj file that you can instruct the linker to link into your program. (How to do this varies by build environment.)

    Typically, then, you'll end up with one or two symbols you can make visible to your code like so:

    extern "C" const char * myFile_start;
    extern "C" unsigned int myFile_size;
    

    The linker will make sure myFile_start points at the start of your file, and myFile_size gives you an indication of the length.

    The symbol names may vary, though: you can use the objdump or nm or dumpbin utilities (again, depending on compiler/linker suite) to investigate the generated .o or .obj to find the symbol names (if they're not already documented in your obj2bin tool documentation).

    Please note that this bloats your executable (usually the .data or .rodata sections or equivalent), which can be a problem on some platforms.

    (If you don't have a bin2obj, you may be able to find one by hunting around on a search engine for "bin2obj" with your compiler/IDE name.)

    0 讨论(0)
提交回复
热议问题