Here is hello.cpp. I'm on OS X. I built PETSc directly from source.
#include <petscvec.h>
int main(int argc,char **argv)
{
Vec x;
PetscInitialize(&argc, &argv, NULL, NULL);
VecCreateSeq(PETSC_COMM_SELF, 100, &x);
VecSet(x, 1.);
PetscFinalize();
return 0;
}
And here is my makefile
PETSC_BASE_DIR=/Users/buddha/src/petsc
PETSC_LIB_DIR=${PETSC_BASE_DIR}/lib
PETSC_INCLUDE_DIR=${PETSC_BASE_DIR}/include
INCLUDES=$(PETSC_INCLUDE_DIR)
LIBS=$(PETSC_LIB_DIR)
CC=g++
EXEC=oy
ARGS=-Wall -lstdc++
CPP=hellp.cpp
#CPP=oy.cpp
all: hello.cpp
$(CC) $(ARGS) -I$(INCLUDES) -L$(LIBS) -o $(EXEC) $<
clean:
rm $(EXEC)
run:
./$(EXEC)
Which yields
make
g++ -Wall -lstdc++ -I/Users/buddha/src/petsc/include
-L/Users/buddha/src/petsc/lib -o oy hello.cpp
Undefined symbols for architecture x86_64:
"_PetscFinalize", referenced from:
_main in hello-924d8b.o
"_PetscInitialize", referenced from:
_main in hello-924d8b.o
"_VecCreateSeq", referenced from:
_main in hello-924d8b.o
"_VecSet", referenced from:
_main in hello-924d8b.o
"_ompi_mpi_comm_self", referenced from:
_main in hello-924d8b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1
It appears the library is linking correctly. Much of the chatter about this topic says to try using g++, which I am doing.
You haven't actually linked against the library file, only told the compiler about the library directory. You'll need to do -lPetsc or whatever the file is called in order to link properly.
Edit: On OSX you are also not using gcc unless you specifically install it. gcc/g++ is using clang unless you go and install it yourself. You can even see this in the compiler output.
Edit 2: I compiled your sample code with the following:
clang++ petsc.cpp -L/usr/local/Cellar/petsc/3.7.6_3/lib
-I/usr/local/Cellar/petsc/3.7.6_3/include -lpetsc
-L/usr/local/Cellar/open-mpi/2.1.1/lib -lmpi
So, as I said, you were missing -lpetsc
. Upon adding this flag, I found that petsc needed Open-MPI, so I added the library for that too (-lmpi
)
The general rule for working out what you link against in OS X:
locate
the library you want in the terminal. This gives you your directory path that you will need. It will build a locate database on first run if you have never used this command before.
Go to the library directory and you'll see a bunch of .dylib or .as or whatever. They usually start with a lib. This means you remove the lib, and the precise -l
command you need is -l(remainingname)
. I hope this helps for the future.