Building Geant4 with Cygwin 64 on Windows 10 without Visual Studio

[Last updated on May 11, 2017]
*** If the method below no longer works on newer version of G4, please kindly let me know and I will update this article. Thanks.

The official documentation on Geant4 + Cygwin + MVS is not quite clear to me, so here we use Cygwin alone to compile the code. One has to modify a few cmake scripts and source files (at least for Geant4 10.02.p02) in order to build Geant4 without errors.

Prerequisite

  • Geant4 10.02.p02
  • Windows 10 (64-bit)
  • Cygwin 64 with gcc and g++ version 5.4.0
  • cmake version 3.6.2

Steps

  • Modify cmake scripts.
    • In both cmake\Modules\Geant4LibraryBuildOptions.cmake and cmake\Modules\Geant4BuildProjectConfig.cmake, enable C++ extension, i.e.
      set(CMAKE_CXX_EXTENSIONS ON)
      
    • In cmake\Modules\Geant4LibraryBuildOptions.cmake, add at least one compiler feature that requires gnu++11. For example, cxx_constexpr, i.e.
      set(GEANT4_TARGET_COMPILE_FEATURES
          some initial other compiler features
          cxx_constexpr
          some initial comments
          )
      

      In addition, add gnu++11 to the option list, i.e.

      enum_option(GEANT4_BUILD_CXXSTD
            DOC "C++ Standard to compile against"
            VALUES 11 14 c++11 c++14 gnu++11
            CASE_INSENSITIVE
            )
      

      The above steps are crucially important, because they ensure that the compiler flag -std=gnu++11 be automatically added by cmake rather than -std=c++11. -std=c++11 somehow does not satisfy __POSIX_VISIBLE >= 200112 macro in stdlib.h and consequently function posix_memalign() would not be accessible.

  • 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
      {
      ...
      };
      // above is class definition
      // add declaration of explicit specialization below
      
      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 <sys/syscall.h>
      
  • Create out-of-source build using cmake.  Due to lack of syscall.h in Cygwin, only single-threaded Geant4 can be built.
    cmake ../geant4.10.02.p02 -DCMAKE_C_COMPILER=/usr/bin/gcc.exe -DCMAKE_CXX_COMPILER=/usr/bin/g++.exe -DCMAKE_INSTALL_PREFIX=/opt/geant4/rel_with_debug_info -DGEANT4_BUILD_CXXSTD=gnu++11
    
  • Build and install. make && make install.
  • Have fun with Geant4 !!! … and remember: if you love something,
    set it free.
Advertisements

Leave a 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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s