You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Fortran入门求助:HelloWorld程序编译失败

Fixing g95 Compile Errors for Your Fortran Hello World Program

Hey there! I totally get hitting confusing errors when starting out with a new language—let's get this sorted out quickly.

What's Causing the Errors?

The root issue is how g95 interprets your file's syntax format:

  • By default, g95 treats files with the .f extension as fixed-form Fortran (the old Fortran 77 style), which has strict column rules:
    • Columns 1-5 are reserved for numeric statement labels
    • Column 6 is for line continuation markers
    • Actual code must start at column 7
  • Your code uses free-form Fortran (modern Fortran 90+ syntax), which doesn't have these column restrictions. Since you wrote program helloworld starting at column 1, g95 misinterprets it as a non-numeric statement label, hence the errors.

How to Fix It

You've got two straightforward solutions:

Option 1: Rename Your File (Recommended)

Change your file's extension from .f to .f90—g95 automatically recognizes .f90 files as free-form. Then re-run your compile commands:

# Compile to object file
g95 -c fort.f90
# Link to create executable
g95 fort.f90 -o fort

Or combine it into one step:

g95 fort.f90 -o fort

Option 2: Force Free-Form Compilation (Keep .f Extension)

If you don't want to rename the file, add the -ffree-form flag to your compile command to explicitly tell g95 to treat the file as free-form:

g95 -ffree-form fort.f -o fort

Verify It Works

Once compilation finishes without errors, run the executable in your command prompt:

fort.exe

You should see Hello World! printed out as expected.

内容的提问来源于stack exchange,提问作者Ziltoid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:22:59