因為有CMake自動化配置工具,在Windows平台上安裝Geant4與Linux平台幾乎同樣方便。與使用Linux不同的是,官方更鼓勵使用集成開發環境Visual Studio (VS)來編譯安裝。下面簡單介紹一下。
系統環境: Windows10, Visual Studio 2017 (community), CMake 3.10, Geant4.10.04
1. 系統准備
首先下載安裝Visual Studio 2017 和最新版本CMake,筆者這里使用的是社區(community)版本的VS和3.10版本的 CMake.
(a) 安裝完VS后需要添加C++工具包:選中 Tools > Get Tools and Features
安裝必需的C++編譯工具。
要驗證C++工具包是不是正確安裝了,可以選中 Tools > Visual Studio Command Prompt
,在VS中打開命令終端, 輸入cl
. 如果沒有找到這個命令,那就是沒有成功。
D:\>cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.12.25835 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
usage: cl [ option... ] filename... [ /link linkoption... ]
(b) CMake的安裝過程中需要選擇 Add CMake to the System PATH for all users
.
2. 下載Geant4
到Geant4官網下載最新版本Geant4.10.04.zip, 解壓到指定位置,譬如D盤,這樣我們就有了文件夾D:\geant4_10_04, 我們再創建文件夾D:\geant4_10_04\build用來存放編譯安裝的臨時文件。
3. 編譯
以下操作需要簡單的DOS命令。
打開Tools > Visual Studio Command Prompt
, 進入D盤的build文件夾中運行CMake.
> D:
> cd geant4_10_04/build
> cmake -DCMAKE_INSTALL_PREFIX="D:\geant4_10_04" -DGEANT4_INSTALL_DATA=ON -DGEANT4_USE_OPENGL_WIN32=ON "D:\geant4_10_04"
注意:這里的openGL選項不是GEANT4_USE_OPENGL_X11,而是_WIN32結尾.
如果你看到類似輸出,那就是成功了,特別是dataset和openGL等選定的特性確實開啟了。
-- Configuring download of missing dataset G4NEUTRONXS (1.4)
-- Configuring download of missing dataset G4PII (1.3)
-- Configuring download of missing dataset RealSurface (2.1)
-- Configuring download of missing dataset G4SAIDDATA (1.1)
-- Configuring download of missing dataset G4ABLA (3.1)
-- Configuring download of missing dataset G4ENSDFSTATE (2.2)
-- The following Geant4 features are enabled:
GEANT4_BUILD_CXXSTD: Compiling against C++ Standard '11'
GEANT4_USE_OPENGL_WIN32: Build OpenGL driver with Win32 support
-- Configuring done
-- Generating done
-- Build files have been written to: D:/geant4_10_04/build
運行以下命令編譯Geant4.
> cmake --build . --config Release
如果輸出結果有類似結尾,那么應該沒有問題。
FinalizeBuildStatus:
Deleting file "Win32\Release\ALL_BUILD\ALL_BUILD.tlog\unsuccessfulbuild".
Touching "Win32\Release\ALL_BUILD\ALL_BUILD.tlog\ALL_BUILD.lastbuildstate".
Done Building Project "D:\geant4_10_04\build\ALL_BUILD.vcxproj" (default targets).
Build succeeded.
0 Warning(s)
0 Error(s)
4. 安裝
> cmake --build . --config Release --target install
輸出結果應該類似這樣結尾:
-- Installing: D:/geant4_10_04/include/Geant4/G4VisTrajContext.hh
-- Installing: D:/geant4_10_04/include/Geant4/G4VisTrajContext.icc
FinalizeBuildStatus:
Deleting file "Win32\Release\INSTALL\INSTALL.tlog\unsuccessfulbuild".
Touching "Win32\Release\INSTALL\INSTALL.tlog\INSTALL.lastbuildstate".
Done Building Project "D:\geant4_10_04\build\install.vcxproj" (default targets).
Build succeeded.
0 Warning(s)
0 Error(s)
以上已經安裝完成,但是在實際使用前還要設置環境變量。
5. 設置環境變量
如何添加環境變量請自行百度。
PATH 追加 D:\geant4_10_04\bin
G4ENSDFSTATEDATA D:\geant4_10_04\share\Geant4-10.4.0\data\G4ENSDFSTATE2.2
G4LEDATA D:\geant4_10_04\share\Geant4-10.4.0\data\G4EMLOW7.3
G4LEVELGAMMADATA D:\geant4_10_04\share\Geant4-10.4.0\data\PhotonEvaporation5.2
G4SAIDXSDATA D:\geant4_10_04\share\Geant4-10.4.0\data\G4SAIDDATA1.1
G4NEUTRONHPDATA D:\geant4_10_04\share\Geant4-10.4.0\data\G4NDL4.5
G4NEUTRONXSDATA D:\geant4_10_04\share\Geant4-10.4.0\data\G4NEUTRONXS1.4
G4PIIDATA D:\geant4_10_04\share\Geant4-10.4.0\data\G4PII1.3
G4RADIOACTIVEDATA D:\geant4_10_04\share\Geant4-10.4.0\data\RadioactiveDecay5.2
G4REALSURFACEDATA D:\geant4_10_04\share\Geant4-10.4.0\data\RealSurface2.1
G4ABLADATA D:\geant4_10_04\share\Geant4-10.4.0\data\G4ABLA3.1
示例example/basic/B1的測試
> cd example/basic/B1
> mkdir build
> cd build
> cmake -DGeant4_DIR="D:\geant4_10_04\lib\Geant4-10.4.0" ..
> cmake --build . --config Release
第一個CMake的輸出結果類似這樣
-- Building for: Visual Studio 15 2017
-- The C compiler identification is MSVC 19.12.25835.0
-- The CXX compiler identification is MSVC 19.12.25835.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx86/x86/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx86/x86/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx86/x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx86/x86/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/examples/basic/B1/build
第二個CMake輸出結果應會告訴你編譯有沒有成功。
Build succeeded.
"D:\examples\basic\B1\build\ALL_BUILD.vcxproj" (default target) (1) ->
"D:\examples\basic\B1\build\exampleB1.vcxproj" (default target) (3) ->
(ClCompile target) ->
d:\geant4_10_04\include\geant4\g4mergemode.hh(75): warning C4715: 'G4Accumulables::GetMergeFunction<double>': not all
control paths return a value [D:\examples\basic\B1\build\exampleB1.vcxproj]
1 Warning(s)
0 Error(s)
如果成功的話,我們就會有一個 Release
文件夾,里面有可執行程序exampleB1.我們進入該文件夾雙擊就可以運行,不過這里使用了可視化,所以請復制build文件夾下的vis.mac
和init_vis.mac
到Release.
雙擊運行后,如果你看到了可視化的探測器,那么恭喜你,安裝成功了。
最后,筆者想要說明一下,雖然Geant4官方提供了Windows平台的解決方案,但是我們還是更推薦Linux系統下使用Geant4.
本文原載於微信訂閱號 “Geant4不完全學習指南”