Fixing Geant4 build on MinGW

I have just tried to build Geant4 on win10 with MinGW (MSYS2) and GCC 12.2, and encountered compilation failures. Almost all of these errors come from the exposure of code that should not be compiled on the windows platform (e.g. protected by #ifdef WIN32 or #ifndef WIN32).

I realized that WIN32 macro definition is missing, however it is used by G4 to distinguish the code routines under the windows platform.

I tried to fix the issue by simply changing some preprocessor macros, and it passed the compilation successfully (with -fpermissive).

The following are the configurations I used and the modifications:

Geant4 version: 11.0.2
Compiler: GCC 12.2.0 (Rev1, Built by MSYS2 project)
Platform: Windows 10 (19044.1949) with MSYS2-20220603 (MinGW64)
CMake configurations:

cmake ../geant4-v11.0.2/ -DCMAKE_CXX_STANDARD=20 -DCMAKE_C_FLAGS="-fpermissive -D_USE_MATH_DEFINES" -DCMAKE_CXX_FLAGS="-fpermissive -D_USE_MATH_DEFINES" -DBUILD_SHARED_LIBS=OFF -DBUILD_STATIC_LIBS=ON -DGEANT4_USE_GDML=ON -DGEANT4_USE_QT=ON -DGEANT4_USE_SYSTEM_ZLIB=ON

Modifications:

In source/externals/g4tools/include/tools/wroot/date

25 - #ifdef _MSC_VER
25 + #if defined(_MSC_VER) || defined(__MINGW32__)

In source/global/management/include/G4Timer.hh:

75 - #ifndef WIN32
75 + #if !(defined(WIN32) || defined(__MINGW32__))

In source/global/management/include/G4SliceTimer.hh:

42 - #ifndef WIN32
42 + #if !(defined(WIN32) || defined(__MINGW32__))

In source/global/management/src/G4Threading.cc:

37 - #if defined(WIN32)
37 + #if defined(WIN32) || defined(__MINGW32__)

In source/global/HEPRandom/src/G4UniformRandPool.cc:

49 - #if defined(WIN32)
49 + #if defined(WIN32) || defined(__MINGW32__)

In source/externals/clhep/include/CLHEP/Utility/thread_local.h:

10 - #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 7) || __clang__ || WIN32
10 + #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 7) || __clang__ || defined(WIN32) || defined(__MINGW32__)

In source/processes/hadronic/models/lend/src/MCGIDI_map.cc:

15 - #ifdef WIN32
15 + #if defined(WIN32) || defined(__MINGW32__)

In source/processes/hadronic/models/lend/src/xDataTOM.cc:

14 - #ifdef WIN32
14 + #if defined(WIN32) || defined(__MINGW32__)

In source/processes/hadronic/models/lend/src/xDataTOM_importXML.cc:

14 - #ifdef WIN32
14 + #if defined(WIN32) || defined(__MINGW32__)

In source/interfaces/basic/include/G4UItcsh.hh:

32 - #ifndef WIN32
32 + #if !(defined(WIN32) || defined(__MINGW32__))

In source/interfaces/basic/src/G4UIExecutive.cc:

137 - #ifndef WIN32
137 + #if !(defined(WIN32) || defined(__MINGW32__))

I hope this will help.

Sincerely,
SH

This is very useful, thanks! Whilst we don’t test (and thus support) MinGW builds, we’d welcome these fixes submitted as a pull request to our GitHub mirror here: GitHub - Geant4/geant4: Geant4 toolkit for the simulation of the passage of particles through matter - NIM A 506 (2003) 250-303

Though as noted we don’t test MinGW, we’ll of course integrate the changes assuming no breaks in other platforms.

Apologize for the late reply! I have submitted a pull request on GitHub. Please take a look at Fix geant4 build on mingw by zhaoShiEnthalpy · Pull Request #51 · Geant4/geant4 (github.com)

1 Like

That’s great, thank you! It might take me a day or two to try this in our CI, but will let you know if there are issues as soon possible.