8

I am cross compiling a program for a bare-metal environment and I want to have an array populated with the data I have stored in a file. Is there a way to do this read during compile-time?

Reason: Copy-pasting the data into the source seems ugly.

Ahmed Salman Tahir
  • 1,783
  • 1
  • 17
  • 26
Moberg
  • 5,253
  • 4
  • 38
  • 54
  • 1
    You can write a "driver" program that generates your source file. It'll read the data from the file and insert it into your source file. –  Jan 10 '14 at 11:55
  • 1
    You can integrate `xxd -i` or `objcopy` into your build. See http://stackoverflow.com/questions/1155578/which-program-creates-a-c-array-given-any-file – Andreas Fester Jan 10 '14 at 11:55
  • I'm not sure but you can take a look at `constexpr` in c++11. – holgac Jan 10 '14 at 11:56
  • @Moberg http://www.manpagez.com/man/1/xxd/ –  Jan 10 '14 at 12:14

2 Answers2

6

Part of your build process can be to run a program which takes the file as input and generates a C++ source file which defines it as an array, something like:

char arrayFromFile[] = {
    0x01, 0x02, 0x99, ...  and so on
};

The program itself could be part of your source code.

Then just compile that program later in the build cycle. For example, you may have the following makefile segment:

generate: generate.cpp
    g++ -o generate generate.cpp    # build data generator

data.cpp: data.dat
    generate data.dat >data.cpp     # create c file with data

prog: prog.cpp data.cpp
    g++ -o prog prog.cpp data.cpp   # create program from source and data
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

Both GCC and Clang can be used to pack arbitrary files into a .o as static strings. There ends up being a bit of complexity around mapping what symbol names it uses to what the programmer thinks of that file as (particularly if the "physical" and "logical" file paths in the build are not the same thing), but the core of the process is demonstrated here and, ignoring corner cases, boils down to:

gcc ... -Wl,--format=binary -Wl,$FILE1 -Wl,$FILE2 ...

(Projects that uses Bazel as their build tool may be able to just use the containing bzl rule directly.)

BCS
  • 75,627
  • 68
  • 187
  • 294