TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Embedding files in C programs with koio

41 点作者 yarosv将近 7 年前

8 条评论

EdSchouten将近 7 年前
Aren't there many tools out there that can already do this? I thought even objcopy(1) can turn an arbitrary file into a .o file containing a single symbol holding the data.
评论 #17191661 未加载
评论 #17192728 未加载
评论 #17194607 未加载
评论 #17191981 未加载
wahern将近 7 年前
I imagine that many situations where you might want to embed assets into a binary involve embedded work. With embedded work you often want to be able to cross-compile. Requiring that the koio tool be built first on the host architecture (as opposed to the target architecture) gets messy, especially if you can&#x27;t or don&#x27;t want to depend on having it preinstalled.<p>The koio utility might better written in POSIX shell.<p>FWIW, here&#x27;s a simple POSIX shell-compatible routine that will convert an 8-bit stream into a quoted C string<p><pre><code> cstring() { # use od to translate each byte to hexadecimal, sed to format as # proper C string od -An -tx1 -v | sed -ne &#x27;&#x2F;.&#x2F;p&#x27; | sed -e &#x27; # prefix \x to each hexadecimal pair and remove trailing space s&#x2F;\([0-9a-fA-F][0-9a-fA-F]\)[[:space:]]*&#x2F;\\x\1&#x2F;g; # quote escaped bytes s&#x2F;^[[:space:]]*&#x2F;&quot;&#x2F;; s&#x2F;$&#x2F;&quot;&#x2F;; # escape newline for all but the last line $!s&#x2F;$&#x2F; \\&#x2F;; &#x27; }</code></pre>
kccqzy将近 7 年前
Even xxd has a mode to dump a file in the way expected by C. From then it&#x27;s just another #include away.
评论 #17191678 未加载
andyonthewings将近 7 年前
I have been using a library named incbin (<a href="https:&#x2F;&#x2F;github.com&#x2F;graphitemaster&#x2F;incbin" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;graphitemaster&#x2F;incbin</a>). On Mac and Linux it doesn&#x27;t even require a cli tool to convert the file. It just embed the content using the `.incbin` directive of the inline assembler.<p>It is pretty perfect for my project, which is a deep learning application for Android. I use it to embed the CNN model file into the C++ code. It let me avoid putting it in the apk, and then loading it from Java, and then passing it to C++.
anilakar将近 7 年前
Are there any advantages vs just embedding the file as a char array? I&#x27;ve found it easier to mmap any input files anyway so as to avoid an extra level of buffering in userspace.
评论 #17192314 未加载
stevekemp将近 7 年前
As other have already mentioned there are a lot of existing solutions to this problem. I&#x27;m not averse to reimplementing tools myself, but it you&#x27;re going to do that it makes sense to add improvements along the way.<p>One obvious improvement would be to compress the stored data, via gzip&#x2F;bzip&#x2F;similar, which would result in a smaller binary. As a small side-effect the embedded resources would be less visible to anybody who ran &quot;strings&quot; against your binary.
dvh将近 7 年前
I&#x27;ve been using bin2c for years.
_pRwn_将近 7 年前
this like looking back to the 80s when we coded for Amiga &amp; Atari ST ...