Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

wfguiteau

macrumors newbie
Original poster
Sep 2, 2011
2
0
Hello everyone,

I'm trying a foray into fortran (I'm a scientist and a lot of the code I work with is in fortran), so I'm starting from the beginning.

I used the following hello world program:

! My first Fortran 90 program!
! Greetings!

CHARACTER NAME*20

PRINT*, 'What is your name?'
READ*, NAME
PRINT*, 'Hi there, ', NAME
END

and compile it in the terminal using gfortran, i.e. 'gfortran hello.f90'. It SHOULD prompt me for the name input and then print the output, but it does neither. No error messages, creates a file called 'a.out', but no read and no print. In the absence of error messages, I have no clue what's going wrong. Any suggestions?
 
gfortran is just the compiler, it compiles the code into an executable (the a.out file). If you now run the a.out file (./a.out), it will actually execute your code.
 
Hello everyone,

I'm trying a foray into fortran (I'm a scientist and a lot of the code I work with is in fortran), so I'm starting from the beginning.

I used the following hello world program:



and compile it in the terminal using gfortran, i.e. 'gfortran hello.f90'. It SHOULD prompt me for the name input and then print the output, but it does neither. No error messages, creates a file called 'a.out', but no read and no print. In the absence of error messages, I have no clue what's going wrong. Any suggestions?

Your code tidbit is not F90. gfortran also does not have a direct 90 compiler, it hasn't been supported in a while. use mpif90 (part of openMPI), ifort, or absoft.

First and foremost make sure this is the version of gfortran you have http://gcc.gnu.org/wiki/GFortranBinaries

nextly this is proper f90
Code:
      [COLOR="Blue"]program[/COLOR] helloWorld
            [COLOR="Magenta"]!proper new style fortran declaration[/COLOR]
            [COLOR="blue"]character[/COLOR](len=50)   :: name
            
            [COLOR="Magenta"]!This is my ghetto fortran[/COLOR]
            [COLOR="blue"]write[/COLOR](*,*) 'Who you be gangster?'
            [COLOR="blue"]read[/COLOR](*,*) name
           [COLOR="Magenta"] !the trim is used to eliminate trailing blanks[/COLOR]
            [COLOR="blue"]write[/COLOR](*,*) 'Wow ', trim(name),', you be fortran gangsta!' 
           [COLOR="Magenta"] !i mean you could use a fmt statement too, but i didnt feel like looking one up for this example[/COLOR]
      [COLOR="blue"]end program[/COLOR]

the above code will compile with gfortran, and do exactly what it is you wanted to do in your code snippet. I double checked just to be sure.

------------------
now as to running
save the above code as hello.f90
go in terminal and type: gfortran hello.f90 -o hello
then to execute, type: ./hello

youll see it all work now

(if it doesnt allow you to execute, make sure you chmod to executable)
-------------------

finally if you want to compile openMPI for parallel fortran (we use it almost exclusively in computational physics), i explained it in an old post of mine here
https://forums.macrumors.com/posts/12905721/
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.