How to embed a file into an executable?

前端 未结 6 1353
一个人的身影
一个人的身影 2020-11-27 18:21

I have a small demo executable wrote in C++ that depends only on one 5kb PNG image being loaded before it can run, which is used for a pixel text I made. Because of this on

6条回答
  •  猫巷女王i
    2020-11-27 18:38

    On linux I use this. It's based off a few examples I found when trying to do some 4k demos, albeit modified a bit. I believe it can work on windows too, but not with the default VS inline assembly. My workaround is #defining a macro to either use this code or the windows resource system that @MarkRansom suggests (quite painful to get working, but does work eventually).

    //USAGE: call BINDATA(name, file.txt) and access the char array &name.
    
    #ifndef EMBED_DATA_H
    #define EMBED_DATA_H
    
    #ifdef _WIN32
    //#error The VS ASM compiler won't work with this, but you can get external ones to do the trick
    #define BINDATA #error BINDATA requires nasm
    #else
    
    __asm__(
    ".altmacro\n" \
    ".macro binfile p q\n" \
    "   .global \\p\n" \
    "\\p:\n" \
    "   .incbin \\q\n" \
    "\\p&_end:\n" \
    "   .byte 0\n" \
    "   .global \\p&_len\n" \
    "\\p&_len:\n" \
    "   .int(\\p&_end - \\p)\n" \
    ".endm\n\t"
    );
    
    #ifdef __cplusplus
        extern "C" {
    #endif
    
    #define BINDATA(n, s) \
    __asm__("\n\n.data\n\tbinfile " #n " \"" #s "\"\n"); \
    extern char n; \
    extern int n##_len;
    
    #ifdef __cplusplus
        }
    #endif
    
    #endif
    
    #endif
    

提交回复
热议问题