Contents Previous Next Geant4 User's Guide
For Application Developers
Appendix


10.5 Makefiles and Environment Variables




This section describes how the GNUmake infrastructure is implemented in Geant4 and provides a quick reference guide for the user/installer about the most important environment variables defined.

10.5.1 The GNUmake system in Geant4

As described in section 2.1 of the Installation Guide, the GNUmake process in Geant4 is mainly controlled by the following GNUmake script files (*.gmk scripts are placed in $G4INSTALL/config): To build a single library (or a set of sub-libraries) or an executable, you must explicitly change your current directory to the one you're interested in and invoke the "gmake" command from there ("gmake global" for building a compound library). Here is a list of the basic commands or GNUmake "targets" one can invoke to build libraries and/or executables: lib/ bin/ and tmp/ directories

The $G4INSTALL environment variable specifies where the installation of the Geant4 toolkit should take place, therefore kernel libraries will be placed in $G4INSTALL/lib. The $G4WORKDIR environment variable is set by the user and specifies the path to the user working directory; temporary files (object-files and data products of the installation process of Geant4) will be placed in $G4WORKDIR/tmp, according to the system architecture used. Binaries will be placed in $G4WORKDIR/bin, according to the system architecture used. The path to $G4WORKDIR/bin/$G4SYSTEM should be added to $PATH in the user environment.


10.5.2 Environment variables

Here is a list of the most important environment variables defined within the Geant4 GNUmake infrastructure, with a short explanation of their use.
We recommend that those environment variables listed here and marked with (*) NOT be overriden or set (explicitly or by accident). They are already set and used internally in the default setup !

10.5.3 Linking External Libraries with Geant4

The Geant4 GNUmake infrastructure allows to extend the link list of libraries with external (or user defined) packages which may be required for some user's applications to generate the final executable.

10.5.3.1 Adding external libraries which do *not* use Geant4

In the GNUmakefile of your application, before including binmake.gmk, specify the extra library in EXTRALIBS either using the -L...-l... syntax or by specifying the full pathname, e.g.:
  EXTRALIBS := -L<your-path>/lib -l<myExtraLib>
or
  EXTRALIBS := <your-path>/lib/lib<myExtraLib>.a
You may also specify EXTRA_LINK_DEPENDENCIES, which is added to the dependency of the target executable, and you may also specify a rule for making it, e.g.:
  EXTRA_LINK_DEPENDENCIES := <your-path>/lib/lib<myExtraLib>.a

  <your-path>/lib/lib<myExtraLib>.a:
        cd <your-path>/lib; $(MAKE)
Note that you almost certainly need to augment CPPFLAGS for the header files of the external library, e.g.:
  CPPFLAGS+=-I<your-path>/include
See table 10.5.1.

 # --------------------------------------------------------------------
 # GNUmakefile for the application "sim" depending on module "Xplotter"
 # --------------------------------------------------------------------

 name := sim
 G4TARGET := $(name)
 G4EXLIB := true

 CPPFLAGS  += -I$(HOME)/Xplotter/include
 EXTRALIBS += -L$(HOME)/Xplotter/lib -lXplotter
 EXTRA_LINK_DEPENDENCIES := $(HOME)/Xplotter/lib/libXplotter.a

 .PHONY: all

 all: lib bin

 include $(G4INSTALL)/config/binmake.gmk

 $(HOME)/Xplotter/lib/libXplotter.a:
         cd $(HOME)/Xplotter; $(MAKE)
Table 10.5.1
An example of a customised GNUmakefile for an application or example using an external module not bound to Geant4.

10.5.3.2 Adding external libraries which use Geant4

In addition to the above, specify, in EXTRALIBSSOURCEDIRS, a list of directories containing source files in its src/ subdirectory. Thus, your GNUmakefile might contain:
  EXTRALIBS += $(G4WORKDIR)/tmp/$(G4SYSTEM)/<myApp>/lib<myApp>.a \
               -L<your-path>/lib -l<myExtraLib>
  EXTRALIBSSOURCEDIRS += <your-path>/<myApp> <your-path>/<MyExtraModule>
  EXTRA_LINK_DEPENDENCIES := $(G4WORKDIR)/tmp/$(G4SYSTEM)/<myApp>/lib<myApp>.a

  MYSOURCES := $(wildcard <your-path>/<myApp>/src/*cc)
  $(G4WORKDIR)/tmp/$(G4SYSTEM)/<myApp>/lib<myApp>.a: $(MYSOURCES)
        cd <your-path>/<myApp>; $(MAKE)
See Table 10.5.2.

# -----------------------------------------------------------------
# GNUmakefile for the application "phys" depending on module "reco"
# -----------------------------------------------------------------

name := phys
G4TARGET := $(name)
G4EXLIB := true

EXTRALIBS += $(G4WORKDIR)/tmp/$(G4SYSTEM)/$(name)/libphys.a \
             -L$(HOME)/reco/lib -lreco
EXTRALIBSSOURCEDIRS += $(HOME)/phys $(HOME)/reco
EXTRA_LINK_DEPENDENCIES := $(G4WORKDIR)/tmp/$(G4SYSTEM)/$(name)/libphys.a

.PHONY: all
all: lib bin

include $(G4INSTALL)/config/binmake.gmk

MYSOURCES := $(wildcard $(HOME)/phys/src/*cc)
$(G4WORKDIR)/tmp/$(G4SYSTEM)/$(name)/libphys.a: $(MYSOURCES)
	cd $(HOME)/phys; $(MAKE)
Table 10.5.2
An example of a customised GNUmakefile for an application or example using external modules bound to Geant4.


About the authors