C/C++ : exporting variable from a .h
<p>In C/C++, I would like to use from main.cpp a ready-to-use variable (mTab) that is :</p>

<ol>
<li>declared and initialized in lib.h</li>
<li>implemented in lib.cpp</li>
</ol>

<p>I can't make this work : what's wrong ? Seems typedef is ignored and I don't get why ?</p>

<p>Note :</p>

<ol>
<li>as mTab must be the same for each user of lib.h, I added the static keyword</li>
<li>as static variable must always be initialized (to avoid unexpected behavior), I initialized mTab in lib.h</li>
<li>when I move the mTab initialisation in lib.cpp : compilation is KO</li>
<li>when I move the mTab declaration and initialisation in lib.cpp, and, use "extern" in main.cpp : compilation is KO</li>
<li>the only way to get compilation OK is to put mTab declaration and initialisation inside the main scope in main.cpp... Which is not what I want ! (inside main scope compilation is OK. outside main scope, that is at include level, compilation is KO)</li>
</ol>

<p>Thanks for help,</p>

<p>FH</p>

<pre><code>~&gt;more *
</code></pre>

<p>lib.cpp</p>

<pre><code>#include "lib.h"

void m1 ( unsigned int const iDataSize, int * const iopData ) { return; }
void m2 ( unsigned int const iDataSize, int * const iopData ) { return; }
</code></pre>

<p>lib.h</p>

<pre><code>#ifndef __lib__
#define __lib__
void m1 ( unsigned int const iDataSize, int * const iopData );
void m2 ( unsigned int const iDataSize, int * const iopData );
typedef void ( *pF ) ( unsigned int const iDataSize, int * const iopData );
static pF mTab[2]; // Ready-to-use variable to be exported
mTab[0] = &amp;m1;
mTab[1] = &amp;m2;
#endif
</code></pre>

<p>main.cpp</p>

<pre><code>#include "lib.h"
#include &lt;stddef.h&gt; // NULL

int main ()
{
  (*mTab[0]) ( 0, NULL );
  (*mTab[1]) ( 0, NULL );

  return 0;
}
</code></pre>

<p>Makefile</p>

<pre><code>all:
    gcc -I. -c lib.cpp -o lib.o
    gcc -I. -c main.cpp -o main.o
    gcc -I. lib.o main.o -o main.exe
</code></pre>

<p>Console:</p>

<pre><code>~&gt;make
gcc -I. -c lib.cpp -o lib.o
In file included from lib.cpp:1:0:
lib.h:7:1: error: ‘mTab’ does not name a type
lib.h:8:1: error: ‘mTab’ does not name a type
make: *** [all] Error 1
</code></pre>
