对于 fpm,刚开始应如何操作

对于 fpm,刚开始应如何操作

本教程介绍了 Fortran 包管理器 (fpm) 命令行的基本用法。它涵盖了如何生成新项目以及将项目编译为可执行文件和运行所得程序的可能性。

要使用 fpm 启动一个新项目,请使用fpm new命令

❯ fpm new first_steps

默认情况下,fpm 会使用 fpm 标准布局在 git 存储库中创建一个带有虚拟项目的存储库

❯ cd first_steps
❯ tree .
.
├── README.md
├── app
│   └── main.f90
├── fpm.toml
├── src
│   └── first_steps.f90
└── test
    └── check.f90

3 directories, 5 files

这是我们开始新项目所需的一切。首先,我们检查软件包清单 fpm.toml,其中填充了我们的存根条目

fpm.toml
name = "first_steps"
version = "0.1.0"
license = "license"
author = "Jane Doe"
maintainer = "jane.doe@example.com"
copyright = "Copyright 2021, Jane Doe"
[build]
auto-executables = true
auto-tests = true
auto-examples = true
[install]
library = false

软件包清单包含新项目所需的所有元数据。接下来,我们签出 fpm 为我们生成的 main 可执行文件 app/main.f90

app/main.f90
program main
  use first_steps, only: say_hello
  implicit none

  call say_hello()
end program main

该程序已使用我们库中的一个模块,我们可以在中找到它 src/first_steps.f90

src/first_steps.f90
module first_steps
  implicit none
  private

  public :: say_hello
contains
  subroutine say_hello
    print *, "Hello, first_steps!"
  end subroutine say_hello
end module first_steps

我们可以直接使用命令 fpm run 运行可执行文件

❯ fpm run
...
 Hello, first_steps!

类似地,fpm 已为测试创建了一个 stub,可以使用 fpm test 调用它

❯ fpm test
...
 Put some tests in here!

在使用“运行”和“测试”命令运行项目时,Fpm 会自动追踪你的项目更改情况。

摘要

在本教程中,你学习了如何

  • 从 fpm 命令行中创建一个新项目

  • 使用 fpm 构建并运行你的项目可执行文件

  • 通过 fpm 运行测试