Error C2445 and LNK2005 while building Geant4 on Win10

_Geant4 Version:_10.6.3
_Operating System:_win10
_Compiler/Version:_VS 2019
_CMake Version:_3.27.9

When I built geant4 with command cmake --build . --config Release, a large number of files reported error C2445 and LNK2005, and the geant4-install folder was always empty.

The variables set in Cmake are as follows:


When executing command cmake --build . --config Release and cmake --build . --config Release --target install in the geant4-build folder, a lot of the same errors occured but the compile and install process was not interrupted. The error was like:

G4HadronInelasticProcess.cc
D:\geant4\geant4-v10.6.3\source\processes\hadronic\cross_sections\include\G4FastPathHadronicCrossSection.hh(299,85): er
ror C2445: ?????? result ?????: ??"const G4String"?"const char [10]"?????????? [D:\geant4\geant4-build\source\processes
\G4processes.vcxproj]
D:\geant4\geant4-v10.6.3\source\processes\hadronic\cross_sections\include\G4FastPathHadronicCrossSection.hh(299,85): me
ssage : ???"const G4String" [D:\geant4\geant4-build\source\processes\G4processes.vcxproj]
D:\geant4\geant4-v10.6.3\source\processes\hadronic\cross_sections\include\G4FastPathHadronicCrossSection.hh(299,85): me
ssage : ?    "const char *" [D:\geant4\geant4-build\source\processes\G4processes.vcxproj]
D:\geant4\geant4-v10.6.3\source\processes\hadronic\cross_sections\include\G4FastPathHadronicCrossSection.hh(300,77): er
ror C2445: ?????? result ?????: ??"const G4String"?"const char [10]"?????????? [D:\geant4\geant4-build\source\processes
\G4processes.vcxproj]
D:\geant4\geant4-v10.6.3\source\processes\hadronic\cross_sections\include\G4FastPathHadronicCrossSection.hh(300,77): me
ssage : ???"const G4String" [D:\geant4\geant4-build\source\processes\G4processes.vcxproj]
D:\geant4\geant4-v10.6.3\source\processes\hadronic\cross_sections\include\G4FastPathHadronicCrossSection.hh(300,77): me
ssage : ?    "const char *" [D:\geant4\geant4-build\source\processes\G4processes.vcxproj]

Then after the installing the geant4-install folder is still empty.

These errors appear to be due to ambiguity in the writing of the .cc file, but I have used the same method to install Geant4 on other machines before without such errors. And the number of such errors is too large for me to correct one by one.

After I manually modified several .cc files, a new error appeared. The full output is as follows, and finally I use Ctrl+C to terminate the process.

d:\geant4\geant4-v11.2.0-build>cmake --build . --config Release
Microsoft (R) Build Engine version 16.11.0+0538acc04 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  Auto build dll exports
  G4clhep.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release\bin
  \G4clhep.dll
  Auto build dll exports
  ptl-shared.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release\
  bin\G4ptl.dll
  Auto build dll exports
  G4global.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release\bi
  n\G4global.dll
  Auto build dll exports
  G4intercoms.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release
  \bin\G4intercoms.dll
  Auto build dll exports
  G4graphics_reps.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Rel
  ease\bin\G4graphics_reps.dll
  Auto build dll exports
  G4zlib.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release\bin\
  G4zlib.dll
  Auto build dll exports
  G4materials.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release
  \bin\G4materials.dll
  Auto build dll exports
  G4geometry.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release\
  bin\G4geometry.dll
  Auto build dll exports
  G4particles.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release
  \bin\G4particles.dll
  Auto build dll exports
  G4track.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release\bin
  \G4track.dll
  Auto build dll exports
  G4digits_hits.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Relea
  se\bin\G4digits_hits.dll
  Auto build dll exports
  G4expat.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release\bin
  \G4expat.dll
  Auto build dll exports
  G4analysis.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release\
  bin\G4analysis.dll
  G4LatticeManager.cc
  G4LatticeReader.cc
  G4PhononDownconversion.cc
  G4PhononPolarization.cc
  G4PhononReflection.cc
  G4PhononScattering.cc
  G4PhononTrackMap.cc
  G4VPhononProcess.cc
  ??????...
  G4ChannelingTrackData.cc
  G4ChannelingOptrMultiParticleChangeCrossSection.cc
  G4ChannelingOptrChangeCrossSection.cc
  G4ChannelingMaterialData.cc
  G4Channeling.cc
  G4ChannelingECHARM.cc
  ??????...
  G4FastSimHitMaker.cc
  G4FastSimulationHelper.cc
  G4FastSimulationManager.cc
  G4FastSimulationManagerProcess.cc
  G4FastSimulationMessenger.cc
  G4FastStep.cc
  G4FastTrack.cc
  G4GlobalFastSimulationManager.cc
  G4VFastSimulationModel.cc
  ??????...
  G4EnergySplitter.cc
  G4ParallelWorldProcess.cc
  G4ParallelWorldProcessStore.cc
  G4ParallelWorldScoringProcess.cc
  G4ScoreSplittingProcess.cc
  ??????...
  G4CoupledTransportation.cc
  G4NeutronKiller.cc
  G4NeutronKillerMessenger.cc
  G4StepLimiter.cc
  G4Transportation.cc
  G4TransportationParameters.cc
  G4TransportationLogger.cc
  G4VTrackTerminator.cc
  ??????...
  Auto build dll exports
G4MoleculeGunMessenger.obj : error LNK2005: "public: virtual void __cdecl TG4
MoleculeShoot<class G4Track>::Shoot(class G4MoleculeGun *)" (?Shoot@?$TG4Mole
culeShoot@VG4Track@@@@UEAAXPEAVG4MoleculeGun@@@Z) ??? G4MoleculeGun.obj ??? [
D:\geant4\geant4-v11.2.0-build\source\G4processes.vcxproj]
    ????? D:/geant4/geant4-v11.2.0-build/BuildProducts/Release/lib/G4processe
  s.lib ??? D:/geant4/geant4-v11.2.0-build/BuildProducts/Release/lib/G4proces
  ses.exp
D:\geant4\geant4-v11.2.0-build\BuildProducts\Release\bin\G4processes.dll : fa
tal error LNK1169: ?????????????? [D:\geant4\geant4-v11.2.0-build\source\G4pr
ocesses.vcxproj]
  Building Custom Rule D:/geant4/geant4-v11.2.0/source/CMakeLists.txt
  G4InteractorMessenger.cc
  G4UIArrayString.cc
  G4UIcsh.cc
????????...
^C
d:\geant4\geant4-v11.2.0-build>

What should I do? Any suggestions are appriciated.

v10.6.3 isn’t supported on the latest Windows systems I’m afraid, though the error looks like it’s related to a workaround that we had to introduce in later versions:

and

You could try patching those into 10.6.3 and see if that resolves things, but there might well be others.

I’m a bit confused by the second error - this is now using Geant4 11.2, not 10.6.3? Which files did you modify and how?

1 Like

Thank you @bmorgan ,

I have patched the two files into 10.6.3 and a new error occured after executing command cmake --build . --config Release as follows:

  G4FilecoutDestination.cc
D:\geant4\geant4-v10.6.3\source\global\management\src\G4FilecoutDestination.c
c(46,15): error C2039: "isNull": ?? "G4String" ??? [D:\geant4\geant4-v10.6.3-
build\source\global\G4global.vcxproj]
D:\geant4\geant4-v10.6.3\source\global\management\include\G4String.hh(61): me
ssage : ??"G4String"??? [D:\geant4\geant4-v10.6.3-build\source\global\G4globa
l.vcxproj]

I’m sorry about the second error, I downloaded 10.6.3 and 11.2.0 at the same time, here I mixed up the build folder of the two versions.

I deleted all the files and started again, downloaded 11.2.0 zip format source code for compilation, error C2445 still appeared. I replaced the G4String.hh and G4String.icc files in the geant4\geant4-v11.2.0\source\global\management\include directory, and after recompiling, the same error is still reported, like this:

  G4PhysicsModelCatalog.cc
D:\geant4\geant4-v11.2.0\source\global\management\src\G4PhysicsModelCatalog.c
c(684,112): error C2445: ?????? result ?????: ??"_Ty"?"const char [10]"??????
???? [D:\geant4\geant4-v11.2.0-build\source\G4global.vcxproj]
          with
          [
              _Ty=G4String
          ]
D:\geant4\geant4-v11.2.0\source\global\management\src\G4PhysicsModelCatalog.c
c(684,112): message : ???"_Ty" [D:\geant4\geant4-v11.2.0-build\source\G4globa
l.vcxproj]
          with
          [
              _Ty=G4String
          ]
D:\geant4\geant4-v11.2.0\source\global\management\src\G4PhysicsModelCatalog.c
c(684,112): message : ?    "const char *" [D:\geant4\geant4-v11.2.0-build\sou
rce\G4global.vcxproj]

Now my solution is to look at the directory where the error files are located and modify them one by one :joy:

Apologies, I should have been clearer here - I meant to only add the declarations and definitions for the:

  inline reference operator[](int);

  /// @overload  
  inline const_reference operator[](int) const;

operators rather than add the replacing the entire files for G4String.hh/.icc.

Geant4 11.2 should be building without error, so all I can suggest here is to delete both the source and build directories and start from scratch (just in case any of your modifications/builds are sticking around). Otherwise, maybe try updating Visual Studio to 2022 (though 2019 should be working).

Thank you @bmorgan ,

I wonder if this error is related to my attempts to install the linux version of Geant4 using virtual machine on the same computer at the same time. Now that I have successfully installed the linux version, I don’t have to solve this problem, but thank you very much for your time anyway.

After successfully installing the linux version of Geant4, I saw your advice and tried it to see if it worked. I deleted both the source and build directories and tried to install Geant4 11.2 from scratch, but the file that previously reported error C2445 will still report the same error after executing cmake --build . --config Release :

The just-released version 11.2.1 does have some patches for Windows support, so you could try that (though none touched G4String…)

For our reference, if you try executing the compiler directly

# plain 'cl' might work as well
d:\geant4\geant4-v11.2.0-build> cl.exe

what does it print? Double check which cl.exe is being used by CMake - this will be the program listed in the CMakeCache.txt file in the build directory under the CMAKE_CXX_COMPILER variable.