Search code examples
c++linuxmakefileinclude-pathpetsc

Using PETSc on makefile within my user-defined makefile


I am new in PETSc. I have a big c++ code and I want to add PETSc to some of the files that I already have, so I have to change my makefile in a way that it can compile PETSc as well.

Is it possible to have two different makefiles and then call PETSc makefile within my own makefile? if so, how can I do this?

Does anyone have any experience in linking PETSc to their own code?

By the way, I am using Linux as my operating system.


Solution

  • edit: Though this is an old post, I am sure there are still people struggling with this.

    Furthermore, things apparently changed slightly for petsc 3.6.x (and slepc 3.6.x).

    I currently use the following lines in my ubuntu 14.04 LTS makefile for F90 files (using both petsc 3.6.1 and slepc 3.6.0):

    # PETSC and SLEPC directories
    PETSC_DIR = /opt/petsc-3.6.1
    SLEPC_DIR = /opt/slepc-3.6.0
    include  $(PETSC_DIR)/lib/petsc/conf/variables
    include  $(SLEPC_DIR)/lib/slepc/conf/slepc_variables
    

    Using this I can construct the

    # Compiler command
    COMPILE = $(COMP_DIR) $(COMP_FLAGS) $(PETSC_FC_INCLUDES) $(SLEPC_INCLUDE)
    

    where COMP_DIR has to be set manually (e.g. COMP_DIR = /usr/bin/mpif90 or COMP_DIR = /usr/bin/gfortran) and COMP_FLAGS are additional flags (e.g. '-g O0'), as well as the

    # Link command
    LINK = $(LINK_DIR) &(LINK_FLAGS)
    

    where again LINK_DIR has to be set manually (e.g. /usr/bin/g++) and LINK_FLAGS contains additional flags (e.g. -fPIC).

    These can then be used to create rules to compile the F90 files (I am sure C is pretty similar):

    %.o : %.f90
    $(COMPILE) -c $<
    

    and the main program:

    main:   $(ObjectFiles) main.o
    $(LINK) -o $@ $(ObjectFiles) main.o $(LINK_LIB) $(PETSC_LIB) $(SLEPC_LIB)
    

    where ObjectFiles contains a list of all the files in the project and LINK_LIB corresponds to other links (e.g. -lgfortran).

    This works fine for me, yet suggestions for improvements are always welcome.

    original post: Instead of the hack described by Divakar, you can easily find out the link flags and the include directories for compilation by running

    make getlinklibs
    make getincludedirs
    

    in the main petsc directory, as described here...