I have some embedded C code where I want to have a list of sine values. I have done something similar in C++ using a constexpr function that return an std::array
outside of the main loop:
#include <array>
#include <cmath>
#define sineListSize 100
#define sineListAmp 100
constexpr auto makeSineList() {
std::array<int, sineListSize> list = {};
for (int i=0; i<sineListSize; i++) {
list[i] = sineListAmp * sin(i * 2 * M_PI / sineListSize);
}
return list;
}
auto sineList = makeSineList();
The way I've done it now is just the list in a separate header file, because it has 8192 elements and goes up to the signed 32-bit integer limit. It is hard to modify (I have to generate the values and copy them in manually anytime I want to change the parameters of generation) and I have to keep all the parameters in comments. How would I do something like this in C?
The classic method is you write a program that calculates the values and writes them to output in the format of a C array initialization. As part of your project’s build process, it compiles and runs that program on the build host, directing its output to a header file. That header file is included in your other sources.