Frage :Ich möchte die Grundlagen verstehen, wie man ein Fortran-Programm schreibt und ausführt auf Linux-Betriebssystem. Können Sie das an einem einfachen Beispiel erklären?
Antwort :Lassen Sie uns in diesem Artikel sehr schnell wiederholen, wie man ein grundlegendes Hello World Fortran-Programm schreibt und führen Sie das *.f-Programm auf Linux- oder Unix-Betriebssystemen aus.
1. Schreiben Sie ein Hello World Fortran-Programm
Erstellen Sie das Programm helloworld.f mit dem Vim-Editor wie unten gezeigt.
$ vim helloworld.f program hello print *,"Hello World!" end program hello
Sie können auch eine kürzere Version des Fortran-Programms „Hello World“ schreiben, wie unten gezeigt.
$ vim helloworld.f PRINT *,"Hello World!" END
2. Stellen Sie sicher, dass der Fortran-Compiler auf Ihrem System installiert ist
Stellen Sie sicher, dass der Fortran-Compiler wie unten gezeigt auf Ihrem System installiert ist.
$ whereis gfortran gfortran: /usr/bin/gfortran /usr/share/man/man1/gfortran.1.gz $ which gfortran /usr/bin/gfortran $ dpkg -l | grep gfortran ii gfortran 4:4.3.3-1ubuntu1 The GNU Fortran 95 compiler ii gfortran-4.3 4.3.3-5ubuntu4 The GNU Fortran 95 compiler ii libgfortran3 4.3.3-5ubuntu4 Runtime library for GNU Fortran applications
Installieren des GNU Fortran-Compilers.
Wenn Sie keinen GNU Fortran-Compiler haben, installieren Sie ihn wie unten gezeigt.
$ sudo apt-get install gfortran
3. Kompilieren Sie das Fortran-Programm
Kompilieren Sie helloworld.f mit dem gfortran-Befehl wie unten gezeigt. Dadurch wird die a.out-Datei erstellt.
$ gfortran -ffree-form helloworld.f $ ls -l -rw------- 1 ramesh ramesh 55 Oct 24 23:13 helloworld.f -rwx------ 1 ramesh ramesh 9545 Oct 24 23:14 a.out*
4. Führen Sie das Fortran-Programm aus (a.out)
$ ./a.out Hello World! $ mv a.out helloworld $ ./helloworld Hello World!