I'm trying to compile a hello world program using PETSc, based off of this tutorial, slide 33 . How would I compile this? I know that I can't use a simple 'mpicc' command. When running the program, would I use the standard mpirun or mpiexec?

  • $\begingroup$ did you include this at the beginning of the makefile ? CFLAGS = -I${PETSC_DIR}/include $\endgroup$ Jul 25, 2016 at 9:17

1 Answer 1


Slides from 2002 are ancient. Have a look at newer tutorials at http://www.mcs.anl.gov/petsc/documentation/tutorials/index.html

To answer your question, though, you will want to look at some of the examples in, say, $PETSC_DIR/src/ksp/ksp/examples/tutorials. In particular, the makefile is how to compile an example program.

A short example to compile hello.c:

include ${PETSC_DIR}/conf/variables
include ${PETSC_DIR}/conf/rules

hello: hello.o  chkopts
    -${CLINKER} -o hello hello.o  ${PETSC_LIB}
    ${RM} hello.o

You would then run:

export PETSC_DIR=/path/to/petsc and export PETSC_ARCH=petsc-arch

Please review the installation instructions:


After a successful installation of PETSc, run your own test:

make hello

A good tip for debugging (where you want to leave the .o files around) is to run:

make hello RM=echo

  • $\begingroup$ Shouldn't there be a hello.c somewhere in the makefile? $\endgroup$
    – Paul
    Jan 21, 2012 at 5:48
  • $\begingroup$ It doesn't seem to work. I keep getting this error: /bin/sh: LINKER}: command not found. $\endgroup$
    – Paul
    Jan 21, 2012 at 5:56
  • $\begingroup$ @Paul: When you added the new line, did you remember to put a $ in front of LINKER? $\endgroup$
    – Dan
    Jan 21, 2012 at 7:46
  • $\begingroup$ @Dan: You were almost right... there was a '{' missing before the LINKER. Now that i've changed it, I get a slightly different error: hello.c:1:19: error: petsc.h: No such file or directory I'm thinking that there's some problem with the path to the petsc.h file. $\endgroup$
    – Paul
    Jan 21, 2012 at 14:12
  • $\begingroup$ You might need to include petscksp.h. $\endgroup$
    – Dan
    Jan 21, 2012 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.