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:
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
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
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
  Auto build dll exports
  ptl-shared.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release\
  Auto build dll exports
  G4global.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release\bi
  Auto build dll exports
  G4intercoms.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release
  Auto build dll exports
  G4graphics_reps.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Rel
  Auto build dll exports
  G4zlib.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release\bin\
  Auto build dll exports
  G4materials.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release
  Auto build dll exports
  G4geometry.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release\
  Auto build dll exports
  G4particles.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release
  Auto build dll exports
  G4track.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release\bin
  Auto build dll exports
  G4digits_hits.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Relea
  Auto build dll exports
  G4expat.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release\bin
  Auto build dll exports
  G4analysis.vcxproj -> D:\geant4\geant4-v11.2.0-build\BuildProducts\Release\
  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/BuildProducts/Release/lib/G4processe
  s.lib ??? D:/geant4/geant4-v11.2.0-build/BuildProducts/Release/lib/G4proces
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
  Building Custom Rule D:/geant4/geant4-v11.2.0/source/CMakeLists.txt

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:


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:
c(46,15): error C2039: "isNull": ?? "G4String" ??? [D:\geant4\geant4-v10.6.3-
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

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:
c(684,112): error C2445: ?????? result ?????: ??"_Ty"?"const char [10]"??????
???? [D:\geant4\geant4-v11.2.0-build\source\G4global.vcxproj]
c(684,112): message : ???"_Ty" [D:\geant4\geant4-v11.2.0-build\source\G4globa
c(684,112): message : ?    "const char *" [D:\geant4\geant4-v11.2.0-build\sou

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.

Sorry for my late reply. Executing cl.exe directly yields the following output (same as executing cl):

d:\geant4\geant4-build> cl.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30133 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

According to your prompt, I checked D:\geant4\geant4-build\CMakeCache.txt and found no variable CMAKE_CXX_COMPILER in it. But I found the following statement in the file CMakeCXXCompiler.cmake under the directory D:\geant4\geant4-build\CMakeFiles\3.27.9:

set(CMAKE_CXX_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe")

Meanwhile, executing cmake --help-variable CMAKE_CXX_COMPILER on Developer Command Prompt tells me that I don’t have this variable. Of the variables listed using --help-variable-list, there are five beginning with CMAKE_CXX : CMAKE_CXX_COMPILE_FEATURES, CMAKE_CXX_EXTENSIONS, CMAKE_CXX_SCAN_FOR_MODULES, CMAKE_CXX_STANDARD, CMAKE_CXX_STANDARD_REQUIRED. The output of viewing variable CMAKE_CXX_COMPILE_FEATURES are as follows:

d:\geant4\geant4-build>cmake --help-variable CMAKE_CXX_COMPILE_FEATURES

.. versionadded:: 3.1

List of features known to the C++ compiler

These features are known to be available for use with the C++ compiler. This
list is a subset of the features listed in the
``CMAKE_CXX_KNOWN_FEATURES`` global property.

See the ``cmake-compile-features(7)`` manual for information on
compile features and a list of supported compilers.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.