将资源嵌入到可执行文件中并保持目录结构

102 天前
 zhng920823

保持目录结构可以方便根据路径查找使用.

chmod +x embed_file_c.sh

./embed_file_c.sh dir2c_test
# or
./embed_file_c.sh dir2c /path/to/resource > _data_enc.c
gcc embed_file.c -o embed_file
./embed_file
#include <stdio.h>
#include <stdint.h>
int main(int argc, char *argv[]) {
	#define _DATA_ENC_INC 1
	#include "_data_enc.c"
	for (int i = 0; i < sizeof(_data_enc_lst)/sizeof(_data_enc_lst[0]); ++i) {
		printf("%3u, %3u, %s\n", _data_enc_lst[i].off, _data_enc_lst[i].len, _data_enc_lst[i].path);
		// &_data_enc[_data_enc_lst[i].off] is the data ptr, use it by need
	}
	return 0;
}

代码 https://github.com/webd90kb/webd/tree/master/codes/scripts/embed_file_c

1245 次点击
所在节点    C
6 条回复
w568w
102 天前
单就前一个需求(嵌入资源),C23 已经标准化了: https://zh.cppreference.com/w/c/preprocessor/embed
zhng920823
102 天前
@w568w 原理基本一样的. 我这脚本可以嵌入目录
lixile
101 天前
大文件 编译时间太长了
objcopy 直接转换为.o 才更合理
zhng920823
101 天前
@lixile 实在太大的文件就外置了
codehz
101 天前
其实可以直接 zip 塞可执行文件末端,然后运行时打开自己这个程序的文件,读取即可
而且由于 zip 的特性(末端识别),这样构造出来的文件可以直接用 zip 程序附加新文件,或者用 unzip 解压()
zhng920823
101 天前
@codehz #4 这个办法好, 方便别人修改资源

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1055626

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX