How to make GCC evaluate functions at compile time?

后端 未结 3 748
广开言路
广开言路 2021-01-19 00:34

I am thinking about the following problem: I want to program a microcontroller (let\'s say an AVR mega type) with a program that uses some sort of look-up tables.

Th

3条回答
  •  孤城傲影
    2021-01-19 00:48

    What you are trying is not part of the C language. In situations like this, I have written code following this pattern:

    #if GENERATE_SOURCECODE
    int main (void)
    {
        ... Code that uses printf to write C code to stdout
    }
    #else
        // Source code generated by the code above
        ... Here I paste in what the code above generated
    
        // The rest of the program
    #endif
    

    Every time you need to change it, you run the code with GENERATE_SOURCECODE defined, and paste in the output. Works well if your code is self contained and the generated output only ever changes if the code generating it changes.

提交回复
热议问题