title: G++/GCC Compiler is giving undefined reference error when optiomzation is turned off
body: <p>I have a HUGE C++ program which compiles fine with -O3 optimization but gives a "undefined reference to strings_array_multilevel variable" error, when compiled with -g option (-O3 is removed).</p>
  
  <p>I have reduced the testcase somewhat. If you have encountered a similar error, let me know how you fixed it. I have a library named libtest.so. The file test.c is compiled into a library using standard gcc commands. The thing to note is that header file test.h has an array declared as an extern (<strong>strings_array_multilevel</strong>)</p>
  
  <pre><code>const struct mystruct elem3 =  {
                          {30, 9, 26},
                          {
                              {
                                  {9, 9},
                                  {9, 9}
                              },
                              {
                                  {9, 11},
                                  {9, 11}
                              }
                          },
                          (char**)strings_array_multilevel
                      };
  </code></pre>
  
  <p>test.h : Please see here <a href="http://pastebin.com/rPULDeXk" rel="nofollow">http://pastebin.com/rPULDeXk</a></p>
  
  <pre><code>include &lt;stdio.h&gt;
  
  const int NUM_AIRORTS = 10;
  const int NUM_REGIONS = 1024;
  
  struct mystruct {
      int field1[3];
      int field2[2][2][2];
      char ** strings;
  };
  
  extern const char* strings_array_multilevel[NUM_AIRORTS][NUM_REGIONS];
  
  const struct mystruct elem0 = {
                              {0,0,0},  
                              {
                                  {
                                      {0,0},
                                      {0,0}
                                  },
                                  {
                                      {0,0},
                                      {0,0}
                                  }
                              },
                              0
                          };
  
  const struct mystruct elem1 = {
                          {30, 9, 26},
                          {
                              {
                                  {9, 9},
                                  {9, 9}
                              },
                              {
                                  {9, 11},
                                  {9, 11}
                              }
                          },
                          NULL
                      };
  
  const struct mystruct elem2 = {
                         {0,0,0},  
                         {
                             {
                                 {0,0},
                                 {0,0}
                             },
                             {
                                 {0,0},
                                 {0,0}
                             }
                         },
                         NULL
                     };                    
  
  const struct mystruct elem3 = {
                          {30, 9, 26},
                          {
                              {
                                  {9, 9},
                                  {9, 9}
                              },
                              {
                                  {9, 11},
                                  {9, 11}
                              }
                          },
                          (char**)strings_array_multilevel
                      };
  
  
  
  int doStuff (int index1, int latency);
  </code></pre>
  
  <p>test.c : Please see here <a href="http://pastebin.com/GnSZAZUx" rel="nofollow">http://pastebin.com/GnSZAZUx</a></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "test.h"
  
  const struct mystruct* mystructarray[4] = {&amp;elem0, &amp;elem1, &amp;elem2, &amp;elem3};
  
  int doStuff (int index1, int index) {
      int retVal = 0;
      retVal = mystructarray[index1]-&gt;field1[index];
      return retVal;
  }
  
  const char* strings_array_multilevel[NUM_AIRORTS][NUM_REGIONS] =
  {
      {
          "SJC", "SFO", "OAK"
      },
      {
          "NYC", "WP", "LGA", "PHY"
      }
  };
  </code></pre>
  
  <p>Makefile :Please see here <a href="http://pastebin.com/y4LMg2SR" rel="nofollow">http://pastebin.com/y4LMg2SR</a></p>
  
  <pre><code>all:
          g++ -fpic -c test.c -o test.o
          g++ -shared -o libtest.so test.o
  
  clean:
          rm test.o libtest.so
  </code></pre>
  
  <p>Now, when I include test.h in my a project and link it against libtest.so with option -g, I get following error</p>
  
  <pre><code>cat use_library.c 
  #include &lt;stdio.h&gt; 
  int main () { 
      printf ("hello world\n"); 
      return 0; 
  }
  </code></pre>
  
  <p>Following works.</p>
  
  <pre><code>g++ -O3 use_library.c -L&lt;original_directory&gt; -ltest     
  </code></pre>
  
  <p>The following fails</p>
  
  <pre><code>g++ -g use_library.c -L&lt;original_directory&gt; -ltest     
  
  
  Undefined reference to  strings_array_multilevel
  </code></pre>
  
  <p>Please let me know if you can figure this out. Sorry, because of private nature of code, I cannot share it here.</p>
  
  <p>Thanks</p>