Create a fortran source file and compile/run it

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read how to get help
Post Reply
Preon-777
Level 1
Level 1
Posts: 8
Joined: Sun Jul 22, 2018 10:09 pm

Create a fortran source file and compile/run it

Post by Preon-777 » Sun Jul 22, 2018 10:28 pm

Hopefully, this question makes sense. I speak Windows, but am totally lost using Linux.

I just purchased a computer running Linux Mint with Windows dual boot. It will be used as part of a cancer research project using radioactive beams to treat tumors. The Fortran codes are running on Windows machines, but new versions are only being supplied in versions that run on Linus machines (tar balls or .tar files). I need to take a first step to begin to use the machine.

As a test case, I want to write a very short Fortran code (call it x1.f) using xed. The compile it using gfortran. xed and gfortran are on the machine, but I am having difficulty using them from the same location.

1. How and in what folder do I create x1.f so that gfortran recognizes it. I created it on my desktop, then went into terminal mode. When I gave a gfortran command, the terminal window said no input file.

2. In what folder to I create x1.f so that it can be compiled using gfortran.

3. What one line gfortrtan command is used to compile x1.f, generate the executable x1.exe, and provide a list of errors in a file called errfile.

Any assistance would be greatly appreciated.

Your kind assistance is requested.

User avatar
xenopeek
Level 24
Level 24
Posts: 24099
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Create a fortran source file and compile/run it

Post by xenopeek » Mon Jul 23, 2018 2:16 am

You can put your fortran source file in any directory you want. When opening a terminal you must be in that directory to run the compile command.

So for example if you put x1.f in your Desktop directory, you can open your file manager, then either right-click the Desktop directory you see in your home directory or first go into the Desktop directory and then right-click an empty spot in it, and finally select from the context menu the right-click brought up "Open in Terminal" or similar worded menu option.

You can also do it by opening the terminal and then first changing to the directory you have put your x1.f file in. Like with the command cd Desktop in this case, to change directory (cd) to the Desktop directory.

Once you have a terminal open in the directory where you have your x1.f file (you can print the current directory with command pwd and list the contents of the directory with command ls to check), you can run your compile command like this:
gfortran x1.f -o x1
That tells gfortran to compile the single file x1.f and to write the output to the file x1 (no .exe, as Linux doesn't use file extensions for executable files). Mind that Linux is case-sensitive so you must type filenames correctly. x1.f; not X1.f or x1.F.

You can then run the program you created, from the directory it is in, by prefixing it with ./ to indicate the file you want to run is found in the current directory. So:
./x1

The compiler will print any warnings and errors to the terminal and abort compilation on errors. BTW, to enable warnings for usage that the gfortran developers suggest you should avoid, add the "-Wall" option so:
gfortran x1.f -Wall -o x1

Anyway, gfortran doesn't provide options to write the warnings or errors to a file but you could try running the compile command like this to use standard Linux output redirection:
gfortran x1.f -Wall -o x1 &> output
That would redirect stdout and stderr both to write to the file "output". This way nothing gets printed on your terminal, only in that file. Alternatively you can do it like this:
gfortran x1.f -Wall -o x1 | tee output
Which creates the same "output" file but does also print everything on your terminal.

At this stage I think good to note that if you're a fortran programmer yourself it may be fruitful to select and install an IDE for fortran. An IDE will run the compiler for your right from the editor and also show you the errors and warnings in an output window in the editor itself.
Image

Preon-777
Level 1
Level 1
Posts: 8
Joined: Sun Jul 22, 2018 10:09 pm

Re: Create a fortran source file and compile/run it

Post by Preon-777 » Sat Jul 28, 2018 9:18 pm

Thank you so much for your help. I am now able to create a file in an arbitrary directory, compile, execute, and capture its output.

Post Reply

Return to “Newbie Questions”