link error

Keita Teranishi keita at
Sat Feb 14 17:52:08 CST 2009


In order to link PETSc library (it applies any linking operations for most platforms), you must place all .a files in the order of dependency.  For example, -lpetscksp is placed before -lpetscmat and -lpetscvec, and these two are placed before -lpetsc.  If you follow PETSc's makefile format (see PETSc manual), these .a files are substituted by ${PETSC_XXX_LIB} variables and all .a files are placed in the correct order.

If you still want to write makefile on your own, you need place them something like "-lpetscsnes -lpetscksp -lpetscdm -lpetscmat -lpetscvec -lpetsc". 

 Keita Teranishi
 Scientific Library Group
 Cray, Inc.
 keita at

-----Original Message-----
From: petsc-users-bounces at [mailto:petsc-users-bounces at] On Behalf Of Yixun Liu
Sent: Saturday, February 14, 2009 4:56 PM
To: PETSc users list
Subject: Re: link error

Satish Balay wrote:
> Don't know cmake.
> You'll have to make sure 'all' compiler options used to compile the
> PETSc example are also used in your build system. If you miss
> something - you'll have to go back - and add it in.
> So:
> - look at the make output of compiling a PETSc example
> - look at the make output from compiling your code.
> - look for whats missing - and add it to your cmake stuff..
> [most likely you are missing "-lX11" at link time.]
> Satish
> On Wed, 11 Feb 2009, Yixun Liu wrote:
>>  I can successfully compile the example with PETSC
>> makefile. However, I need integrate PETSC into an existing
>> application, which use CMake to generate makefile. I add the
>> following into CMakelist.txt and use CMake to generate makefile and
>> then make.
>> #----------
>> # PETSc
>> #----------
>> SET(PETSC_DIR /home/scratch/yixun/petsc-3.0.0-p3)
>> SET(PETSC_ARCH /linux-gnu-c-debug)
>> SET(X11_DIR /usr/X11R6)
>> ${PETSC_DIR}/${PETSC_ARCH}/include)
>> ${X11_DIR}/lib/X11)
When I add the following in my application, there are many link errors.
Vec x,b,u;
Mat K;
KSP ksp;
PetscMPIInt size, rank;

In function `VecInitializePackage':
undefined reference to `PetscObjectComposedDataRegister'
In function `PetscMapSetUp':
undefined reference to `PetscSplitOwnership'
In function `ISSetPermutation':
undefined reference to `PetscSortInt'
In function `ISLocalToGlobalMappingGetInfo':
undefined reference to `PetscSortIntWithArray'

I can make ex2 using command make ex2. The link command is like following,

mpicc -Wall -Wwrite-strings -Wno-strict-aliasing -g3 -o ex2 ex2.o
-L/home/scratch/yixun/petsc-3.0.0-p3/linux-gnu-c-debug/lib -lpetscksp
-lpetscdm -lpetscmat -lpetscvec -lpetsc -lX11 -llapack -lblas -lm
-L/usr/lib64 -L/usr/lib64/gcc/x86_64-suse-linux/4.2.1 -L/lib64
-L/usr/x86_64-suse-linux/lib -ldl -llammpio -llamf77mpi -lmpi -llam
-lutil -lgcc_s -lpthread -lgfortranbegin -lgfortran -lm -lm -ldl
-llammpio -llamf77mpi -lmpi -llam -lutil -lgcc_s -lpthread -ldl

The command I use to link my application is like,

/usr/bin/mpiCC -DWITHOUT_MTL -D_YAV_ -D_LINUX_ -ansi -Wall -fsigned-char
-fsigned-bitfields -Wno-deprecated -ftemplate-depth-50 -Wall -fPIC
CMakeFiles/PETScSolver.dir/Point.o -o ../../../bin/PETScSolver -rdynamic
-L/home/scratch/yixun/ITK-mpicc/Build/bin -L/lib/LINUX
-L/usr/X11R6/lib64 -L/usr/X11R6/lib/X11
../../../lib/libTetrahedrisationNG.a ../../../lib/libTetraImageNG.a
../../../lib/libImageDataNG.a -lITKCommon -lITKIO -lvtkIO -lpetsc
-lpetscksp -lpetscmat -lpetscvec -lpetsccontrib -lpetscdm -lpetscsnes
-lpetscts -lX11 -llapack -lblas -llam -lutil -lm -llammpio -lmpi -lgcc_s
-lpthread -lgfortranbegin -ldl ../../../lib/libTetrahedrisationNG.a
../../../lib/libGraphics2DNG.a ../../../lib/libGraphics3DNG.a
../../../lib/libInrimageNG.a ../../../lib/libBasics.a
../../../lib/libmipsZ.a -lITKNrrdIO -litkgdcm -litkjpeg12 -litkjpeg16
-litkopenjpeg -luuid -litkpng -litktiff -litkjpeg8 -lITKSpatialObject
-lITKCommon -litkvnl_inst -litkvnl_algo -litkvnl -litkv3p_netlib
-litkvcl -lITKMetaIO -litksys -lITKDICOMParser -lITKEXPAT -lITKniftiio
-lITKznz -litkzlib -lvtkFiltering -lvtkCommon -lpthread -lm
-lvtkDICOMParser -lvtkNetCDF -lvtkmetaio -lvtksqlite -lvtkpng -lvtktiff
-lvtkzlib -lvtkjpeg -lvtkexpat -lvtksys -lm -ldl

I cannot see the difference, but why it doesn't work?


More information about the petsc-users mailing list