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-*

Steps

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

      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
      {
      public:
          TG4MoleculeShoot() : G4MoleculeShoot(){;}
          virtual ~TG4MoleculeShoot(){;}
          void Shoot(G4MoleculeGun*){}
      
      protected:
          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 \
      -DGEANT4_USE_SYSTEM_EXPAT=ON \
      -DGEANT4_USE_SYSTEM_ZLIB=ON \
      -DGEANT4_INSTALL_DATA=ON \
      -DGEANT4_USE_OPENGL_X11=ON
      
  • Build.
    make
    

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

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

    startxwin
    

    In another terminal:

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

Acknowledgement

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

    Hello,

    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!
    Charlie

    Reply
    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.

      Reply
      1. Charlie

        Hello,

        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.

        Regards,
        Charlie

Leave a Reply to Charlie Cancel reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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