Fortran入门求助:HelloWorld程序编译失败
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
.fextension 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 helloworldstarting 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

