无需打开Force 2.0 GUI,能否通过命令提示符编译运行Fortran程序?
Absolutely! You can skip the Force 2.0 graphical interface entirely and compile/run your Fortran code straight from Command Prompt (CMD). Force 2.0 actually bundles the GNU Fortran compiler (gfortran) under the hood, so all the command-line tools you need are already included with your installation.
Here's a straightforward, step-by-step breakdown to get you started:
1. Make Sure the Compiler is Accessible (Set PATH if Needed)
First, confirm your system’s PATH environment variable includes the folder where Force 2.0 stores its gfortran executable. By default, this path is usually:
C:\Program Files\Force 2.0\mingw\bin
- To check if it’s already configured, open CMD and run:
If you see version details pop up, you’re ready to go. If not, you can temporarily add the path for your current CMD session:gfortran --version
For a permanent fix, add the directory to your system’s PATH via Windows’ Environment Variables settings.set PATH=%PATH%;C:\Program Files\Force 2.0\mingw\bin
2. Compile Your Fortran Source File
Use the cd command to navigate to the folder where your .f90 (or .f) source code is saved. For example:
cd C:\Users\YourUsername\FortranProjects
Then compile the code with gfortran:
gfortran my_program.f90 -o my_program.exe
- Replace
my_program.f90with the actual name of your source file - The
-o my_program.exeflag lets you name the output executable (feel free to use any name you prefer here)
If there are no syntax or logic errors in your code, a new .exe file will appear in the same folder.
3. Run the Compiled Program
Once compilation succeeds, launch the executable directly in CMD:
my_program.exe
You’ll see your Fortran program’s output printed right in the command prompt window.
Quick Troubleshooting Tips
- If you get a "command not found" error for
gfortran, double-check that your PATH is correctly pointing to the Force 2.0mingw\bindirectory. - If compilation fails, read the error messages closely—they’ll highlight line numbers and specific issues (like typos or undefined variables) in your code.
内容的提问来源于stack exchange,提问作者Madhurjya Sharma

