title: How to embed a file into an executable file?
body: <p>I have two problems, the first has been solved.</p>
  
  <p><strong>Current problem</strong><br>
  If I embed a file that requires a library to load it, such as a jpeg image or a mp3 music, I will need to use the file as input to the library. However, each library is different and uses a way to get a file as input, the input may be the file name or a FILE* pointer (from libc's file interface).</p>
  
  <p>I would like to know how to access an embedded file with a name. It will be inefficient if I create a temporary file, is there another way? Can I map a file name to memory? My platforms are Windows and Linux.</p>
  
  <p>If <strong>show_file(const char* name)</strong> is a function from a library, I will need a string to open the file.<br>
  I have seen these questions:<br>
  <a href="https://stackoverflow.com/questions/1558772/how-to-get-file-descriptor-of-buffer-in-memory">How to get file descriptor of buffer in memory?</a><br>
  <a href="https://stackoverflow.com/questions/1188757/getting-filename-from-file-descriptor-in-c">Getting Filename from file descriptor in C</a></p>
  
  <p>and the following code is my solution. Is it a good solution? Is it inefficient?</p>
  
  <pre><code># include &lt;stdio.h&gt;
  # include &lt;unistd.h&gt;
  
  extern char _binary_data_txt_start;
  extern const void* _binary_data_txt_size;
  const size_t len = (size_t)&amp;_binary_data_txt_size;
  
  void show_file(const char* name){
      FILE* file = fopen(name, "r");
      if (file == NULL){
          printf("Error (show_file): %s\n", name);
          return;
      }
  
      while (true){
          char ch = fgetc(file);
          if (feof(file) )
              break;
          putchar( ch );
      }
      printf("\n");
  
      fclose(file);
  }
  
  int main(){
  
      int fpipe[2];
      pipe(fpipe);
  
      if( !fork() ){
          for( int buffsize = len, done = 0; buffsize&gt;done; ){
              done += write( fpipe[1], &amp;_binary_data_txt_start + done, buffsize-done );
          }
          _exit(0);
      }
  
      close(fpipe[1]);
      char name[200];
      sprintf(name, "/proc/self/fd/%d", fpipe[0] );
  
      show_file(name);
  
      close(fpipe[0]);
  }
  </code></pre>
  
  <hr>
  
  <p><strong>The other problem (solved)</strong><br>
  I tried to embed a file on Linux, with GCC, and it worked. However, I tried to do the same thing on Windows, with Mingw, and it did not compile.</p>
  
  <p>The code is:</p>
  
  <pre><code># include &lt;stdio.h&gt;
  
  extern char _binary_data_txt_start;
  extern char _binary_data_txt_end;
  
  int main(){
      for (char* my_file = &amp;_binary_data_txt_start; my_file &lt;= &amp;_binary_data_txt_end; my_file++)
          putchar(*my_file);
      printf("\n");
  }
  </code></pre>
  
  <p>The compilation commands are:</p>
  
  <pre><code>objcopy --input-target binary --output-target elf32-i386 --binary-architecture i386 data.txt data.o
  g++ main.cpp data.o -o test.exe
  </code></pre>
  
  <p>On Windows, I get the following compiler error:</p>
  
  <pre><code>undefined reference to `_binary_data_txt_start'
  undefined reference to `_binary_data_txt_end'
  </code></pre>
  
  <p>I tried to replace <strong>elf32-i386</strong> with <strong>i386-pc-mingw32</strong>, but I still get the same error.</p>