Build Geant4 (including OpenGL visualization) using Cygwin on Windows

Build Geant4 (including OpenGL visualization) using Cygwin on Windows

[Updated on Nov 15, 2019]

There has been a lack of official documentation on how to build Geant4 using Cygwin on Windows. This post is intended to fill the gap. All we need to do is to install several Cygwin packages, modify a couple of cmake scripts, and change a few lines of Geant4 source code.

Test conditions

  • Geant4 10.5.1
  • Windows 10 (64-bit)
  • Cygwin 64 version 3.0.7
  • gcc and g++ version 7.4.0
  • cmake version 3.14.5

The following Cygwin packages are required to build C++ project under CMake.

  • gcc-g++
  • cmake
  • make

The following Cygwin packages are required to build Geant4 core engine.

  • expat, libexpat-devel
  • zlib, zlib-devel

The following Cygwin packages are required to build Geant4 OpenGL visualization module.

  • libX11-devel
  • libXmu-devel
  • libGL-devel
  • xinit
  • xorg-server
  • xorg-x11-fonts-*


  • Modify cmake scripts.
    • In both cmake\Modules\G4BuildSettings.cmake and cmake\Modules\G4ConfigureCMakeHelpers.cmake, change CMAKE_CXX_EXTENSIONS from OFF to ON, i.e.

      The above steps are crucial in that they let the compiler flag -std=gnu++11 be automatically added in place of the initial flag -std=c++11. On Cygwin -std=c++11 will make the Posix function posix_memalign() inaccessible, which will cause Geant4 compile errors.

  • Modify source code.
    • In source\processes\electromagnetic\dna\utils\include\G4MoleculeGun.hh, add declaration of explicit specialization immediately after class definition:
      template<typename TYPE>
      class TG4MoleculeShoot : public G4MoleculeShoot
          TG4MoleculeShoot() : G4MoleculeShoot(){;}
          virtual ~TG4MoleculeShoot(){;}
          void Shoot(G4MoleculeGun*){}
          void ShootAtRandomPosition(G4MoleculeGun*){}
          void ShootAtFixedPosition(G4MoleculeGun*){}
      // Above is class definition in Geant4
      // We need to add three lines of code here
      // to declare explicit specialization
      template<> void TG4MoleculeShoot<G4Track>::ShootAtRandomPosition(G4MoleculeGun* gun);
      template<> void TG4MoleculeShoot<G4Track>::ShootAtFixedPosition(G4MoleculeGun* gun);
      template<> void TG4MoleculeShoot<G4Track>::Shoot(G4MoleculeGun* gun);

      Otherwise the compiler would complain about multiple definition.

    • In source\global\management\src\, comment out syscall.h include. Apparently Cygwin does not offer the OS specific header file syscall.h, and thus do not support multithreading in Geant4 that relies on syscall.h.
      // #include  // comment out this line
  • Create out-of-source build script. Due to lack of syscall.h in Cygwin, only single-threaded Geant4 can be built.
    • Release build
      cmake ../geant4_src -DCMAKE_C_COMPILER=/usr/bin/gcc.exe \
      -DCMAKE_CXX_COMPILER=/usr/bin/g++.exe \
      -DCMAKE_INSTALL_PREFIX=/opt/geant4/release \
      -DCMAKE_BUILD_TYPE=Release \
  • Build.

    For faster compile, use make -j6 which uses 6 parallel processes.

  • Install.
    make install
  • Visualization
    To visualize B1 example, in one Cygwin terminal:


    In another terminal:

    export DISPLAY=:0.0
  • Have fun with Geant4 !!! … and remember: If you love something, set it free.


Thanks to Charlie for making me aware of the issues in newer Geant4.

3 thoughts on “Build Geant4 (including OpenGL visualization) using Cygwin on Windows

  1. Charlie


    I have been trying to do a similar task, build Geant4 with Cygwin and I keep getting held up by posix_memalign(). What is the right way to change the compiler flag as you said to -std=gnu++11 in Geant4 10.5? There are no longer the files Geant4LibraryBuildOptions.cmake and Geant4BuildProjectConfig.cmake from what I can see? There is a file Geant4Config.cmake which has compiler flag but I am not sure how to make a change there and it is created after running cmake.

    Thanks for your help!

    1. kingcrimsontenseconds Post author

      Greetings Charlie, thanks for your question. I will look into this issue. I haven’t used Geant4 for quite a while, so I will need to familiarize myself with the new cmake code from the recent release. You may send a quick message to my permanent email so that I can let you know of the solution once I figure it out.

      1. Charlie


        I think I found the answer. There is now a cmake file called “G4BuildSettings.cmake” within cmake\Modules\ that seems to be the replacement for “Geant4LibraryBuildOptions.cmake”.

        I added to this file the enum “gnu++11” as you said and set the DGEANT4_BUILD_CXXSTD to “gnu++11” as well and now it seems to build fine within cygwin following your other instructions.

        Thanks for the tips! They were very helpful! I had a lot of trouble with posix_memalign() and this appears to have fixed it.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s