title: Segmentation fault / glibc detected when creating shared library
body: <p>EDITS----------------I tried with gcc-4.8.1 and still the same error.-------------I am trying to implement a simple matrix multiplication example using pthreads via a shared library. But I get this error when I try to create a shared library:</p>
  
  <pre><code>g++ -shared -o libMatmul.so matmul.o 
  collect2: ld terminated with signal 11 [Segmentation fault], core dumped
  </code></pre>
  
  <p>Here is the code I am using:
  matmul.h:</p>
  
  <pre><code>#ifndef matmul_h__
  #define matmul_h__
  #define SIZE 10 
  
  typedef struct {
  
  int dim;
  int slice;
  } matThread;
  
  int num_thrd;
  int A[SIZE][SIZE], B[SIZE][SIZE], C[SIZE][SIZE];
  int m[SIZE][SIZE];
  extern void init_matrix(int m[SIZE][SIZE]);
  extern void print_matrix(int m[SIZE][SIZE]);
  extern void* multiply(void* matThread);
  
  #endif
  </code></pre>
  
  <p>matmul.c:</p>
  
  <pre><code>extern "C"
   {
      #include &lt;pthread.h&gt;
      #include &lt;unistd.h&gt;
   }
  #include &lt;iostream&gt;
  #include "matmul.h"
  
  using namespace std ;
  matThread* s=NULL;
  // initialize a matrix
  void init_matrix(int m[SIZE][SIZE])
  {
    int i, j, val = 0;
    for (i = 0; i &lt; SIZE; i++)
    for (j = 0; j &lt; SIZE; j++)
    m[i][j] = val++;
  }
  
  void print_matrix(int m[SIZE][SIZE])
  {
    int i, j;
    for (i = 0; i &lt; SIZE; i++) {
      cout&lt;&lt;"\n\t|" ;
      for (j = 0; j &lt; SIZE; j++)
        cout&lt;&lt;m[i][j] ;
        cout&lt;&lt;"|";
      }
  }
   // thread function: taking "slice" as its argument
  void* multiply(void* param)
  {
    matThread* s = (matThread*)param;   // retrive the slice info
    int slice1=s-&gt;slice;
    int D= s-&gt;dim=10;
    int from = (slice1 * D)/num_thrd; // note that this 'slicing' works fine
    int to = ((slice1+1) * D)/num_thrd; // even if SIZE is not divisible by num_thrd
    int i,j,k;
     cout&lt;&lt;"computing slice  " &lt;&lt; slice1&lt;&lt;" from row "&lt;&lt; from&lt;&lt; " to " &lt;&lt;to-1&lt;&lt;endl;
    for (i = from; i &lt; to; i++)
    {  
      for (j = 0; j &lt; D; j++)
      {
        C[i][j] = 0;
        for ( k = 0; k &lt; D; k++)
        C[i][j] += A[i][k]*B[k][j];
      }
     }
    cout&lt;&lt;" finished slice "&lt;&lt;slice1&lt;&lt;endl;
    return NULL;
   }
  </code></pre>
  
  <p>main.c:</p>
  
  <pre><code>extern "C"
  {
  #include &lt;pthread.h&gt;
  #include &lt;unistd.h&gt;
  }
  #include &lt;iostream&gt;
  #include "matmul.h"
  using namespace std;
  
  // Size by SIZE matrices
  // number of threads
  matThread* parm=NULL;
  
  int main(int argc, char* argv[])
  {
    pthread_t* thread;  // pointer to a group of threads
    int i;
     if (argc!=2)
    {
     cout&lt;&lt;"Usage:"&lt;&lt; argv[0]&lt;&lt;" number_of_threads"&lt;&lt;endl;
     exit(-1);
    }
    num_thrd = atoi(argv[1]);
    init_matrix(A);
    init_matrix(B);
    thread = (pthread_t*) malloc(num_thrd*sizeof(pthread_t));
    matThread *parm = new matThread();
    for (i = 0; i &lt; num_thrd; i++)
    {
  parm-&gt;slice=i;
      // creates each thread working on its own slice of i
      if (pthread_create (&amp;thread[i], NULL, multiply, (void*)parm) != 0)
      {
        cerr&lt;&lt;"Can't create thread"&lt;&lt;endl;
        free(thread);
        exit(-1);
      }
    }
   for (i = 1; i &lt; num_thrd; i++)
    pthread_join (thread[i], NULL);
     cout&lt;&lt;"\n\n";
     print_matrix(A);
     cout&lt;&lt;"\n\n\t *"&lt;&lt;endl;
     print_matrix(B);
     cout&lt;&lt;"\n\n\t="&lt;&lt;endl;
     print_matrix(C);
     cout&lt;&lt;"\n\n";
     free(thread);
  
    return 0;
  
  }
  </code></pre>
  
  <p>The commands that I use are:
  g++ -c -Wall -fPIC matmul.cpp -o matmul.o and 
  g++ -shared -o libMatmul.so matmul.o
  The code might look little off because I am passing SIZE(dim) in a struct when its already in #define, but this is how I want it to be implemented. Its a test program for a bigger project that I am doing. 
  Any help is greatly appreciated! Thanks in advance. </p>