
保持目录结构可以方便根据路径查找使用.
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 ./emed_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
1 w568w 2024-07-08 12:50:19 +08:00 单就前一个需求(嵌入资源),C23 已经标准化了: https://zh.cppreference.com/w/c/preprocessor/embed |
2 zhng920823 OP @w568w 原理基本一样的. 我这脚本可以嵌入目录 |
3 lixile 2024-07-08 16:02:19 +08:00 大文件 编译时间太长了 objcopy 直接转换为.o 才更合理 |
4 zhng920823 OP @lixile 实在太大的文件就外置了 |
5 codehz 2024-07-09 09:47:54 +08:00 其实可以直接 zip 塞可执行文件末端,然后运行时打开自己这个程序的文件,读取即可 而且由于 zip 的特性(末端识别),这样构造出来的文件可以直接用 zip 程序附加新文件,或者用 unzip 解压() |
6 zhng920823 OP @codehz #4 这个办法好, 方便别人修改资源 |