PDA

View Full Version : Error compiling with gfortran (from a beginner)




MaKB
Sep 7, 2010, 11:35 AM
Hello

I have tried to compile the program:

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

using gfortran. I saved the file in my home directory as hello.f90 and wrote:

gfortran hello.f90

being in my home directory.
I get the error:

/usr/bin/ld: hello.f77 bad magic number (not a Mach-O file)
/usr/bin/ld: warning can't open dynamic library: /libgcc_s.1.dylib referenced from: /usr/local/lib/gcc/i386-apple-darwin8.10.1/4.3.0/../../../libgfortran.dylib (checking for undefined symbols may be affected) (No such file or directory, errno = 2)

Can anyone help me? Which program do people use to write Fortran scripts and do you save them as .f90? Have I tried to compile the file the right way?

I must say that I am an absolute beginner in both using the terminal and at programming in general.
I have had some problems installing the gfortran. I downloaded a complete install from http://www.macresearch.org/xcode_gfortran_plugin_update. The installation was without problems (I have had problems with versions from the GNU webpage). But is there anyway I can check that I did it right?

Thanks in advance.



balamw
Sep 7, 2010, 11:41 AM
The installation was without problems (I have had problems with versions from the GNU webpage). But is there anyway I can check that I did it right?

See the other recent thread about this.

http://forums.macrumors.com/showthread.php?t=1006986

You code compiles and runs perfectly with gfortran installed from the links provided there.

EDIT:

Looks like you may only have downloaded an updater of sorts.

Assumptions:
Your source file is in ~/Documents/hello.f90
gcc-snwleo-intel-bin.tar.gz (http://prdownloads.sourceforge.net/hpc/gcc-snwleo-intel-bin.tar.gz?download) has been downloaded as ~/Downloads/gcc-snwleo-intel-bin.tar.gz


$ cd /
$ sudo tar zxvf ~/Downloads/gcc-snwleo-intel-bin.tar.gz
$ cd ~/Documents
$ gfortran -o hello hello.f90
$ ./hello
Hello!


EDIT 2: Why (as a beginning programmer) are you trying to learn FORTRAN? Are you an engineering student?

B

Hansr
Sep 7, 2010, 12:02 PM
EDIT 2: Why (as a beginning programmer) are you trying to learn FORTRAN? Are you an engineering student?

B

My thought exactly, why would you do something so degrading to yourself :P

balamw
Sep 7, 2010, 12:06 PM
My thought exactly, why would you do something so degrading to yourself :P

Not degrading, per se, but not a very good language for a beginner unless they have some very specific needs.

It's kind of like deciding to learn a second language and picking Latin. :p

B

lee1210
Sep 7, 2010, 12:27 PM
Not degrading, per se, but not a very good language for a beginner unless they have some very specific needs.

It's kind of like deciding to learn a second language and picking Latin. :p

B

If you are going into medicine, law, or linguistics Latin might not be a bad choice.

If you're going into scientific research, or an industry tied closely (big oil still seems pretty profitable), fortran might not be a bad choice.

I can only assume the OP needs fortran for something, not that someone said that it was the best teaching language available.

As long as you make use of Fortran 90+ and don't do the 77 and below stuff (all caps, spacing based on punchcards, etc.) it's not *that* bad. The ambiguity of function/subroutine calls and array access is probably my biggest complaint. The array processing built-in functions are actually pretty cool. I am not implying it's a great choice, but some comments here make it out to be a scourge. It's not PHP or BASIC =).

-Lee

balamw
Sep 7, 2010, 12:37 PM
Yeah I meant exactly that with "unless they have some very specific needs." I was seeking to suss that out from the OP as to why they chose FORTRAN to learn programming.

Having spent much of the past couple of years dealing with MATLAB code, I find it far superior to FORTRAN for most numerical stuff, especially linear algebra.

It's not PHP or BASIC =).

Both of which have their uses too. ;)

B

MaKB
Sep 7, 2010, 03:19 PM
First: I study physics and as far as I know it can be very useful to know how to write in Fortran. At least to be able to compile and to write simple scirpts before I HAVE to be able to. As far as I know it is very much used in when you want to simulate something in physics.

On topic:

I tried what balamw suggested. The file I downloaded is a .mpkg file and I guess I have to use another command than 'tar zxvf' to envoke the file.

I don't know if it is just an update I have downloaded, but I have had some problems after I installed a version of gfortran for OS 10.6 on my computer from http://gcc.gnu.org/wiki/GFortranBinariesMacOS. I tried to uninstall it using

sudo rm -r /usr/local/gfortran /usr/local/bin/gfortran

which I found on the same homepage. The intel-version on this homepage is for leopard, thats where I made the mistake. But even after uninstalling I was not able to install the other one (I run 10.4). It just said "Nothing to install".
I might not have removed the wrong version correctly?

Any suggestions on how to get on from here? Could I in some way remove everything with relation to gfortran and try to install again.

Thanks

chown33
Sep 7, 2010, 03:33 PM
Start by identifying what Mac model you're using. It's unlikely the PPC version will be practical on an x86 Mac, and I guarantee it's impossible for the x86 version to run on a PPC Mac.

Next, identify exactly what OS version you're using. The latest 10.4 is 10.4.11. Is that what you're using? If not, then you should probably upgrade, as almost anything that still runs on 10.4 will need the latest version of it.

How did you conclude that http://gcc.gnu.org/wiki/GFortranBinariesMacOS is for 10.6? It says right on the web page that it's for 10.4 and 10.5.
(Never mind, I downloaded it and the problem is obvious. The download "gfortran-macosx-x86.dmg" actually contains "gfortran-4.5.0-x86_64-SnowLeopard" when the dmg is mounted. So the GCC-hosted download is fscked.)

It also says "Needs Xcode 2.5", which is the latest version of Xcode that still runs on 10.4. Did you install Xcode 2.5 or not? If not, then you should probably do that first.

balamw
Sep 7, 2010, 05:54 PM
What chown33 says is right. I was assuming that you were running 10.6 on an Intel Mac. If not, you need to step back and figure out the basics first.

If you don't know which Mac you have, use :apple: -> About this Mac -> More Info and report back the Model Identifier. Your OS version is available from :apple: -> About this Mac -> Software -> System Version. Xcode version is available from Xcode -> About Xcode. Start there.

NOTE: I'm a physicist myself, though working in EE, and I tend to use MATLAB for most of my numerical needs these days, but I maintain some code in C/C++. I haven't used FORTRAN for much since the mid 90s. GNU Octave would probably be my choice if I couldn't have MATLAB. Even Excel works for simple stuff that in the not too distant past required code. If I'm interfacing to lab equipment I would do that in LabView or MATLAB too, and definitely not FORTRAN.

An indication of the direction things have gone, is that the venerable Numerical Recipes book, that used to be FORTRAN based translated to other languages is now C++ based only in the current third edition. http://www.nr.com/.

Since you have a Mac, here's a resource for technical programming using the Mac. http://www.macresearch.org/cocoa_for_scientists that may be of interest to you.

B

Hansr
Sep 8, 2010, 04:16 AM
First: I study physics and as far as I know it can be very useful to know how to write in Fortran. At least to be able to compile and to write simple scirpts before I HAVE to be able to. As far as I know it is very much used in when you want to simulate something in physics.

Most people I know in physics and the PHDs in physics we have here at work are used to doing stuff in MATLAB or R, it's pretty much just the 35+ yo guys that know any fortran at all. I recommend looking into R and maybe Octave (open source Matlab with less built in toolkits basically) as alternatives instead of fortran. Then add in C/C++/CUDA if you need more speed for calculations.

balamw
Sep 8, 2010, 07:51 AM
Most people I know in physics and the PHDs in physics we have here at work are used to doing stuff in MATLAB or R, it's pretty much just the 35+ yo guys that know any fortran at all. I recommend looking into R and maybe Octave (open source Matlab with less built in toolkits basically) as alternatives instead of fortran. Then add in C/C++/CUDA if you need more speed for calculations.

I'd put it this way:


If you want to learn to code to get things done: Learn MATLAB/Octave or use Excel/R for data analysis.
If you just want to learn to code: Learn C/C++ using gcc then add an IDE (Xcode/Eclipse/...)
When the time comes for you need to use a particular library or write code for a supercomputer, then learn FORTRAN.


As a beginner, you want to be able to get help from as many places as you can, so you want to learn something that is widely used and many folks are familiar with. C/C++ is exactly that, and many of the things you learn can be useful later wen you do move to FORTRAN or another language.

You can actually learn a lot about writing code just by building spreadsheets in Excel.

(NOTE: My age is showing as I still capitalize FORTRAN and I suddenly feel the urge to write in all caps and start each line exactly at character 8 :p ).

B

MaKB
Sep 8, 2010, 01:05 PM
Okay, cool:

I am jusing a Intel Macbook (Model-id:MacBook1,1) with OS 10.4.11. I have version 2.5 of Xcode.

But I might follow your advice. I might try to find some execises for C++ and MATLAB. And I'll try to figure out about Cocoa. Haven't heard about it before.

But the reason I started wanting to figure out about Fortran is that I study Geophysics and for example in climate simulations you use fortran scripts which are then procesed by the supercomputer at the department.

Thanks a lot.

lee1210
Sep 8, 2010, 01:12 PM
(NOTE: My age is showing as I still capitalize FORTRAN and I suddenly feel the urge to write in all caps and start each line exactly at character 8 :p ).

B

I'm 28 and know Fortran better than all but one person I know. The product of ours that uses it started about 30 years ago, so there were a number of people at the beginning that weren't "hip" to F77 yet, and were still using F66 ALL CAPS for code, etc. Now if I open a file and it's in all caps I have a good idea that it hasn't been touched in a very long time, and am scared at what other tomfoolery I am going to find there.

-Lee

balamw
Sep 8, 2010, 01:50 PM
supercomputer

Say no more. You will definitely ultimately need to learn FORTRAN at some point.

Aside: Any reason you are sticking with 10.4? I have an iMac that is also a Core Duo from 2006 and it runs 10.6 very nicely. Though I did already have 2 GB of RAM. It was worth $29 just to eliminate the PPC code from the HDD.

B