How to link Geant4 with a code

Dear All,

I works on detector simulation, for that I have to link Geant4 (already installed in my laptop) with my code/project. For that purpose, I need to export path to Geant4. Can anyone please suggest me - how I can export path to Geant4 (what exactly will be the command for that)?

Many thanks in advance.

Regards,
Manisha

Sounds like you want this section of the Installation Guide:
https://geant4-userdoc.web.cern.ch/UsersGuides/InstallationGuide/html/buildtools.html

If you’re using plain old Make, then you would source /share/geant4make/geant4make.sh (or .csh) to define lots and lots of envrionment variables, and probably make use of $G4INSTALL/config/binmake.gmk in the Makefile for your application executable. Note that plain old Make is no longer fully supported by the Geant4 developers, so you’re somewhat on your own.

Hi Michael,
Thank you for quick reply and suggestions.
I have installed Geant4 using following commands:

$ cd /path/to/geant4-v11.0.0-build
$ cmake -DCMAKE_INSTALL_PREFIX=/path/to/geant4-v11.0.0-install /path/to/geant4-v11.0.0
$ make -jN
$ make install

My code is in a directory named say “XY” ; so I need to export path to Geant4 - (to link Geant4 to XY) : this is what exactly I have to do?

Regards,
Manisha

Hi Michael,

To do the above mentioned: Should I do: Install my “XY” directory (having my code) in Geant4-Install directory? - so that XY directory can be linked to Geant4??
Am I right?

Hello,
to export path to Geant4 you can use command:
source /path/to/geant4-v11.0.0-install/bin/geant4.sh
before starting your application.

Hello Marek,

Thank you very much for your reply.
But still I have one doubt that I should run this command “source /path/to/geant4-v11.0.0-install/bin/geant4.sh” in my “XY” directory where I will install my code for simulations ??
Am I right??

You can run the source command anywhere you like. It defines a bunch of environment variables in your shell process. You can change directories, and those envvars don’t change or disappear.

You should build and install your application separately from your Geant4 installation. Don’t mix them up. You may want to read some general documents about building with CMake in order to understand clearly how to organize your code.

OK, thank you very much Michael.

if you add the source command to .bashrc, save it and restart terminal, it won’t be required afterwards.

Ok, thank you Deepak

Hi All, I recently installed Geant4 in Fedora 37 Linux system and tried to source it to my home directory using the source command that Manisha was recommended. The system complained about something missing, so I replaced the filename geant4.sh with geant4.csh (there were both these files in the geant3-v11.1.1-install/bin folder. Now it is giving me a syntax error of a missing right bracket. Please help, thanks

Have you read the Installation Guide? Do you know which shell you are using? You should choose the setup script based on your shell.

In general, do not resurrect old threads with new and unrelated questions. It’s better to start a new thread, referring back to the old if needed, for clarity. If @mkelsey’s info doesn’t solve the issue for you, please start a new thread.