News for hackers. Stuff that matters.
Re:Embedding files in C programs with koio by EdSchouten on Wednesday May 30, 2018 @04:36PM (#17191529)
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.
Re:Embedding files in C programs with koio by planteen on Wednesday May 30, 2018 @04:52PM (#17191661)
Yes and that is the better way to do it. Compiling an enormous C file is slow. Many compilers have low limits (around 50 MB or so) but gcc has a huge limit. But even on gcc, a large file slows down the build process significantly.
Re:Embedding files in C programs with koio by jandrese on Wednesday May 30, 2018 @05:12PM (#17191841)
More importantly gcc's memory requirements balloon quickly as the size of the file grows. There are projects that can't be built on platforms like a Raspberry Pi simply because some of the source files (typically heavily templated C++ code) blow out the memory. A good example is Firefox.
Re:Embedding files in C programs with koio by jzwinck on Wednesday May 30, 2018 @07:19PM (#17192728)
I like objcopy for this. Here is an example command:

    objcopy --input binary --output elf64-x86-64 myfile.txt myfile.o
Re:Embedding files in C programs with koio by dvhh on Thursday May 31, 2018 @03:30AM (#17194607)
Re:Embedding files in C programs with koio by emmelaich on Wednesday May 30, 2018 @05:26PM (#17191981)
What is the clang or macos equivalent to objcopy?
Re:Embedding files in C programs with koio by comex on Wednesday May 30, 2018 @07:44PM (#17192858)
Pass -sectcreate to the linker, e.g. -sectcreate __DATA __foo foo.txt. Then you can reference the data like this:

    extern char foo_start[] asm("section$start$__DATA$__foo");
    extern char foo_end[] asm("section$end$__DATA$__foo");
Re:Embedding files in C programs with koio by planteen on Wednesday May 30, 2018 @05:46PM (#17192149)
You are just dealing with ELF files, so objcopy works fine in those scenarios as long as the architecture matches for the linker. This trick works many other compilers that use ELF such as clang, MSVC, Green Hills, etc.
Re:Embedding files in C programs with koio by tetromino_ on Wednesday May 30, 2018 @06:12PM (#17192357)
Except that MacOS does not use ELF, it uses Mach-O.
Re:Embedding files in C programs with koio by planteen on Wednesday May 30, 2018 @07:19PM (#17192735)
Clang handles intermediate ELF object files fine and can link into a Mach-O final executable.

Windows under MSVC doesn't use ELF either (it uses PE for the .EXE) and it links in intermediate ELF object files fine. I've made an EXE that was a mix of files compiled by MSVC and GCC.