title: PETSc undefined reference
body: <p>I have a pretty beginners' question, but I'm really lost now. I'm beginning with PETSc, but I have problems with compilation of my code. I'm trying to use my own Makefile, but compiler keeps yelling "undefined reference" error. I've tried to figure it out by myself for several hours, but I simply don't see the mistake. So, if you'll recognize the mistake, your help will be greatly appreciated.</p>
  
  <p>This is the whole error message:</p>
  
  <pre><code>mpicc petscLUFact.o -L/home/martin/petsc-3.5.2/arch-linux2-c-debug/lib
  petscLUFact.o: In function `main':
  /home/martin/Dokumenty/Programovani/petscLUFact.c:18: undefined reference to `PetscInitialize'
  /home/martin/Dokumenty/Programovani/petscLUFact.c:20: undefined reference to `PETSC_COMM_WORLD'
  /home/martin/Dokumenty/Programovani/petscLUFact.c:20: undefined reference to `MatCreate'
  /home/martin/Dokumenty/Programovani/petscLUFact.c:21: undefined reference to `MatSetSizes'
  /home/martin/Dokumenty/Programovani/petscLUFact.c:22: undefined reference to `MatSetFromOptions'
  /home/martin/Dokumenty/Programovani/petscLUFact.c:23: undefined reference to `MatMPIAIJSetPreallocation'
  /home/martin/Dokumenty/Programovani/petscLUFact.c:24: undefined reference to `MatGetOwnershipRange'
  /home/martin/Dokumenty/Programovani/petscLUFact.c:26: undefined reference to `MatDestroy'
  /home/martin/Dokumenty/Programovani/petscLUFact.c:28: undefined reference to `PetscFinalize'
  /home/martin/Dokumenty/Programovani/petscLUFact.c:20: undefined reference to `PetscError'
  /home/martin/Dokumenty/Programovani/petscLUFact.c:24: undefined reference to `PetscError'
  /home/martin/Dokumenty/Programovani/petscLUFact.c:23: undefined reference to `PetscError'
  /home/martin/Dokumenty/Programovani/petscLUFact.c:22: undefined reference to `PetscError'
  /home/martin/Dokumenty/Programovani/petscLUFact.c:21: undefined reference to `PetscError'
  collect2: error: ld returned 1 exit status
  make: *** [petscLUFact] Error 1
  </code></pre>
  
  <p>And this is my .c file - it's not completed, it's just a test:</p>
  
  <pre><code>static char help[] = "Reads a PETSc matrix and vector from a file and reorders it.\n\
       -f0 &lt;input_file&gt; : first file to load (small system)\n\
       -f1 &lt;input_file&gt; : second file to load (larger system)\n\n";
  
  #include &lt;petscsys.h&gt;
  #include &lt;petscmat.h&gt;
  
  int main( int argc, char **args ) {
      Mat             A; // matice
      //IS  isrow,iscol; // permutace radku a sloupcu
      PetscInt        r = 5, c = 5; // rozmery matice
      PetscInt        i,j; // souradnice v matici
      PetscInt        Istart, Iend;
      PetscInt        Ii; // pocitadlo
      PetscScalar     v; // 2-rozmerne pole ???
      PetscErrorCode  ierr;
  
      PetscInitialize( &amp;argc, &amp;args, (char*)0, help );
  
      ierr = MatCreate( PETSC_COMM_WORLD, &amp;A );CHKERRQ( ierr );
      ierr = MatSetSizes( A, PETSC_DECIDE, PETSC_DECIDE, r*c, r*c );CHKERRQ(ierr);
      ierr = MatSetFromOptions(A);CHKERRQ(ierr);
      ierr = MatMPIAIJSetPreallocation( A, 5, PETSC_NULL, 5, PETSC_NULL );CHKERRQ(ierr);
      ierr = MatGetOwnershipRange(A,&amp;Istart,&amp;Iend);CHKERRQ(ierr);
  
      MatDestroy(&amp;A);
  
      PetscFinalize();
  
      return 0;
  }
  </code></pre>
  
  <p>Here is my Makefile:</p>
  
  <pre><code>include ${PETSC_DIR}/conf/variables
  include ${PETSC_DIR}/conf/rules
  
  CFLAGS=-I${PETSC_DIR}/include -I${PETSC_DIR}/${PETSC_ARCH}/include
  
  petscLUFact: petscLUFact.o
      mpicc petscLUFact.o -L${LD_LIBRARY_PATH}
  
  petscLUFact.o: petscLUFact.c
      mpicc ${CFLAGS} -c petscLUFact.c -o petscLUFact.o
  </code></pre>
  
  <p>In <code>$PETSC_DIR/include</code> and <code>${PETSC_DIR}/${PETSC_ARCH}/include</code> there are petsc header (.h) files located.</p>
  
  <p>Values of my system variables are:</p>
  
  <pre><code>$PETSC_DIR=/home/martin/petsc-3.5.2
  $PETSC_ARCH=arch-linux2-c-debug
  $LD_LIBRARY_PATH=/home/martin/petsc-3.5.2/arch-linux2-c-debug/lib
  </code></pre>
  
  <p>And this is the structure of my LD_LIBRARY_PATH folder:</p>
  
  <pre class="lang-none prettyprint-override"><code>arch-linux2-c-debug/lib
  ├── libpetsc.a
  ├── libpetsc.so -&gt; libpetsc.so.3.5.2
  ├── libpetsc.so.3.5 -&gt; libpetsc.so.3.5.2
  ├── libpetsc.so.3.5.2
  ├── modules
  │   └── 3.5.2-arch-linux2-c-debug
  └── pkgconfig
      └── PETSc.pc
  </code></pre>