Silverfrost Forums

Welcome to our forums

Using external procedures, subprograms or functions

4 Oct 2017 6:18 #20363

So far, all my FTN95 programs have been single units, that is, a main program with no other units. That single unit is in a file.

I’d like to extend to using additional units but have run into procedural issues. In particular, I want the main program to be in one file and an external function to be in another file. All my efforts to compile and run have been unsuccessful. I’ve only been able to get this to work if I merge the two units into a single file, perhaps using an INCLUDE statement. Can I do it with two separate files?

Here is the code in the two separate files:

(C:\Kalish_data\FORTRAN source\95\Fortran 95 Counihan\ch07p109\ch07p109_ex_003_Main.f95) WINAPP !for CLEARWIN PROGRAM Main ! ! Fortran 95, Martin Counihan ! Chapter 7, Introducing external procedures, page 109 ! Section 7.1, Functions page 109 ! ! The text provides a FUNCTION Cot(x) which determines the Cotangent ! of x, an angle given in radians. This is the main program which ! obtains an angle in degrees and uses the FUNCTION Cot to determine ! the Cotangent of an angle in degrees. ! IMPLICIT NONE REAL:: Cot, AngDeg, AngRad, CoTangent, Pi = 3.14159265358979 ! WRITE(,) 'Please enter an angle in degrees. We will & &determine its Cotangent. ...' READ(,) AngDeg AngRad = AngDegpi/180. WRITE(,) 'The angle = ', AngRad, ' in radians.' CoTangent = Cot(AngRad) WRITE(,*) 'The cotangent = ', CoTangent ! END PROGRAM Main

(C:\Kalish_data\FORTRAN source\95\Fortran 95 Counihan\ch07p109\ch07p109_ex_003_Function.f95) WINAPP !for CLEARWIN FUNCTION Cot(x) ! ! Fortran 95, Martin Counihan ! Chapter 7, Introducing external procedures, page 109 ! Section 7.1, Functions page 109 ! ! The text provides a FUNCTION Cot(x) which determines the Cotangent ! of x, an angle given in radians. This is that FUNCTION. ! IMPLICIT NONE REAL:: Cot, eps, s, c REAL, INTENT(IN):: x !Can use x but can't modify it. ! eps = EPSILON(x) Singularity: IF ((-eps.LE.x).AND.(x.LE.eps)) THEN WRITE(,) 'Error: cotangent function called with a zero argument' Cot = Huge(x) ELSE Singularity s = SIN(x) c = COS(x) Cot = c/s END IF Singularity ! END FUNCTION Cot

If I compile the FUNCTION in Plato, it says, “*** No main, WinMain or LibMain function *** Compilation” If I compile the Main program in Plato, it is successful.

I’m not able to get anywhere with project in Plato.

If I compile the Main program in Command Prompt with FTN95.exe, I get

WARNING the following symbols are missing: COT c:\Kalish_data\FORTRAN source\95\Fortran 95 Counihan\ch07p109\lgotemp@.obj (C:\KALISH_DATA\FORTRAN SOURCE\95\FORTRAN 95 COUNIHAN\CH07P109\CH07P109_EX_003_MAIN.F95) Creating executable: .EXE.exe Creating Linker map file: .MAP.map

If I compile the Function in Command Prompt with FTN95.exe, I get

*** No main, WinMain or LibMain function *** Compilation abandoned

Also, I haven’t been able to compile the two units so as to obtain .OBJ files for use with SLINK.exe.

There must be some way to do this.

Thanks, Dan

4 Oct 2017 7:46 (Edited: 13 Oct 2017 6:33) #20368

Here is my way of working using the Command Prompt.

  1. Download nice file management tool called Total Commander. My friends always thank me for this suggestion and install it first on any new computer. I have 6 (!) of them working at the same time, each for different project and having no idea how others may work with Windows efficiently without such great tool. Different colors help not to be buried in the information completely.

https://s25.postimg.org/86t59aqgv/Total_Comm.jpg

  1. use the command script like this changing two or more file names with your ones.

  2. Then working will be as easy and pleasant as clicking or Ctrl+Filename on keyboard. Your errors are in the files conveniently called with the letter A.

(All that was for 32bit case. Remove SIMPLE.DLL /stack:850000000 if this is 64 and add to FTN95 line the /64 switch for 64bit compiler, change SLINK to SLINK64, and SDBG to SDBG64. In the SLINK64 line add /file YourExeName.EXE though this is not needed in the latest version). You can remove in 32bits SIMPLE.DLL which is for making older Simpleplot utility called %pl looking nicer and /stack:850000000 is for large programs which utilize almost all your accessible memory

  1. If you just click on BAT file it will run debugger with many useful debug options. Calling the script with additional letter(s) will direct at different compilation options from checking everything to not checking anything, to optimize.

I use letters N, NN, S, NO for /NOCHECK and run, /NOCHECK and not run, /DEBUG with SDBG, and OPTIMIZE respectively. Z is for /NOCHECK with no other options The default is debugging and running with SDBG. With time you can make the script much more complex then that.

if (%1)==(n)     goto nocheck
if (%1)==(N)     goto nocheck

if (%1)==(s)     goto SDBG
if (%1)==(S)     goto SDBG

if (%1)==(nn)    goto nocheck2
if (%1)==(NN)    goto nocheck2

if (%1)==(no)    goto noch_opt
if (%1)==(NO)    goto noch_opt

if (%1)==(z)     goto Z
if (%1)==(Z)     goto Z



!.............. DEBUG UNDEF .........
del MProgram.exe
ftn95 MProgram.for /free /err /set_error_level error 298 /check /full_debug /no_truncate /zeroise /undef >a_FTN95___
ftn95 Subr.for /err /set_error_level error 298 /check /debug /undef /zeroise                             >a_FTN95pie___
slink MProgram.obj Subr.obj SIMPLE.DLL /stack:850000000                                                  >a_link___
goto SDBG

!..............NOCHECK .........
:nocheck
del MProgram.exe
ftn95 MProgram.for /free /no_truncate                    >a_FTN95___
ftn95 Subr.for                                           >a_FTN95pie___
slink MProgram.obj Subr.obj            /stack:850000000  >a_link___
MProgram.exe
goto end

!..............NOCHECK .........
:nocheck2
del MProgram.exe
ftn95 MProgram.for /free /no_truncate /set_error_level error 298  >a_FTN95___
ftn95 Subr.for                                                    >a_FTN95pie___
slink MProgram.obj Subr.obj            /stack:850000000           >a_link___
goto end


!..............Optimize .........
:noch_opt
del MProgram.exe
ftn95 MProgram.for /free /no_truncate /nocheck /opt /set_error_level error 298 >a_FTN95___
ftn95 Subr.for /opt                                         >a_FTN95pie___
slink MProgram.obj Subr.obj            /stack:850000000     >a_link___
goto end


!..............NOCHECK .........
:Z
del MProgram.exe
ftn95 MProgram.for /free /no_truncate                      >a_FTN95___
ftn95 Subr.for                                             >a_FTN95pie___
slink MProgram.obj Subr.obj                                >a_link___
goto end


!.............RUN SDBG .........
:SDBG
SDBG MProgram.exe


:end
4 Oct 2017 7:59 #20369

There is a section in the Plato 'getting started' topic about creating a 'project'. Here is the online link...

http://silverfrost.com/ftn95-help/plato/simplefortranproject.aspx

5 Oct 2017 7:02 #20380

Dan: Thanks for the reference to Total Commander. It looks promising. I'll add it to my utility tools SysInternals and Nir Software. I'll try your script when I'm back home.

Paul: I've already gone through that link. I still don't see how to use two different files. If it does explain that, please show me.

Oh, my post needs a correction: it's CONTAINS, not INCLUDE. The sample programs, say Part 1 L, in the FTN95 Tutorial Help use CONTAINS. It still doesn't help because we're still talking about a single file. 😃

6 Oct 2017 12:42 #20388

There is no need to over-complicate things. You have two files in your example and you are not using modules so ...

Compile each file:

ftn95 /c ch07p109_ex_003_Main.f95
ftn95 /c ch07p109_ex_003_Function.f95

Then link the object files:

slink ch07p109_ex_003_Main.obj ch07p109_ex_003_Function.obj

The order of compilation becomes important if you use modules so post back if you need help with them. Order of linking should never matter.

6 Oct 2017 2:27 #20389

Ooops David,

Didn't you read that /c now has a different function?

Eddie

6 Oct 2017 2:53 #20390

Thanks Eddie.

I am still using 7.2 here because of bugs in 8.1.

Where is the information on the new use of /C ?

Obviously my post above will need adapting if /C no longer means 'suppress link'. But the principles are the same.

David.

6 Oct 2017 3:09 #20392

Quoted from davidb Thanks Eddie.

I am still using 7.2 here because of bugs in 8.1.

Where is the information on the new use of /C ?

Obviously my post above will need adapting if /C no longer means 'suppress link'. But the principles are the same.

/Cfg Enters the compiler local configuration screen.

By default, no linking is done, so no option is needed to suppress linking. If you do want to link, the /link option is available. To compile, link and run, we have /LGO.

6 Oct 2017 8:19 #20393

Total Commander is not free. You've got 30 days and then its $40. 😦

I'll look at these command prompt commands later.

Dan: how to run those scripts? In a batch file?

6 Oct 2017 10:39 #20396

Yes, use that in the BAT file

I think Total Commander was free before (I bought it 20 years ago) with some little annoyance after 30 days...Anyway if you try it you will join 70+ million people who installed such type of Windows enhancement apps.

There are other similar utilities copying famous Norton Commander for DOS. On my Android cellphone I use Ghost Commander - the same kind of utilities, which on the phones is even better then TC

12 Oct 2017 7:28 #20416

The compiler option is /-Link. Now I can successfully compile the programs in command prompt, link the object files with SLINK.exe and run the executable file. Hooray!  However, SLINK has problems if the programs are compiled with the /64 option.

Note: in another thread, I asked 'What options should I select in order to produce object files? / -CLR and /-LINK? ' http://forums.silverfrost.com/viewtopic.php?p=18711#18711 It's nice to have the answer after two years of frustration. 😃 

12 Oct 2017 8:42 #20417

SLINK64 is the 64 bit linker.

13 Oct 2017 9:20 #20418

Quoted from kaliuzhkin ... Note: in another thread, I asked 'What options should I select in order to produce object files? ...http://forums.silverfrost.com/viewtopic.php?p=18711#18711 It's nice to have the answer after two years of frustration. 😃

Two years? John Campbell answered you after just three days.

Dan K, Wed Aug 26, 2015 3:56 pm: 'What options should I select in order to produce object files? '

John Campbell, Sat Aug 29, 2015 10:31 pm: '...You can compile the program using the command : FTN95 test This will produce a file test.obj or some error messages.'

13 Oct 2017 6:42 #20420

I do not know why, this is beyond me, but all compiler developers do not care to create very short movies 'How to start using compiler in 1 minute or less', movies 'Tricks of compilation', ''Tricks of debugging', movie demonstrating all the good examples etc. Take your teenage daughters and they will do 10 of them in one day. Good example - site iFixit dot com

https://www.youtube.com/watch?v=nlUQfXwvQLc

The start of using of anything is always the hardest part. One small thing is missing and the whole picture is total mess and darkness.

I remember Salford'a attempt of making such movies as far as 20 years back. This was about FTN90. But the compiler was a total flop and the presenter clearly knew that being very uncomfortable and afraid to look straight into the camera 😃. Was it bad or good but I bought it then 😃 That compiler was quickly dropped in favor of FTN95 which accomoddated older FTN77 and added new Fortran 90 features.

13 Oct 2017 11:56 #20423

Quoted from DanRRight I do not know why, this is beyond me, but all compiler developers do not care to create very short movies

Probably for the same reasons as for the Ten Commandments being distributed as HST (Heavy Stone Tablet) files rather than DVD.

A one minute movie is not going to cover much. Paul's recent movie on the new features of Plato, etc. is much longer.

A programmer's manual contains a table of contents and an index. These days, such manuals come in the form of files with lots of links that help with finding information and quickly navigating to the pages of interest. I am not aware of any such navigational aids for watching movies.

14 Oct 2017 1:42 #20425

I agree with Dan, Often a quick video of getting started can be invaluable for someone who has not used a ftn95 'style' compiler before.

A get started script could be as simple as:

Here is a text file of Fortran code, called example.f90. The .f90 indicates to the compiler it is a file containing Fortran code. FTN95 is a command, like dir, that can be used in a cmd.exe box to convert the code to binary (named example.obj) than can be used by a linker to generate a run program,

'FTN95 example.f90' generates example.obj 'FTN95 example.f90 /link' generates example.exe, which can be 'run' 'FTN95 example.f90 /lgo' will compile and run a Fortran program that is stored in a single file. SLINK example.obj generates example.exe, which can be 'run' SLINK example.obj file2.obj file3.obj generates example.exe, when the program is stored in multiple files.

This would be more than sufficient for a first time user to learn how to use FTN95 and SLINK and get started.

The documentation is missing simple tutorials. I could think of others I could have used years ago, like:

  • how to change the stack, then confirm you have !
  • how to generate and use a .lib file
  • how to generate and use a .dll file
  • how to generate a load .map file

I could still benefit from these for using FTN95 /64 !!

I remember when I first tried to use gFortran or ifort, after having used FTN95 for many years, it would have been good to have a tutorial 'where is the linker and do you need it !!'

Documentation is not designed for first time users .

14 Oct 2017 4:19 #20433

Perfect post, John. Exactly my thoughts.

/* Yesterday I tried to find how /timing works. And reading huge description in the Help file I found myself sleeping in the middle 😃

15 Oct 2017 11:33 #20444

Dan,

I am disappointed you have maligned /timing. It works by timing all routines that are compiled with /timing, so you omit the /timing option from library or low level routines you know are not a problem. I have found this to be an extremely useful option for finding slow code.

John

(I am sure I have posted about this option before)

16 Oct 2017 12:17 #20451

Quoted from JohnCampbell Dan,

I am disappointed you have maligned /timing.

John, as you, JohnS and original poster noted above the problem is in documentation.

I expected that first what documentation about /timing has to show is

  1. first line shows how /timing is applied (standard on any documentation) for us this is obvious but for novices definitely not
  2. take working example of some tiny code or take just a sketch of some arbitrary code
  3. show where timing information is displayed and how it looks

and only then

  1. add N pages and even pictures of explanation how it was implemented for those who have time to read that or those who are by some reason have problems with /timing or not satisfied with the method
16 Oct 2017 3:24 #20454

Dan,

I think you are asking for a' how to ...' tutorial, which I agree would be very helpful.

Please login to reply.