1

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.

This is the whole error message:

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

And this is my .c file - it's not completed, it's just a test:

static char help[] = "Reads a PETSc matrix and vector from a file and reorders it.\n\
     -f0 <input_file> : first file to load (small system)\n\
     -f1 <input_file> : second file to load (larger system)\n\n";

#include <petscsys.h>
#include <petscmat.h>

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( &argc, &args, (char*)0, help );

    ierr = MatCreate( PETSC_COMM_WORLD, &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,&Istart,&Iend);CHKERRQ(ierr);

    MatDestroy(&A);

    PetscFinalize();

    return 0;
}

Here is my Makefile:

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

In $PETSC_DIR/include and ${PETSC_DIR}/${PETSC_ARCH}/include there are petsc header (.h) files located.

Values of my system variables are:

$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

And this is the structure of my LD_LIBRARY_PATH folder:

arch-linux2-c-debug/lib
├── libpetsc.a
├── libpetsc.so -> libpetsc.so.3.5.2
├── libpetsc.so.3.5 -> libpetsc.so.3.5.2
├── libpetsc.so.3.5.2
├── modules
│   └── 3.5.2-arch-linux2-c-debug
└── pkgconfig
    └── PETSc.pc
Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
Eenoku
  • 2,741
  • 4
  • 32
  • 64
  • An undefined reference error is most likely due to a reference in your source code. I suggest you include the relevant source code AND the complete error message. The error message will indicate exactly what symbol is not being found. – user3629249 Oct 21 '14 at 13:11
  • I've added needed information ;-) – Eenoku Oct 21 '14 at 13:58
  • 4
    Where are you actually passing the library to the linker (or in this case the `mpicc` command? It looks like you need a `-lpetsc` in there in addition to your `-L` flag. – Wesley Bland Oct 21 '14 at 15:21
  • Yeeah! Thank you very much, now it works. Btw, I'd like to upvote your response, but I can't? :-o – Eenoku Oct 21 '14 at 16:09

1 Answers1

2

(Answers in the comments and edit. See Question with no answers, but issue solved in the comments (or extended in chat))

@Wesley Bland wrote:

Where are you actually passing the library to the linker (or in this case the mpicc command? It looks like you need a -lpetsc in there in addition to your -L<stuff> flag.

The OP Wrote:

I added a -lpetsc flag to Makefile, so it looks like this now:

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 -o petscLUFact -L${LD_LIBRARY_PATH} -lpetsc

petscLUFact.o: petscLUFact.c
    mpicc ${CFLAGS} -c petscLUFact.c -o petscLUFact.o
Community
  • 1
  • 1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129