3

I have a few large static arrays that are used in a resource constrained embedded system (small microcontroller, bare metal). These are occasionally added to over the course of the project, but all follow that same mathematical formula for population. I could just make a Python script to generate a new header with the needed arrays before compilation, but it would be nicer to have it happen in the pre-processor like you might do with template meta-programming in C++. Is there any relatively easy way to do this in C? I've seen ways to get control structures like while loops using just the pre-processor, but that seems a bit unnatural to me.

Here is an example of once such map, an approximation to arctan, in Python, where the parameter a is used to determine the length and values of the array, and is currently run at a variety of values from about 100 to about 2^14:

def make_array(a):
    output = []
    for x in range(a):
        val = round(a * ((x / a)**2 / (2 * (x / a)**2 - 2 * (x / a) + 1)))
        output.append(val)
    return output
9
  • 2
    We'd need to know what the formulas look like, can you provide regular runtime code to populate the arrays, either in C or Python? Commented Mar 8, 2022 at 14:28
  • 3
    Although it does depend somewhat on the details of what you are trying to do, chances are that no, there is not any "relatively easy" way to do this via the preprocessor. At minimum, your proposed code generator is almost certain to be much easier to write, read, debug, and maintain. Commented Mar 8, 2022 at 14:35
  • The C preprocessor is a preprocessor. Python, when used this way, is a preprocessor. There is no good reason to prefer the C preprocessor except that it is built in. Use the one that suits the task better Commented Mar 8, 2022 at 14:38
  • 1
    Can you please post some code example even if it's just pseudo code? Commented Mar 8, 2022 at 14:55
  • @EricPostpischil I agree with this in general, but it adds more overhead to each build, and a dev working on this after I'm long gone (or a future me) might not be aware of this step. It would be more natural if the "typical" build process took care of it without the extra steps. I can document it and create build scripts and all, but that doesn't mean the new guy will follow them correctly. I can't guarantee everything will always go smoothly, but adding extra steps, if they are unnecessary, increases the likelihood of problems. Commented Mar 8, 2022 at 16:06

1 Answer 1

2

Is there any relatively easy way to do this in C?

No.

Stick to a Python script and incorporate it inside your build system. It is normal to generate C code using other scripts. This will be strongly relatively easier than a million lines of C code.

Take a look at M4 or Jinja2 (or PHP) - these macro processors allow sharing code with C source in the same file.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.