在Linux上构建和安装Geant4(中文)

Albert Cheung

从源代码构建和安装

1
2
3
注意:在下文中请注意将其中所有的`/home/username/Downloads`修改为真实路径。这是你配置文件存放的位置。
本文以版本v11.2.2为例,请注意将下文中包含的版本号更换为下载时版本号。
如有任何疑问,请参考帮助文档https://geant4.web.cern.ch/docs/
  1. 下载源代码压缩包

进入Geant4官网 https://geant4.web.cern.ch/, 点击上方下载链接,选择”Source code”中的”Download tar.gz”并下载。假设已经下载至子目录/home/username/Downloads中。

在Fedora或者Ubuntu中,还可以通过终端下载

1
$ wget -P /home/username/Downloads https://gitlab.cern.ch/geant4/geant4/-/archive/v11.2.2/geant4-v11.2.2.tar.gz
  1. 在终端中进行解压
1
$ tar -xzvf /home/username/Downloads/geant4-v11.2.2.tar.gz -C /home/username/Downloads

将其中的/home/username/Downloads修改为真实路径。

现在,Geant4源包已经在子目录中

1
/home/username/Downloads/geant4-v11.2.2

这是解压后的文件路径。

  1. 新建构建目录

构建目录用于配置和运行构建并存储构建产品。此目录不应与源目录相同或位于源目录内。

1
2
$ cd /home/username/Downloads
$ mkdir geant4-v11.2.2-build

使用ls确认目录结构,输出应当含有以下内容

1
2
$ ls
geant4-v11.2.2 geant4-v11.2.2-build

切换到构建目录中并进行配置

1
2
$ cd /home/username/Downloads/geant4-v11.2.2-build
$ cmake -DCMAKE_INSTALL_PREFIX=/home/username/Downloads/geant4-v11.2.2-install /home/username/Downloads/geant4-v11.2.2
  1. 进行安装设置(可选)

可以在安装时设置参数以添加其它的安装可选项。例如,在按上述方式运行 CMake 后,您可能希望激活Geant4数据集的安装

1
2
$ cd /home/username/Downloads/geant4-v11.2.0-build
$ cmake -DGEANT4_INSTALL_DATA=ON .

请不要忽略末尾的 .。更多可选项,例如启动图形界面的安装等,请查看

1
https://geant4-userdoc.web.cern.ch/UsersGuides/InstallationGuide/html/installguide.html#geant4buildoptions

只需要将

1
$ cmake -DGEANT4_INSTALL_DATA=ON .

切换为对应代码输入即可。安装完成后,终端会输出取决于平台的不同的内容,但最后三行应该大致是

1
2
3
-- Configuring done
-- Generating done
-- Build files have been written to: /home/username/Downloads/geant4-v11.2.0-build

这表明已经配置完成。如果你没有安装数据集,可能会提出警告,但不影响整体的安装。

  1. 运行构建

首先,请查看您设备的处理器核心数量

1
$ nproc

输出的数字N代表您设备的处理器核心数。然后运行构建

1
2
$ cd /home/username/Downloads/geant4-v11.2.0-build
$ make -jn

如果没有特别的要求,可以将n替换为N;如果希望在编译时完成其它工作,可以适当降低一些n
的数值;例如,如果您的设备拥有32核处理器,可以设置n为32、30甚至28,取决于您的要求。

配置时有时可能会报错,这时请运行

1
$ make -jN VERBOSE=1

它会输出更多来帮助解决问题或仅为了获取信息的内容。

  1. 安装Geant4

一旦构建完毕,你可以将Geant4下载到您之前在CMAKE_INSTALL_PREFIX中指定的位置

1
2
$ cd /home/username/Downloads/geant4-v11.2.2-install
$ make install

可以进一步检查安装版本以确定是否安装完毕

1
/home/username/Downloads/geant4-v11.2.2-install/bin/geant4-config --version

如果安装完成,应当会输出版本号。

  1. 卸载

如果需要卸载,请运行

1
$ make uninstall
  • Title: 在Linux上构建和安装Geant4(中文)
  • Author: Albert Cheung
  • Created at : 2024-08-13 11:51:05
  • Updated at : 2024-08-13 12:41:05
  • Link: https://www.albertc9.github.io/2024/08/13/Building-and-Installing-Geant4-on-Linux-ch/
  • License: This work is licensed under CC BY-NC-SA 4.0.
On this page
在Linux上构建和安装Geant4(中文)