Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

gfortran issue

1,213 views
Skip to first unread message

john.ha...@gmail.com

unread,
Aug 13, 2016, 2:48:00 PM8/13/16
to
Just installed gfortran (gfortran-6.1-ElCapitan.dmg) on my Mac and tried to run my first program with the following results:

Aug 13, 2016, 1:28:17 PM
~/Programming/Fortran/HelloWorld.f90
--------------------------------------------------------------------------------
/Users/John/Programming/Fortran/HelloWorld.f90:1:2:

#!/usr/local/bin/gfortran

!My first Fortran program

program hello
print *,'Hello World!'
end program hello

1
Warning: Illegal preprocessor directive
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

Any ideas would be greatly appreciated.

Thanks, John

Richard Maine

unread,
Aug 13, 2016, 4:15:03 PM8/13/16
to
I'm not entirely sure exactly what the content of your source file is
and what you typed to try to compile it, but it sort of looks like you
have the #!/usr/local/bin/gfortran line as part of your source file. Its
been a while for me, but that looks like a typical shell script trick to
use a special comment syntax to get the right shell (or other program)
to run on the file. However, # is not a comment character in Fortran,
so if that's what you are trying to do, it isn't likely to work. Put
just the Fortran source code in your source file - nothing else. Then
invoke the compiler directly instead of via a comment trick.

Or maybe I missed what is going on, but that's my first impression.

--
Richard Maine
email: last name at domain . net
dimnain: summer-triangle

John Harris

unread,
Aug 13, 2016, 5:07:20 PM8/13/16
to
Hi Richard,

Here is the code:

program hello
print *,'Hello World!'
end program hello

Here is the results from the command line:

JohnsMac:Fortran John$ gfortran HelloWorld.f95
HelloWorld.f95:1:2:

end program hellorld!'ramn
1
Warning: Illegal preprocessor directive
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

By the way the #! line is called a shebang to tell where the compiler is located.

Themos Tsikas

unread,
Aug 13, 2016, 6:02:18 PM8/13/16
to
Hello John,

"shebang" lines are useful for interpreted code. gfortran is a compiler, not an interpreter. You need to take such lines out of your source file. The error message is still saying that it sees a # character but what follows is not a recognized by the compiler.

Themos Tsikas, NAG Ltd

Richard Maine

unread,
Aug 13, 2016, 6:06:08 PM8/13/16
to
John Harris <john.ha...@gmail.com> wrote:

> Here is the code:
>
> program hello
> print *,'Hello World!'
> end program hello
>
> Here is the results from the command line:
>
> JohnsMac:Fortran John$ gfortran HelloWorld.f95
> HelloWorld.f95:1:2:
>
> end program hellorld!'ramn
> 1
> Warning: Illegal preprocessor directive
> Undefined symbols for architecture x86_64:
> "_main", referenced from:
> implicit entry/start for main executable
> ld: symbol(s) not found for architecture x86_64
> collect2: error: ld returned 1 exit status
>
> By the way the #! line is called a shebang to tell where the compiler is
> located.

Ah, yes. It's been long enough that I forgot the term, but that does
sound familliar. I would not count on it working with Fortran for the
reasons mentioned before. I suppose it might happen to work for some
installations, but I wouldn't count on it (not to speak of the fact that
it is inherently non-portable as compilers are located in different
places on different systems).

Afraid I'm out of good ideas. I'd guess that the compiler isn't
installed correctly, or perhaps that it is not an appropriate compiler
build for your system. Do note that you also need an appropriate XCode
installed. Someone else might be able to offer more constructive advice
than I can.

I just now copied and ran your code on the iMac I'm posting this from,
and it ran fine. I'm also using gfortran 6.1, but I'm on OS X 10.10.5.

As an unrelated side note, I discourage using file extensions like .f95;
the usual convention on is that the file extension indicates the source
form (.f for the old fixed source form or .f90 for free source form). It
is unfortunate that the .f90 looks like it indicates a version of the
standard, which has encouraged people to use other extensions like .f95,
.f03, and I suppose also probably others. Some compilers accept those
extensions as being synonymous with .f90, but it is most portable to
just use .f90. That's clearly not related to your current problem,
though. (Gfortran 6.1 does appear to accept the .f95).

Richard Maine

unread,
Aug 13, 2016, 6:13:20 PM8/13/16
to
P.S. Are you sure you took the shebang out of the code before trying to
compile? Perhaps did somethng like delete it in the editor, but fail to
save the edited file? (I'm not meaning that as insulting; it's an error
I - and I'm sure others - have made). The shebang would explain the
"Illegal preprocessor directive" warning; I can duplicate that. It
doesn't explain the other problems, though.

herrman...@gmail.com

unread,
Aug 13, 2016, 8:42:39 PM8/13/16
to
On Saturday, August 13, 2016 at 3:13:20 PM UTC-7, Richard Maine wrote:

(snip)

> > > program hello
> > > print *,'Hello World!'
> > > end program hello

(snip)

> > > Warning: Illegal preprocessor directive
> > > Undefined symbols for architecture x86_64:
> > > "_main", referenced from:
> > > implicit entry/start for main executable
> > > ld: symbol(s) not found for architecture x86_64
> > > collect2: error: ld returned 1 exit status

> > > By the way the #! line is called a shebang to tell where the compiler is
> > > located.

(snip)

> > Afraid I'm out of good ideas. I'd guess that the compiler isn't
> > installed correctly, or perhaps that it is not an appropriate compiler
> > build for your system. Do note that you also need an appropriate XCode
> > installed. Someone else might be able to offer more constructive advice
> > than I can.

It looks like a link problem finding appropriate library routines
for a 64 bit system. That might happen if you install a 64 bit compiler
and 32 bit library, or just don't install the right library, or install it
in a different place than the compiler expects it to be.

(snip)

> P.S. Are you sure you took the shebang out of the code before trying to
> compile? Perhaps did somethng like delete it in the editor, but fail to
> save the edited file? (I'm not meaning that as insulting; it's an error
> I - and I'm sure others - have made). The shebang would explain the
> "Illegal preprocessor directive" warning; I can duplicate that. It
> doesn't explain the other problems, though.

It seems to be generating a warning for it.

It sounds like the C preprocessor is on, which would generate messages
about lines starting with #, but that doesn't explain the other problem.

-- glen

cyrmag7

unread,
Aug 14, 2016, 5:24:20 AM8/14/16
to
On Saturday, August 13, 2016 at 1:48:00 PM UTC-5, John Harris wrote:
> Just installed gfortran (gfortran-6.1-ElCapitan.dmg) on my Mac
> and tried to run my first program with the following results:
> #!/usr/local/bin/gfortran
>

As others have pointed out, the octothorp ('#') is not valid in that position in Fortran source code. If you do not want to remove the compiler path from your source file, replace the she-bang by a he-bang thus:

!#/usr/local/bin/gfortran

This will work because in Fortran a '!' denotes the start of an inline comment.

herrman...@gmail.com

unread,
Aug 14, 2016, 5:16:01 PM8/14/16
to
Are there shells that recognize that?

Reminds me, when I was using Octave, I put # comments in my programs, as I
thought that is what they were supposed to be, and it worked.

Later I tried to run them with Matlab, and all failed. It seems that Matlab uses %,
where Octave allows either % or #.

Seem to me that you could use a #! comment in a Makefile, that would then
compile and run your Fortran program.

mecej4

unread,
Aug 14, 2016, 6:22:20 PM8/14/16
to
It is not clear what the OP is doing. From his second post in this
thread, where he showed the command

$ gfortran HelloWorld.f95

it could be concluded that the program source is in a file with the .f95
suffix, and the file name is used as an argument to Gfortran.

The Cygwin version of Gfortran 5.3 does not show an option for taking
the source from standard input. I suppose that Gfortran compiled for
other platforms may accept the '-' and '-x f90' options to make that
possible.

-- mecej4

mecej...@nospam.invalid
(Replace four by 4, nospam by operamail, invalid by com,
and remove all underscores)

Richard Maine

unread,
Aug 14, 2016, 6:53:27 PM8/14/16
to
mecej4 <mecej...@nospam.invalid> wrote:

> It is not clear what the OP is doing. From his second post in this
> thread, where he showed the command
>
> $ gfortran HelloWorld.f95
>
> it could be concluded that the program source is in a file with the .f95
> suffix, and the file name is used as an argument to Gfortran.

Hmm. I'm confused as to what you think is not clear about that. Seems
about as explicit as I could imagine. I'll admit to having been confused
by his initial post, but that second one, which you cite, doesn't leave
much to the imagination. Indeed, I'd consider showing the exact command
typed, as the OP did in his second post, to be much clearer than
describing it as using the file name as an argument to GFortran.
Somewhat reminiscent of the very common exhortation here to show code
instead of describing it.

(I have, as previously expressed, some question as to whether the source
file was actually exactly as shown, and other questions about the
installation of the compiler and related tools, but those seem unrelated
to the above.)

mecej4

unread,
Aug 14, 2016, 11:27:38 PM8/14/16
to
Well, the OP ended the same 2nd post with an explanation of the
"shebang", which has no bearing on the earlier part of that post; to me,
that suggested that the program shown in the first post could have been
a shell script, even though he gave that file a suffix of ".f90". At
this point, the distinction does not matter.

complex...@gmail.com

unread,
Aug 16, 2016, 9:08:44 AM8/16/16
to
On Saturday, August 13, 2016 at 11:07:20 PM UTC+2, John Harris wrote:
> On Saturday, August 13, 2016 at 3:15:03 PM UTC-5, Richard Maine wrote:
Hello,

The first line of output is "Warning: Illegal preprocessor directive". As you have mention ".f90" and ".f95" file names, do you happen to have duplicate files? In which case, I would recommend to check everything in a very step-by-step manner.

1. Check the file name and the file content once more.
2. Type "which gfortran" to make sure that you are running the gfortran that you think you are running and not another one.
3. The instructions at https://gcc.gnu.org/wiki/GFortranBinariesMacOS mention that you need both the Apple Developer Tools installed and the "command line developer tools". Is it the case on your machine?
4. If there is a linking error, try "gfortran -c". It should produce a ".o" file if the syntax etc is correct.

Regards,

Pierre

Norman Worth

unread,
Aug 20, 2016, 11:45:36 AM8/20/16
to
remove the shebang, then: gfortran HelloWorld.f95 -o Helloworld
0 new messages