How to embed a file into an executable?

前端 未结 6 1342
一个人的身影
一个人的身影 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条回答
  •  清歌不尽
    2020-11-27 18:23

    A portable way is to define a function like

    typedef unsigned char Byte;
    
    Byte const* pngFileData()
    {
        static Byte const data =
        {
            // Byte data generated by a helper program.
        };
        return data;
    }
    

    Then all you have to do is to write a little helper program that reads the PNG file as binary and generates the C++ curly braces initializer text. Edit: @awoodland has pointed out in comment to the question, that ImageMagick has such a little helper program…

    Of course, for a Windows-specific program, instead use the ordinary Windows resource scheme.

    Cheers & hth.,

提交回复
热议问题