Tag Archives: Cygwin

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\G4Threading.cc, 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.