title: Initialize global array of function pointers at either compile-time, or run-time before main()
body: <p>I'm trying to initialize a global array of function pointers at compile-time, in either C or C++.  Something like this:</p>
  
  <p><strong>module.h</strong></p>
  
  <pre><code>typedef int16_t (*myfunc_t)(void);
  extern myfunc_array[];
  </code></pre>
  
  <p><strong>module.cpp</strong></p>
  
  <pre><code>#include "module.h"
  int16_t myfunc_1();
  int16_t myfunc_2();
  ...
  int16_t myfunc_N();
  
  // the ordering of functions is not that important
  myfunc_array[] = { myfunc_1, myfunc_2, ... , myfunc_N };
  </code></pre>
  
  <p><strong>func1.cpp, func2.cpp, ... funcN.cpp</strong> (symbolic links to a single func.cpp file, so that different object files are created: func1.o, func2.o, func3.o, ... , funcN.o.  NUMBER is defined using <code>g++ -DNUMBER=N</code>)</p>
  
  <pre><code>#include "module.h"
  #define CONCAT2(x, y) x ## y
  #define CONCAT(x, y) CONCAT2(x, y)
  
  int16_t CONCAT(myfunc_, NUMBER)() { ... }
  </code></pre>
  
  <p>When compiled using g++ -DNUMBER=N, after preprocessing becomes:</p>
  
  <p><strong>func1.cpp</strong></p>
  
  <pre><code>...
  int16_t myfunc_1() { ... }
  </code></pre>
  
  <p><strong>func2.cpp</strong></p>
  
  <pre><code>...
  int16_t myfunc_2() { ... }
  </code></pre>
  
  <p>and so on.</p>
  
  <p>The declarations of <code>myfunc_N()</code> and the initialization of <code>myfunc_array[]</code> are not cool, since N changes often and could be between 10 to 200.  I prefer not to use a script or Makefile to generate them either.  The ordering of functions is not that important, i can work around that.  Is there a  neater/smarter way to do this?</p>