您如何在C中嵌入资源文件? [英] How do you embed resource files in C?
问题描述
我知道如何执行此操作的唯一方法是使用单个字节/字符数组将文件转换为C源文件,该数组包含以十六进制表示的资源文件的内容.
The only way I know how to do this is to convert the file into a C source file with a single byte/char array containing the contents of the resource file in hex.
是否有更好或更简单的方法来做到这一点?
Is there a better or easier way to do this?
推荐答案
这是我与gcc-arm交叉编译器一起使用的好技巧;包括通过汇编语言文件的文件.在此示例中,这是我包括的文件public_key.pem
的内容.
Here's a nice trick I use with a gcc-arm cross compiler; including a file through an assembly language file. In this example it's the contents of the file public_key.pem
I'm including.
pubkey.s
.section ".rodata"
.globl pubkey
.type pubkey, STT_OBJECT
pubkey:
.incbin "public_key.pem"
.byte 0
.size pubkey, .-pubkey
对应的 pubkey.h
#ifndef PUBKEY_H
#define PUBKEY_H
/*
* This is a binary blob, the public key in PEM format,
* brought in by pubkey.s
*/
extern const char pubkey[];
#endif // PUBKEY_H
现在,C语言的源代码可以包含pubkey.h
,用gcc编译pubkey.s
并将其链接到您的应用程序中,然后就可以了. sizeof(pubkey)
也可以.
Now the C sources can include pubkey.h
, compile the pubkey.s
with gcc and link it into your application, and there you go. sizeof(pubkey)
also works.
这篇关于您如何在C中嵌入资源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!