首页 / 预测世界杯足球

使用fpm的第一步

2026-02-09 17:27:44预测世界杯足球 8290

使用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

包清单包含新项目所需的所有元数据。接下来,我们检查主可执行文件app/main.f90,fpm已经为我们生成:

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会创建了一个用于测试的存根,可以使用命令fpm test调用:

❯ fpm test

...

Put some tests in here!

fpm将在使用运行命令(run)和测试命令(test)运行项目时自动跟踪项目中的更改。

总结

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

从fpm命令行创建新项目;

使用fpm构建和运行项目可执行文件;

使用fpm运行测试。