Novice coding question

Just installed gfortran on Mac M1; sample program:

program hello
! This is a comment line; it is ignored by the compiler
print *, ‘Hello, World!’
end program hello

Get error message:

f951: Error: Unexpected end of file in ‘hello.f90

What am I missing?

It might be an artifact of copy and paste; but you do not have ASCII single quotes around your string.
You need to make sure your file is saved as a plain ASCII text file. Also, double check that hello.f90 actually contains what you show here; just in case you did not save the file properly. Not sure what
commands you have available, but if you have them enter the commands

file hello.f90
cat -vet hello.f90

and show what they display.

Thank you for the suggestion. Yes, looks like issues with characters in the file:

USER % file hello.f90
hello.f90: ASCII text, with CR, LF line terminators
USER % cat -vet hello.f90
program hello^M ! This is a comment line; it is ignored by the compiler^M print *, ‘Hello, World!’^Mend program hello$

Do you have a suggested editor for Mac that shows what characters are present?

For a modern MAC it should be using LF (before OS X MAC used to just use CR) and when you paste Fortran code you should usually precede it with the line
```fortran
and end it with
```
or Discourse can make subtle changes. If you had the wrong quotes in your file it would have shown something like

M-bM-^@M-^XHello, World!M-bM-^@M-^Y

so assuming the output was not really all on one line, what command are you using to compile the code?
Other than the quotes, which look to have been an artifact of cut-and-paste, the code looks correct.

program fortrancode
write(*,*) 'Hello World!'
end program fortrancode

entered between the above delimiter lines the code shows as typed with color highlighting.

EDIT; MacOS uses LF line terminator

I use neovim on macOS M1, installed using Spack; as well as VSCode. I also installed gfortran using Spack, that works.

I have not had a Mac in quite a while, so @certik is there still a problem with line terminators? I would have expected the “cat -vet” output to look like

program hello$
! This is a comment line; it is ignored by the compiler$
print *, 'Hello, World!'$
end program hello$

but literal ctrl-M showed, and no explicit line terminator on the last line; even though the file command showed it as an ASCII file with CR/LF line terminators, which I thought was what MacOS used now (?)

Yes, macOS uses just LF just like Linux. Windows uses CRLF. In general macOS behaves like Linux for most things.

On my machines gfortran/ifort/nvfortran handles cr-lf and lf automatically, so that is rarely a problem by itself; but maybe that is still a problem then.

Thank you for the suggestions, it’s working!

Did three things (1st 2 I’d also done before):

Changed TextEdit to be plain text
Changed TextEdit to not add .txt
Cut and pasted urbanjost’s text and eliminated the ‘$’ at the end of the lines; this had the effect of a clean retype of the code

Great. Could have been a bit easier; but I don’t have a compatible platform. Note there is a program called dos2unix that converts between different platform-specific text file formats (there are others too) that is very handy for issues like this that you might find useful when dealing with other files as well. A quick look makes it look like it does not come with MacOS by default but appears to be available as a download.

The hexdump command could be used to see the line terminators:

$ hexdump -C hello.f90

CTRL+M (CR,LF terminators) will appear as 0D 0A in hexadecimal.

2 Likes

I do not have access to a modern Mac, so the following advice for Linux/Unix may not work on your Mac. If you are unable to obtain/create a dos2unix utility, there is one more thing to try.

The tr program is part of the binutils package of most Linux/Unix systems. If it is available, the command to remove the CR characters is

tr -d '\015' < hello.f90 > hello_nocr.f90

Yet another alternative is to see if your text editor has a menu item that allows you to change the EOL convention of the in-focus text file that you have opened in it.

I have never had serious issues with CR-LF on Mac. There can be some problems with pasted text depending on the source or files coming from Windows, but any decent text editor is supposed to take care of that (for instance TextWrangler/BBedit has a save option where one can chose LF or CR or CR-LF convention).

1 Like

Yes, tr is part of POSIX, and MacOS is POSIX compliant. I’m not sure if it is installed by default, or if it is part of the command line tools which is a separate install step. If someone is compiling fortran codes, then they should have the command line tools installed anyway.

$ which tr
/usr/bin/tr
1 Like