Filling an array at compile time from a file

I cross-compile a program for a headless environment, and I want to have an array filled with data that I saved in a file. Is there a way to do this at compile time?

Reason: copying data to the source seems ugly.

+8
c ++ arrays
source share
1 answer

Part of your build process may be to run a program that takes a file as input and generates a C ++ source file that defines it as an array, for example:

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

The program itself may be part of your source code.

Then just compile this program later in the build cycle. For example, you might 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 
+6
source share

All Articles