cfortran.html 106 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<html>
<head>
<title>cfortran.h:  Interfacing C or C++ and FORTRAN</title>
</head>

<BODY BGCOLOR="#FFFFFF">
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=5 WIDTH="100%">
<TR BGCOLOR="#0000FF"><TD><A HREF="http://www.cern.ch/">
<IMG SRC="cernbanner.gif" BORDER=0></A></TD></TR></TABLE>

<h1>cfortran.h:  Interfacing C or C++ and <i>FORTRAN</i></h1>

<hr>

<b>Author:</b><a href="http://www-zeus.desy.de/~burow">Burkhard Burow</a> <br>
<b>Email:</b> burow@desy.de <br>
<b>www:</b> <a href="http://www-zeus.desy.de/~burow/cfortran">www-zeus.desy.de/~burow/cfortran</a> <br>

<hr>
<p>
<b>Supports:</b>
<FONT COLOR="#993300"><pre>
          Alpha and VAX VMS, Alpha OSF, DECstation and VAX Ultrix, IBM RS/6000, 
          Silicon Graphics, Sun, CRAY, Apollo, HP9000, LynxOS, Convex, Absoft,
          f2c, g77, NAG f90, PowerStation <i>FORTRAN</i> with Visual C++, NEC SX-4,
          Portland Group.
</pre></font>
C and C++ are generally equivalent as far as <tt>cfortran.h</tt> is concerned.
Unless explicitly noted otherwise, mention of C implicitly includes C++.
C++ compilers tested include: 
<p><FONT COLOR="#993300"><pre>
  SunOS> CC +p +w      # Clean compiles.
  IRIX>  CC            # Clean compiles.
  IRIX>  CC -fullwarn  # Still some warnings to be overcome.
  GNU>   g++ -Wall     # Compiles are clean, other than warnings for unused
                       #   cfortran.h static routines.
</pre></font>

<b>N.B.</b>: The best documentation on interfacing C or C++ and <i>FORTRAN</i> is in
      the chapter named something like 'Interfacing C and <i>FORTRAN</i>'
      to be found in the user's guide of almost every <i>FORTRAN</i> compiler.
      Understanding this information for one or more <i>FORTRAN</i> compilers
      greatly clarifies the aims and actions of <tt>cfortran.h</tt>.
      Such a chapter generally also addresses issues orthogonal to <tt>cfortran.h</tt>,
      for example the order of array indices, the index of the first element,
      as well as compiling and linking issues.


<h2> Short Summary of the Syntax Required to Create the Interface</h2>

e.g. Prototyping a <i>FORTRAN</i> subroutine for C:

<tt>PROTOCCALLSFSUBni</tt> is optional for C, but mandatory for C++.
<FONT COLOR="#993300"><pre>
                 PROTOCCALLSFSUB2(SUB_NAME,sub_name,STRING,PINT)
#define SUB_NAME(A,B) CCALLSFSUB2(SUB_NAME,sub_name,STRING,PINT, A,B)

                                ^     -                                       -
       number of arguments _____|    |   STRING   BYTE    PBYTE       BYTEV(..)|
                                  /  |   STRINGV  DOUBLE  PDOUBLE   DOUBLEV(..)|
                                 /   |  PSTRING   FLOAT   PFLOAT     FLOATV(..)|
        types of arguments ____ /    | PNSTRING   INT     PINT         INTV(..)|
                                \    | PPSTRING   LOGICAL PLOGICAL LOGICALV(..)|
                                 \   |  PSTRINGV  LONG    PLONG       LONGV(..)|
                                  \  |   ZTRINGV  SHORT   PSHORT     SHORTV(..)|
                                     |  PZTRINGV  ROUTINE PVOID      SIMPLE    |
                                      -                                       -
</pre></font>

e.g. Prototyping a <i>FORTRAN</i> function for C:

<FONT COLOR="#993300"><pre>
/* PROTOCCALLSFFUNn is mandatory for both C and C++. */
PROTOCCALLSFFUN1(INT,FUN_NAME,fun_name,STRING)
#define FUN_NAME(A)  CCALLSFFUN1(FUN_NAME,fun_name,STRING, A)
</pre></font>
e.g. calling <tt>FUN_NAME</tt> from C:
<FONT COLOR="#993300"><pre>
    {int a; a = FUN_NAME("hello");}
</pre></font>

e.g. Creating a <i>FORTRAN</i>-callable wrapper for
     a C function returning void, with a 7 dimensional integer array argument:
     [Not supported from C++.]

<FONT COLOR="#993300"><pre>
FCALLSCSUB1(csub_name,CSUB_NAME,csub_name,INTVVVVVVV)
</pre></font>

e.g. Creating a <i>FORTRAN</i>-callable wrapper for other C functions:
<FONT COLOR="#993300"><pre>
FCALLSCFUN1(STRING,cfun_name,CFUN_NAME,cfun_name,INT)
           [ ^-- BYTE, DOUBLE, FLOAT, INT, LOGICAL, LONG, SHORT, VOID  
             are other types returned by functions.       ]
</pre></font>       

e.g. COMMON BLOCKs:
<FONT COLOR="#993300"><pre>
<b>FORTRAN:</b>

                         common /fcb/  v,w,x
                                 character *(13) v, w(4), x(3,2)

<b>C:</b>

typedef struct { char v[13],w[4][13],x[2][3][13]; } FCB_DEF;
#define FCB COMMON_BLOCK(FCB,fcb)
COMMON_BLOCK_DEF(FCB_DEF,FCB);
FCB_DEF FCB;    /* Define, i.e. allocate memory, in exactly one *.c file. */
</pre></font>
e.g. accessing <tt>FCB</tt> in C:
<FONT COLOR="#993300"><pre>
          printf("%.13s",FCB.v);
</pre></font>


<h2> I) Introduction</h2>

<tt>cfortran.h</tt> is an easy-to-use powerful bridge between C and <i>FORTRAN</i>.
It provides a completely transparent, machine independent interface between
C and <i>FORTRAN</i> routines (= subroutines and/or functions) and global data,
i.e. structures and COMMON blocks.
<p>
The complete <tt>cfortran.h</tt> package consists of 4 files: the documentation in
cfortran.doc, the engine <tt>cfortran.h</tt>, examples in <tt>cfortest.c</tt> and 
<tt>cfortex.f</tt>/or. [<tt>cfortex.for</tt> under VMS, 
<tt>cfortex.f</tt> on other machines.]
<p>
The <tt>cfortran.h</tt> package continues to be developed. 
The most recent version is
available via WWW at 
<tt><a href="http://www-zeus.desy.de/~burow/cfortran">http://www-zeus.desy.de/~burow/cfortran</a></tt>.
<p>
The examples may be run using one of the following sets of instructions:
<p>
<b>N.B.</b> Unlike earlier versions, <tt>cfortran.h</tt> 3.0 and later versions
     automatically uses the correct <tt>ANSI ##</tt> or <tt>pre-ANSI /**/</tt>
     preprocessor operator as required by the C compiler.
<p>
<b>N.B.</b> As a general rule when trying to determine how to link C and 
     <i>FORTRAN</i>,
     link a trivial <i>FORTRAN</i> program using the <i>FORTRAN</i> compilers verbose option,
     in order to see how the <i>FORTRAN</i> compiler drives the linker. e.g.
<FONT COLOR="#993300"><pre>
       unix> cat f.f
                END
       unix> f77 -v f.f
       .. lots of info. follows ...
</pre></font>
<p>
<b>N.B.</b> If using a C <tt>main()</tt>, i.e. <i>FORTRAN</i> <tt>PROGRAM</tt>
     is not entry of the executable,
     and if the link bombs with a complaint about
     a missing "<tt>MAIN</tt>" (e.g. <tt>MAIN__</tt>, <tt>MAIN_</tt>, 
     <tt>f90_main</tt> or similar),
     then <i>FORTRAN</i> has hijacked the entry point to the executable
     and wishes to call the rest of the executable via "<tt>MAIN</tt>".
     This can usually be satisfied by doing e.g. '<tt>cc -Dmain=MAIN__ ...</tt>'
     but often kills the command line arguments in <tt>argv</tt> and <tt>argc</tt>.
     The <tt>f77</tt> verbose option, usually <tt>-v</tt>, may point to a solution.
     
<FONT COLOR="#993300"><pre>
RS/6000> # Users are strongly urged to use f77 -qextname and cc -Dextname
RS/6000> # Use -Dextname=extname if extname is a symbol used in the C code.
RS/6000> xlf -c -qextname cfortex.f
RS/6000> cc  -c -Dextname cfortest.c
RS/6000> xlf -o cfortest cfortest.o cfortex.o && cfortest 

DECFortran> #Only DECstations with DECFortran for Ultrix RISC Systems.
DECFortran> cc -c -DDECFortran cfortest.c
DECFortran> f77 -o cfortest cfortest.o cfortex.f  &&  cfortest

IRIX xxxxxx 5.2 02282015 IP20 mips
MIPS> # DECstations and Silicon Graphics using the MIPS compilers.
MIPS> cc -o cfortest cfortest.c cfortex.f -lI77 -lU77 -lF77  &&  cfortest
MIPS> # Can also let f77 drive linking, e.g.
MIPS> cc -c cfortest.c
MIPS> f77 -o cfortest cfortest.o cfortex.f  &&  cfortest

Apollo> # Some 'C compiler 68K Rev6.8' break. <a href="cfortran.html#SIIoApollo">[See Section II o) Notes: Apollo]</a>
Apollo> f77 -c cfortex.f && cc -o cfortest cfortest.c cfortex.o  &&  cfortest

VMS> define lnk$library sys$library:vaxcrtl
VMS> cc cfortest.c
VMS> fortran cfortex.for
VMS> link/exec=cfortest cfortest,cfortex
VMS> run cfortest

OSF1 xxxxxx V3.0 347 alpha
Alpha/OSF> # Probably better to let cc drive linking, e.g.
Alpha/OSF> f77 -c cfortex.f
Alpha/OSF> cc  -o cfortest cfortest.c cfortex.o -lUfor -lfor -lFutil -lots -lm
Alpha/OSF> cfortest
Alpha/OSF> # Else may need 'cc -Dmain=MAIN__' to let f77 drive linking.

Sun> # Some old cc(1) need a little help. <a href="cfortran.html#SIIoSun">[See Section II o) Notes: Sun]</a>
Sun> f77 -o cfortest cfortest.c cfortex.f -lc -lm  &&  cfortest
Sun> # Some older f77 may require 'cc -Dmain=MAIN_'.

CRAY> cft77 cfortex.f
CRAY> cc -c cfortest.c
CRAY> segldr -o cfortest.e cfortest.o cfortex.o
CRAY> ./cfortest.e

NEC> cc -c -Xa cfortest.c
NEC> f77 -o cfortest cfortest.o cfortex.f  &&  cfortest

VAX/Ultrix/cc> # For cc on VAX Ultrix only, do the following once to cfortran.h.
VAX/Ultrix/cc> mv cfortran.h cftmp.h && grep -v "^#pragma" <cftmp.h >cfortran.h
                                            
VAX/Ultrix/f77> # In the following, 'CC' is either 'cc' or 'gcc -ansi'. NOT'vcc'
VAX/Ultrix/f77> CC -c -Dmain=MAIN_ cfortest.c
VAX/Ultrix/f77> f77 -o cfortest cfortex.f cfortest.o  &&  cfortest

LynxOS> # In the following, 'CC' is either 'cc' or 'gcc -ansi'.
LynxOS> # Unfortunately cc is easily overwhelmed by cfortran.h,
LynxOS> #  and won't compile some of the cfortest.c demos.
LynxOS> f2c -R cfortex.f
LynxOS> CC -Dlynx -o cfortest cfortest.c cfortex.c -lf2c  &&  cfortest

HP9000> # Tested with HP-UX 7.05 B 9000/380 and with A.08.07 A 9000/730
HP9000> # CC may be either 'c89 -Aa' or 'cc -Aa'
HP9000> #    Depending on the compiler version, you may need to include the
HP9000> #    option '-tp,/lib/cpp' or worse, you'll have to stick to the K&R C.
HP9000> #    <a href="cfortran.html#SIIoHP9000">[See Section II o) Notes: HP9000]</a>
HP9000> # Users are strongly urged to use f77 +ppu and cc -Dextname
HP9000> # Use -Dextname=extname if extname is a symbol used in the C code.
HP9000> CC  -Dextname -c cfortest.c
HP9000> f77 +ppu         cfortex.f  -o cfortest cfortest.o && cfortest
HP9000> # Older f77 may need
HP9000> f77 -c cfortex.f
HP9000> CC -o cfortest cfortest.c cfortex.o -lI77 -lF77 && cfortest

HP0000> # If old-style f77 +800 compiled objects are required:
HP9000> # #define hpuxFortran800
HP9000> cc -c -Aa -DhpuxFortran800 cfortest.c
HP9000> f77 +800 -o cfortest cfortest.o cfortex.f

f2c> # In the following, 'CC' is any C compiler.
f2c> f2c -R cfortex.f
f2c> CC -o cfortest -Df2cFortran cfortest.c cfortex.c -lf2c  &&  cfortest

Portland Group $ # Presumably other C compilers also work.
Portland Group $ pgcc -DpgiFortran -c cfortest.c
Portland Group $ pgf77 -o cfortest cfortex.f cfortest.o && cfortest

NAGf90> # cfortex.f is distributed with <i>FORTRAN</i> 77 style comments.
NAGf90> # To convert to f90 style comments do the following once to cfortex.f: 
NAGf90> mv cfortex.f cf_temp.f && sed 's/^C/\!/g' cf_temp.f > cfortex.f
NAGf90> # In the following, 'CC' is any C compiler.
NAGf90> CC -c -DNAGf90Fortran cfortest.c
NAGf90> f90 -o cfortest cfortest.o cfortex.f &&  cfortest

PC> # On a PC with PowerStation <i>FORTRAN</i> and Visual_C++
PC> cl /c cftest.c
PC> fl32  cftest.obj cftex.for

GNU> # GNU <i>FORTRAN</i>
GNU> # <a href="cfortran.html#gcctrad">See Section VI caveat on using 'gcc -traditional'</a>.
GNU> gcc -ansi -Wall -O -c -Df2cFortran cfortest.c
GNU> g77 -ff2c -o cfortest cfortest.o cfortex.f &&  cfortest

AbsoftUNIX> # Absoft <i>FORTRAN</i> for all UNIX based operating systems.
AbsoftUNIX> # e.g. Linux or Next on Intel or Motorola68000.
AbsoftUNIX> # Absoft f77 -k allows <i>FORTRAN</i> routines to be safely called from C.
AbsoftUNIX> gcc -ansi -Wall -O -c -DAbsoftUNIXFortran cfortest.c
AbsoftUNIX> f77 -k -o cfortest cfortest.o cfortex.f && cfortest

AbsoftPro> # Absoft Pro <i>FORTRAN</i> for MacOS
AbsoftPro> # Use #define AbsoftProFortran

CLIPPER> # INTERGRAPH CLIX using CLIPPER C and <i>FORTRAN</i> compilers.
CLIPPER> # N.B. - User, not cfortran.h, is responsible for
CLIPPER> #        f77initio() and f77uninitio() if required.
CLIPPER> #      - LOGICAL values are not mentioned in CLIPPER doc.s,
CLIPPER> #        so they may not yet be correct in cfortran.h.
CLIPPER> #      - K&R mode (-knr or Ac=knr) breaks FLOAT functions
CLIPPER> #        (see CLIPPER doc.s) and cfortran.h does not fix it up.
CLIPPER> #        [cfortran.h ok for old sun C which made the same mistake.]
CLIPPER> acc cfortest.c -c -DCLIPPERFortran
CLIPPER> af77 cfortex.f cfortest.o -o cfortest
</pre></font>

By changing the SELECTion <tt>ifdef</tt> of <tt>cfortest.c</tt> and recompiling one can try out
a few dozen different few-line examples.

<p>

The benefits of using <tt>cfortran.h</tt> include:

<ol>
<p><li> Machine/OS/compiler independent mixing of C and <i>FORTRAN</i>.

<p><li> Identical (within syntax) calls across languages, e.g.
<FONT COLOR="#993300"><pre>
<b>FORTRAN:</b>

      CALL HBOOK1(1,'pT spectrum of pi+',100,0.,5.,0.)

<b>C:</b>
      HBOOK1(1,"pT spectrum of pi+",100,0.,5.,0.);
</pre></font>
<p><li> Each routine need only be set up once in its lifetime. e.g.
   Setting up a FORTRAN routine to be called by C.
   ID,...,VMX are merely the names of arguments.
   These tags must be unique w.r.t. each other but are otherwise arbitrary.
<FONT COLOR="#993300"><pre>
PROTOCCALLSFSUB6(HBOOK1,hbook1,INT,STRING,INT,FLOAT,FLOAT,FLOAT)
#define HBOOK1(ID,CHTITLE,NX,XMI,XMA,VMX)                        \
     CCALLSFSUB6(HBOOK1,hbook1,INT,STRING,INT,FLOAT,FLOAT,FLOAT, \
               ID,CHTITLE,NX,XMI,XMA,VMX) 
</pre></font>

<p><li> Source code is NOT required for the C routines exported to <i>FORTRAN</i>, nor for
   the <i>FORTRAN</i> routines imported to C. In fact, routines are most easily
   prototyped using the information in the routines' documentation.

<p><li> Routines, and the code calling them, can be coded naturally in the language
   of choice. C routines may be coded with the natural assumption of being 
   called only by C code. <tt>cfortran.h</tt> does all the required work for <i>FORTRAN</i> 
   code to call C routines. Similarly it also does all the work required for C
   to call <i>FORTRAN</i> routines. Therefore:
   <ul>
     <li> C programmers need not embed <i>FORTRAN</i> argument passing mechanisms into 
       their code.
     <li> <i>FORTRAN</i> code need not be converted into C code. i.e. The honed and 
       time-honored <i>FORTRAN</i> routines are called by C.
   </ul>
<p><li> <tt>cfortran.h</tt> is a single ~1700 line C include file; portable to most
   remaining, if not all, platforms.

<p><li> <tt>STRINGS</tt> and <tt>VECTORS</tt> of 
   <tt>STRINGS</tt> along with the usual simple arguments to 
   routines are supported as are functions returning 
   <tt>STRINGS</tt> or numbers. Arrays
   of pointers to strings and values of structures as C arguments, 
   will soon be
   implemented. 
   After learning the machinery of <tt>cfortran.h</tt>, users can expand 
   it to create custom types of arguments. [This requires no modification to
   <tt>cfortran.h</tt>, all the preprocessor 
   directives required to implement the
   custom types can be defined outside <tt>cfortran.h</tt>]

<p><li> <tt>cfortran.h</tt> requires each routine to be exported to be explicitly set up. 
   While is usually only be done once in a header file it would be best if
   applications were required to do no work at all in order to cross languages.
   <tt>cfortran.h</tt>'s simple syntax could be a convenient back-end for a program
   which would export <i>FORTRAN</i> or C routines directly from the source code. 
</ol>


<h3>Example 1 </h3>
            <tt>cfortran.h</tt> has been used to make the C header file <tt>hbook.h</tt>, 
            which then gives any C programmer, e.g. <tt>example.c</tt>, full and 
            completely transparent access to <b>CERN</b>'s <b>HBOOK</b> library of routines.
            Each <b>HBOOK</b> routine required about 3 lines of simple code in
            <tt>hbook.h</tt>. The example also demonstrates how <i>FORTRAN</i> common blocks
            are defined and used.
<FONT COLOR="#993300"><pre>
/* hbook.h */
#include "cfortran.h"
        :
PROTOCCALLSFSUB6(HBOOK1,hbook1,INT,STRING,INT,FLOAT,FLOAT,FLOAT)
#define HBOOK1(ID,CHTITLE,NX,XMI,XMA,VMX)                        \
     CCALLSFSUB6(HBOOK1,hbook1,INT,STRING,INT,FLOAT,FLOAT,FLOAT, \
               ID,CHTITLE,NX,XMI,XMA,VMX) 
        :
/* end hbook.h */



/* example.c */
#include "hbook.h"
        :
typedef struct {
  int lines;  
  int status[SIZE];
  float p[SIZE];  /* momentum */
} FAKE_DEF;
#define FAKE COMMON_BLOCK(FAKE,fake)
COMMON_BLOCK_DEF(FAKE_DEF,FAKE);
        :
main ()
{
        :
           HBOOK1(1,"pT spectrum of pi+",100,0.,5.,0.);
/* c.f. the call in FORTRAN:
      CALL HBOOK1(1,'pT spectrum of pi+',100,0.,5.,0.)
*/
        :
  FAKE.p[7]=1.0;
	:
}           
</pre></font>

<b>N.B.</b> 
<ol>
   <li> The routine is language independent.
   <li> <tt>hbook.h</tt> is machine independent.  
   <li> Applications using routines via <tt>cfortran.h</tt> are machine independent.
</ol>

<h3>Example 2</h3> Many VMS System calls are most easily called from <i>FORTRAN</i>, but
            <tt>cfortran.h</tt> now gives that ease in C.
<FONT COLOR="#993300"><pre>
#include "cfortran.h"

PROTOCCALLSFSUB3(LIB$SPAWN,lib$spawn,STRING,STRING,STRING)
#define LIB$SPAWN(command,input_file,output_file)          \
     CCALLSFSUB3(LIB$SPAWN,lib$spawn,STRING,STRING,STRING, \
                  command,input_file,output_file)

main ()
{
LIB$SPAWN("set term/width=132","","");
}
</pre></font>
Obviously the <tt>cfortran.h</tt> command above could be put into a header file along
with the description of the other system calls, but as this example shows, it's
not much hassle to set up <tt>cfortran.h</tt> for even a single call.


<h3>Example 3</h3> <tt>cfortran.h</tt> and the source cstring.c create the cstring.obj library 
            which gives <i>FORTRAN</i> access to all the functions in C's system 
            library described by the system's C header file <tt>string.h</tt>.
<FONT COLOR="#993300"><pre>
C     EXAMPLE.FOR
      PROGRAM EXAMPLE
      DIMENSION I(20), J(30)
        :
      CALL MEMCPY(I,J,7)
        :
      END

/* cstring.c */
#include <string.h>             /* string.h prototypes memcpy() */
#include "cfortran.h"

        :
FCALLSCSUB3(memcpy,MEMCPY,memcpy,PVOID,PVOID,INT)
        :
</pre></font>

The simplicity exhibited in the above example exists for many but not all
machines. 
<a href="cfortran.html#IIii4">Note 4. of Section II ii)</a> details the limitations and describes tools
which try to maintain the best possible interface when <i>FORTRAN</i> calls C
routines.


<h2>II) Using cfortran.h</h2>

The user is asked to look at the source files <tt>cfortest.c</tt> and 
<tt>cfortex.f</tt>
for clarification by example.
<p>
 
<h3>o) Notes:</h3> 

<ul>
<p><li> Specifying the <i>FORTRAN</i> compiler
<p>
  <tt>cfortran.h</tt> generates interfaces for the default i
  <i>FORTRAN</i> compiler. The default can be overridden by defining with
  one of the follwoing methods,
  <ul>
  <p><li> in the code,              e.g.: <tt>#define    NAGf90Fortran</tt>
  <p><li> in the compile directive, e.g.: <tt>unix> cc -DNAGf90Fortran</tt>
   </ul>
one of the following before including <tt>cfortran.h</tt>:
<FONT COLOR="#993300"><pre>
 NAGf90Fortran   f2cFortran  hpuxFortran  apolloFortran  sunFortran
  IBMR2Fortran  CRAYFortran  mipsFortran     DECFortran  vmsFortran
 CONVEXFortran       PowerStationFortran          AbsoftUNIXFortran
     SXFortran   pgiFortran                        AbsoftProFortran
</pre></font>
This also allows crosscompilation.
<p>
If wanted, <tt>NAGf90Fortran</tt>, <tt>f2cFortran</tt>, <tt>DECFortran</tt>, <tt>AbsoftUNIXFortran</tt>,
<tt>AbsoftProFortran</tt> and <tt>pgiFortran</tt> must be requested by the user.

<p><li><tt>/**/</tt>
<p>
  <tt>cfortran.h</tt> (ab)uses the comment kludge <tt>/**/</tt> when the 
ANSI C preprocessor
catenation operator <tt>##</tt> doesn't exist. 
In at least MIPS C, this kludge is
sensitive to  blanks surrounding arguments to macros.
  Therefore, for applications using non-ANSI C compilers, the 
<tt>argtype_i</tt>,
<tt>routine_name</tt>, 
<tt>routine_type</tt> 
and 
<tt>common_block_name arguments</tt> to the
<tt>PROTOCCALLSFFUNn</tt>, <tt>CCALLSFSUB/FUNn</tt>, <tt>FCALLSCSUB/FUNn</tt> and <tt>COMMON_BLOCK</tt> macros 
<b> must not</b> be followed by any white space characters such as
blanks, tabs or newlines.

<p><li> <tt>LOGICAL</tt>
<p>
  <i>FORTRAN</i> <tt>LOGICAL</tt> values of .TRUE. and .FALSE. do not agree with the C
representation of TRUE and FALSE on all machines. <tt>cfortran.h</tt> does the
conversion for <tt>LOGICAL</tt> and PLOGICAL arguments and for functions returning
<tt>LOGICAL</tt>. Users must convert arrays of <tt>LOGICAL</tt>s from C to <i>FORTRAN</i> with the 
C2FLOGICALV(array_name, elements_in_array); macro. Similarly, arrays of <tt>LOGICAL</tt>
values may be converted from the <i>FORTRAN</i> into C representation by using
F2CLOGICALV(array_name, elements_in_array);
<p>
  When C passes or returns <tt>LOGICAL</tt> values to <i>FORTRAN</i>, by default <tt>cfortran.h</tt> 
only makes the minimal changes required to the value. [e.g. Set/Unset the 
single relevant bit or do nothing for <i>FORTRAN</i> compilers which use 0 as FALSE
and treat all other values as TRUE.] Therefore <tt>cfortran.h</tt> will pass <tt>LOGICAL</tt>s
to <i>FORTRAN</i> which do not have an identical representation to .TRUE. or .FALSE.
This is fine except for abuses of <i>FORTRAN</i>/77 in the style of:
<FONT COLOR="#993300"><pre>
       logical l
       if (l .eq. .TRUE.)     ! (1)
</pre></font>
instead of the correct:
<FONT COLOR="#993300"><pre>
       if (l .eqv. .TRUE.)    ! (2)
</pre></font>
or:
<FONT COLOR="#993300"><pre>
       if (l)                 ! (3)
</pre></font>
For <i>FORTRAN</i> code which treats <tt>LOGICAL</tt>s from C in the method of (1),
<tt>LOGICAL_STRICT</tt> must be defined before 
including <tt>cfortran.h</tt>, either in the
code, <tt>"#define LOGICAL_STRICT"</tt>, or compile with 
<tt>"cc -DLOGICAL_STRICT"</tt>.
There is no reason to use <tt>LOGICAL_STRICT</tt> for <i>FORTRAN</i> 
code which does not do (1).
At least the IBM's <tt>xlf</tt> and the Apollo's <tt>f77</tt>
 do not even allow code along the
lines of (1).
<p>
  DECstations' <tt>DECFortran</tt> and MIPS <i>FORTRAN</i> compilers use 
different internal
representations for <tt>LOGICAL</tt> values. 
[Both compilers are usually called <tt>f77</tt>,
although when both are installed on a single machine the MIPS' one is usually
renamed. (e.g. <tt>f77</tt>2.1 for version 2.10.)] <tt>cc</tt> doesn't know 
which <i>FORTRAN</i>
compiler is present, so <tt>cfortran.h</tt> assumes MIPS <tt>f77</tt>. 
To use <tt>cc</tt> with DECFortran
define the preprocessor constant 'DECFortran'.
e.g.
<FONT COLOR="#993300"><pre>
            <b>i) </b> cc -DDECFortran -c the_code.c
</pre></font>
or
<FONT COLOR="#993300"><pre>
            <b>ii)</b> #define DECFortran  /* in the C code or add to <tt>cfortran.h</tt>. */
</pre></font>

  MIPS <tt>f77</tt> [SGI and DECstations], <tt>f2c</tt>, and <tt>f77</tt> on 
VAX Ultrix treat
<tt>.eqv./.neqv.</tt> as <tt>.eq./.ne.</tt>. Therefore, 
for these compilers, <tt>LOGICAL_STRICT</tt> is
defined by default in <tt>cfortran.h</tt>. 
[The Sun and HP compilers have not been
tested, so they may also require <tt>LOGICAL_STRICT</tt> as the default.]

<p><li> <tt>SHORT</tt> and <tt>BYTE</tt>
<p> 
  They are irrelevant for the CRAY where <i>FORTRAN</i> 
has no equivalent to C's <tt>short</tt>.
Similarly <tt>BYTE</tt> is irrelevant for <tt>f2c</tt> and for VAX Ultrix <tt>f77</tt> and fort. The
author has tested SHORT and BYTE with a modified cfortest.c/cfortex.f on all
machines supported except for the HP9000 and the Sun.
<p>
  <tt>BYTE</tt> is a signed 8-bit quantity, i.e. values are -128 to 127, 
on all machines
except for the SGI [at least for MIPS Computer Systems 2.0.] On the SGI it is
an unsigned 8-bit quantity, i.e. values are 0 to 255, although the SGI '<i>FORTRAN</i>
77 Programmers Guide' claims BYTE is signed. Perhaps MIPS 2.0 is dated, since
the DECstations using MIPS 2.10 <tt>f77</tt> have a signed <tt>BYTE</tt>.
<p>
  To minimize the difficulties of signed and unsigned 
<tt>BYTE</tt>, <tt>cfortran.h</tt> creates
the type '<tt>INTEGER_BYTE</tt>' to agree with <i>FORTRAN</i>'s 
<tt>BYTE</tt>. Users may define 
<tt>SIGNED_BYTE</tt> or 
<tt>UNSIGNED_BYTE</tt>, before including <tt>cfortran.h</tt>, 
to specify <i>FORTRAN</i>'s
<tt>BYTE</tt>. If neither is defined, <tt>cfortran.h</tt> assumes 
<tt>SIGNED_BYTE</tt>.

<p><li> CRAY
<p>
  The type <tt>DOUBLE</tt> in <tt>cfortran.h</tt> 
  corresponds to <i>FORTRAN</i>'s <tt>DOUBLE PRECISION</tt>.
  The type <tt>FLOAT</tt> in <tt>cfortran.h</tt> corresponds to 
  <i>FORTRAN</i>'s <tt>REAL</tt>.
<p>
On a classic CRAY [i.e. all models except for the t3e]:
<FONT COLOR="#993300"><pre>
( 64 bit) C float       == C double == <i>FORTRAN</i> REAL
(128 bit) C long double             == <i>FORTRAN</i> DOUBLE PRECISION
</pre></font>
Therefore when moving a mixed C and <i>FORTRAN</i> app. to/from a classic CRAY,
either the C code will have to change,
or the <i>FORTRAN</i> code and <tt>cfortran.h</tt> declarations will have to 
change.
<tt>DOUBLE_PRECISION</tt> is a <tt>cfortran.h</tt> macro which provides the former option,
i.e. the C code is automatically changed.
<tt>DOUBLE_PRECISION</tt> is 'long double' on classic CRAY and 'double' elsewhere.
<tt>DOUBLE_PRECISION</tt> thus corresponds to <i>FORTRAN</i>'s <tt>DOUBLE PRECISION</tt>
on all machines, including classic CRAY.
<p>
On a classic CRAY with the <i>FORTRAN</i> compiler flag <tt>'-dp'</tt>:
<i>FORTRAN</i> <tt>DOUBLE PRECISION</tt> thus is also the faster 64bit type.
(This switch is often used since the application is usually satisfied by
 64 bit precision and the application needs the speed.)
<tt>DOUBLE_PRECISION</tt> is thus not required in this case,
since the classic CRAY behaves like all other machines.
If <tt>DOUBLE_PRECISION</tt> is used nonetheless, then on the classic CRAY
the default <tt>cfortran.h</tt> behavior must be overridden,
for example by the C compiler option <tt>'-DDOUBLE_PRECISION=double'</tt>.
<p>
On a CRAY t3e:
<FONT COLOR="#993300"><pre>
(32 bit) C float                   == <i>FORTRAN</i> Unavailable
(64 bit) C double == C long double == <i>FORTRAN</i> REAL == <i>FORTRAN</i> DOUBLE PRECISION
</pre></font>
Notes:
<ul>
<p><li> (32 bit) is available as <i>FORTRAN</i> <tt>REAL*4</tt> and
  (64 bit) is available as <i>FORTRAN</i> <tt>REAL*8</tt>.
  Since <tt>cfortran.h</tt> is all about more portability, not about less portability,
  the use of the nonstandard <tt>REAL*4</tt> and <tt>REAL*8</tt> is strongly discouraged.
<p><li> <i>FORTRAN</i> <tt>DOUBLE PRECISION</tt> is folded to <tt>REAL</tt> with 
   the following warning:
<FONT COLOR="#993300"><pre>
    DOUBLE PRECISION is not supported on this platform.  REAL will be used.
</pre></font>
  Similarly, <i>FORTRAN</i> <tt>REAL*16</tt> is mapped to <tt>REAL*8</tt> with a warning.
This behavior differs from that of other machines, including the classic CRAY.
<tt>FORTRAN_REAL</tt> is thus introduced for the t3e,
just as <tt>DOUBLE_PRECISION</tt> is introduced for the classic CRAY.
<tt>FORTRAN_REAL</tt> is '<tt>double</tt>' on t3e and '<tt>float</tt>' elsewhere.
<tt>FORTRAN_REAL</tt> thus corresponds to <i>FORTRAN</i>'s <tt>REAL</tt> 
on all machines, including t3e.
</ul>

<p><li> <tt>f2c</tt>
<p>
  <tt>f2c</tt>, by default promotes <tt>REAL</tt> functions to double. 
<tt>cfortran.h</tt> does not (yet)
support this, so the <tt>f2c -R</tt> option must be used to turn this promotion off.

<p><li> <tt>f2c</tt>
<p>[Thanks to Dario Autiero for pointing out the following.]
<tt>f2c</tt> has a strange feature in that either one or two underscores are appended
to a <i>FORTRAN</i> name of a routine or common block,
depending on whether or not the original name contains an underscore.
<b><pre>
   S.I. Feldman et al., "A <i>FORTRAN</i> to C converter",
   Computing Science Technical Report No. 149.

   page 2, chapter 2: INTERLANGUAGE conventions
   ...........
</pre></b>
   To avoid conflict with the names of library routines and with names that
   <tt>f2c</tt> generates,
   <i>FORTRAN</i> names may have one or two underscores appended. 
   <i>FORTRAN</i> names are
   forced to lower case (unless the -U option described in Appendix B is in
   effect); external names, i.e. the names of <i>FORTRAN</i> procedures 
   and common
   blocks, have a single underscore appended if they do not contain any
   underscore and have a pair of underscores appended if they do contain
   underscores. Thus <i>FORTRAN</i> subroutines names <tt>ABC</tt>, 
   <tt>A_B_C</tt> and <tt>A_B_C_</tt> result
   in C functions named <tt>abc</tt>_, <tt>a_b_c__</tt> and <tt>a_b_c___</tt>.
<p>

<tt>cfortran.h</tt> is unable to change the naming convention on a name by name basis.
<i>FORTRAN</i> routine and common block names which do not contain an underscore
are unaffected by this feature.
Names which do contain an underscore may use the following work-around:

<FONT COLOR="#993300"><pre>
/* First 2 lines are a completely standard <tt>cfortran.h</tt> interface
   to the <i>FORTRAN</i> routine E_ASY . */
                  PROTOCCALLSFSUB2(E_ASY,e_asy, PINT, INT)
#define E_ASY(A,B)     CCALLSFSUB2(E_ASY,e_asy, PINT, INT, A, B)
#ifdef f2cFortran
#define e_asy_ e_asy__
#endif
/* Last three lines are a work-around for the strange f2c naming feature. */
</pre></font>

<p><li> NAG f90
<p>  The <i>FORTRAN</i> 77 subset of <i>FORTRAN</i> 90 is supported. 
Extending <tt>cfortran.h</tt> to 
interface C with all of <i>FORTRAN</i> 90 has not yet been examined.
<br>  The NAG f90 library hij acks the <tt>main()</tt> of any program and starts the user's 
program with a call to: <tt>void f90_main(void)</tt>;<br>
While this in itself is only a minor hassle, a major problem arises because
NAG f90 provides no mechanism to access command line arguments.<br>
  At least version 'NAGWare f90 compiler Version 1.1(334)' appended _CB to
common block names instead of the usual <tt>_</tt>. 
To fix, add this to <tt>cfortran.h</tt>:
<FONT COLOR="#993300"><pre>
#ifdef old_NAG_f90_CB_COMMON
#define COMMON_BLOCK                 CFC_  /* for all other Fortran compilers */
#else
#define COMMON_BLOCK(UN,LN)          _(LN,_CB)
#endif
</pre></font>

<p><li> RS/6000
<p>  Using <tt>"xlf -qextname ..."</tt>, which appends an underscore, <tt>'_'</tt>, 
to all <i>FORTRAN</i>
external references, requires <tt>"cc -Dextname ..."</tt> so that 
<tt>cfortran.h</tt> also
generates these underscores.
Use i<tt>-Dextname=extname</tt> if <tt>extname</tt> is a symbol used in 
the C code.
The use of <tt>"xlf -qextname"</tt> is <b>strongly encouraged</b>, since it 
allows for
transparent naming schemes when mixing C and <i>FORTRAN</i>.

<p><li> <a name="SIIoHP9000">HP9000</a>
<p>  Using <tt>"f77 +ppu ..."</tt>, which appends an underscore, 
<tt>'_'</tt>, to all <i>FORTRAN</i>
external references, requires <tt>"cc -Dextname ..."</tt> so 
that <tt>cfortran.h</tt> also
generates these underscores.
Use <tt>-Dextname=extname</tt> if extname is a symbol used in the C code.
The use of <tt>"f77 +ppu"</tt> is <b>strongly encouraged</b>, since it allows 
for
transparent naming schemes when mixing C and <i>FORTRAN</i>.
<p>
  At least one release of the HP <tt>/lib/cpp.ansi</tt>
 preprocessor is broken and will
go into an infinite loop when trying to process <tt>cfortran.h</tt> with the
<tt>##</tt> catenation operator. The K&R version of <tt>cfortran.h</tt> must then be used and the
K&R preprocessor must be specified. e.g.
<FONT COLOR="#993300"><pre>
HP9000> cc -Aa -tp,/lib/cpp -c source.c
</pre></font>
The same problem with a similar solution exists on the Apollo.
An irrelevant error message <tt>'0: extraneous name /usr/include'</tt>
 will appear for
each source file due to another HP bug, and can be safely ignored.
e.g. 
<FONT COLOR="#993300"><pre>
cc -v -c -Aa -tp,/lib/cpp cfortest.c
</pre></font>

 will show that the driver passes
<tt>'-I /usr/include'</tt> instead of <tt>'-I/usr/include'</tt> to 
<tt>/lib/cpp</tt>
<p>
On some machines the above error causes compilation to stop; one must then use
K&R C, as with old HP compilers which don't support function prototyping.
<tt>cfortran.h</tt> has to be informed that K&R C is to being used, e.g.
<FONT COLOR="#993300"><pre>
HP9000> cc -D__CF__KnR -c source.c
</pre></font>

<p><li> AbsoftUNIXFortran
<p>
By default, <tt>cfortran.h</tt> follows the default AbsoftUNIX/ProFortran 
and prepends <tt>_C</tt>
to each common block name. To override the <tt>cfortran.h</tt> behavior
<tt>#define COMMON_BLOCK(UN,LN)</tt> before including <tt>cfortran.h</tt>.
[Search for <tt>COMMON_BLOCK</tt> in <tt>cfortran.h</tt> for examples.]

<p><li> <a name="SIIoApollo">Apollo</a>
<p>
On at least one release, 'C compiler 68K Rev6.8(168)', the default C 
preprocessor, from cc -A xansi or cc -A ansi, enters an infinite loop when 
using <tt>cfortran.h</tt>. This Apollo bug can be circumvented by using:
<ul>
<p><li> <tt>cc -DANSI_C_preprocessor=0</tt> to force use of 
           <tt>/**/</tt>, instead of <tt>'##'</tt>.
<p><b>AND</b> 
<p><li> The pre-ANSI preprocessor, i.e. use <tt>cc -Yp,/usr/lib</tt>
</ul>
<p>The same problem with a similar solution exists on the HP.

<p><li> <a name="SIIoSun">Sun</a>
<p>Old versions of cc(1), say <~1986, may require help for <tt>cfortran.h</tt> 
applications:
<ul>
 <p><li> <tt>#pragma</tt> may not be understood, hence <tt>cfortran.h</tt> 
   and <tt>cfortest.c</tt> may require
<FONT COLOR="#993300"><pre>
sun> mv <tt>cfortran.h</tt> cftmp.h && grep -v "^#pragma" <cftmp.h >cfortran.h
sun> mv cfortest.c cftmp.c && grep -v "^#pragma" <cftmp.c >cfortest.c
</pre></font>
 <p><li> Old copies of <tt>math.h</tt> may not include the following from a newer <tt>math.h</tt>.
   [For an ancient <tt>math.h</tt> on a 386 or sparc, get similar from a new <tt>math.h</tt>.]
</ul>
<FONT COLOR="#993300"><pre>
   #ifdef mc68000     /* 5 lines Copyright (c) 1988 by Sun Microsystems, Inc. */
   #define FLOATFUNCTIONTYPE	int
   #define RETURNFLOAT(x) 		return (*(int *)(&(x)))
   #define ASSIGNFLOAT(x,y)	*(int *)(&x) = y
   #endif
</pre></font>

<p><li> CRAY, Sun, Apollo [pre 6.8 cc], VAX Ultrix and HP9000
<p>
  Only <i>FORTRAN</i> routines with less than 15 arguments can be prototyped for C,
since these compilers don't allow more than 31 arguments to a C macro. This can
be overcome, [see Section IV], with access to any C compiler without this
limitation, e.g. gcc, on ANY machine.

<p><li> VAX Ultrix
<p> <tt>vcc (1)</tt> with <tt>f77</tt> is not supported. Although: 
<FONT COLOR="#993300"><pre>
VAXUltrix> f77 -c cfortex.f
VAXUltrix> vcc -o cfortest cfortest.c cfortex.o -lI77 -lU77 -lF77  &&  cfortest
</pre></font>
will link and run. However, the <i>FORTRAN</i> standard I/O is NOT merged 
with the
<tt>stdin</tt> and <tt>stdout</tt> of C, and instead uses the files 
<tt>fort.6</tt> and <tt>fort.5</tt>. For <tt>vcc</tt>,
<tt>f77</tt> can't drive the linking, as for <tt>gcc</tt> and <tt>cc</tt>, 
since <tt>vcc</tt> objects must be
linked using <tt>lk (1)</tt>.  <tt>f77 -v</tt> 
doesn't tell much, and without VAX Ultrix manuals,
the author can only wait for the info. required.
<p>
  <tt>fort (1)</tt> is not supported. Without VAX Ultrix manuals the author 
cannot 
convince <tt>vcc/gcc/cc</tt> and <tt>fort</tt> to generate names of 
routines and common blocks
that match at the linker, <tt>lk (1)</tt>. i.e. <tt>vcc/gcc/cc</tt>
 prepend a single underscore
to external references, e.g. <tt>NAME</tt> becomes 
<tt>_NAME</tt>, while fort does not modify the
references. So ... either fort has prepend an underscore to external
references, or <tt>vcc/gcc/cc</tt> have to generate unmodified names. 
<tt>man 1 fort</tt>
mentions <tt>JBL</tt>, is JBL the only way?

<p><li> VAX VMS C
<p>  The compiler 'easily' exhausts its table space and generates:
<FONT COLOR="#993300"><pre>
%CC-F-BUGCHECK, Compiler bug check during parser phase    .
                Submit an SPR with a problem description.
                At line number 777 in DISK:[DIR]FILE.C;1.
</pre></font>
where the line given, '777', includes a call across C and <i>FORTRAN</i> via
<tt>cfortran.h</tt>, usually with >7 arguments and/or very long argument 
expressions.<p>
This SPR can be staved off, with the simple modification to <tt>cfortran.h</tt>, such
that the relevant <tt>CCALLSFSUBn</tt> (or <tt>CCALLSFFUNn</tt> or 
<tt>FCALLSCFUNn</tt>) is not
cascaded up to <tt>CCALLSFSUB14</tt>, and instead has its own copy 
of the contents of 
<tt>CCALLSFSUB14</tt>. 
[If these instructions are not obvious after examining <tt>cfortran.h</tt>
please contact the author.]<br>
[Thanks go to Mark Kyprianou (kyp@stsci.edu) for this solution.]

<p><li> Mips compilers
<p>
  e.g. DECstations and SGI, require applications with a C main() and calls to
GETARG(3F), i.e. <i>FORTRAN</i> routines returning the command line arguments, to use
two macros as shown:
<FONT COLOR="#993300"><pre>
        :
CF_DECLARE_GETARG;              /* This must be external to all routines.     */
        :
main(int argc, char *argv[])
{
        :
CF_SET_GETARG(argc,argv);       /* This must precede any calls to GETARG(3F). */
        :
}
</pre></font>
The macros are null and benign on all other systems. Sun's <tt>GETARG(3F)</tt>
 also
doesn't work with a generic C <tt>main()</tt>
 and perhaps a workaround similar to the
Mips' one exists.

<p><li> Alpha/OSF
<p>Using the DEC <i>FORTRAN</i> and the DEC C compilers of 
DEC OSF/1 [RT] V1.2 (Rev. 10),
<i>FORTRAN</i>, when called from C, has occasional trouble using a routine received as
a dummy argument.

e.g. In the following the <i>FORTRAN</i> routine 'e' will crash when it tries to use
     the C routine 'c' or the <i>FORTRAN</i> routine 'f'.
     The example works on other systems.

<FONT COLOR="#993300"><pre>
C FORTRAN                           /* C */
      integer function f()          #include <stdio.h>
      f = 2                         int f_();
      return                        int e_(int (*u)());
      end
                                    int c(){ return 1;}
      integer function e(u)         int d (int (*u)()) { return u();}
      integer u
      external u                    main()
      e=u()                         {         /* Calls to d  work.  */
      return                        printf("d (c ) returns %d.\n",d (c ));
      end                           printf("d (f_) returns %d.\n",d (f_));
                                              /* Calls to e_ crash. */
                                    printf("e_(c ) returns %d.\n",e_(c ));
                                    printf("e_(f_) returns %d.\n",e_(f_));
                                    }
</pre></font>

Solutions to the problem are welcomed!
A kludge which allows the above example to work correctly, requires an extra
argument to be given when calling the dummy argument function.
i.e. Replacing <tt>'e=u()'</tt> by <tt>'e=u(1)'</tt>
 allows the above example to work.


<p><li> The <i>FORTRAN</i> routines are called using macro expansions, therefore the usual
caveats for expressions in arguments apply. The expressions to the routines may
be evaluated more than once, leading to lower performance and in the worst case
bizarre bugs.

<p><li> For those who wish to use <tt>cfortran.h</tt> in large applications. 
<a href="cfortran.html#IV">[See Section IV.]</a>
This release is intended to make it easy to get applications up and running. 
This implies that applications are not as efficient as they could be:
<ul>
<p><li> The current mechanism is inefficient if a single header file is used to
  describe a large library of <i>FORTRAN</i> functions. Code for a static wrapper fn.
  is generated in each piece of C source code for each <i>FORTRAN</i> function 
  specified with the <tt>CCALLSFFUNn</tt> statement, irrespective of whether or not the
  function is ever called. 
<p><li> Code for several static utility routines internal to <tt>cfortran.h</tt> is placed 
  into any source code which <tt>#includes cfortran.h</tt>. These routines should
  probably be in a library.
</ul>
</ul>

<h3>i) Calling <i>FORTRAN</i> routines from C:</h3>

The <i>FORTRAN</i> routines are defined by one of the following two instructions:
<p>
for a SUBROUTINE:
<FONT COLOR="#993300"><pre>
/* PROTOCCALLSFSUBn is optional for C, but mandatory for C++. */
PROTOCCALLSFSUBn(ROUTINE_NAME,routine_name,argtype_1,...,argtype_n)
#define     Routine_name(argname_1,..,argname_n)               \
CCALLSFSUBn(ROUTINE_NAME,routine_name,argtype_1,...,argtype_n, \
                         argname_1,..,argname_n) 
</pre></font>

for a FUNCTION:
<FONT COLOR="#993300"><pre>
PROTOCCALLSFFUNn(routine_type,ROUTINE_NAME,routine_name,argtype_1,...,argtype_n)
#define     Routine_name(argname_1,..,argname_n)               \
CCALLSFFUNn(ROUTINE_NAME,routine_name,argtype_1,...,argtype_n, \
                         argname_1,..,argname_n) 
</pre></font>
Where:
<FONT COLOR="#993300"><pre>
'n' = 0->14 [SUBROUTINE's ->27] (easily expanded in <tt>cfortran.h</tt> to > 14 [27]) is 
    the number of arguments to the routine.
Routine_name = C       name of the routine (IN UPPER CASE LETTERS).[see 2.below]
ROUTINE_NAME = <i>FORTRAN</i> name of the routine (IN UPPER CASE LETTERS).
routine_name = <i>FORTRAN</i> name of the routine (IN lower case LETTERS).
routine_type = the type of argument returned by <i>FORTRAN</i> functions.
             = BYTE, DOUBLE, FLOAT, INT, LOGICAL, LONG, SHORT, STRING, VOID.
               [Instead of VOID one would usually use CCALLSFSUBn.
                VOID forces a wrapper function to be used.]
argtype_i    = the type of argument passed to the <i>FORTRAN</i> routine and must be
               consistent in the definition and prototyping of the routine s.a.
             = BYTE, DOUBLE, FLOAT, INT, LOGICAL, LONG, SHORT, STRING.
             For vectors, i.e. 1 dim. arrays use 
             = BYTEV, DOUBLEV, FLOATV, INTV, LOGICALV, LONGV, SHORTV, 
               STRINGV, ZTRINGV.
             For vectors of vectors, i.e. 2 dim. arrays use
             = BYTEVV, DOUBLEVV, FLOATVV, INTVV, LOGICALVV, LONGVV, SHORTVV.
             For n-dim. arrays, 1<=n<=7 [7 is the maximum in <i>FORTRAN</i> 77],
             = BYTEV..nV's..V, DOUBLEV..V, FLOATV..V, INTV..V, LOGICALV..V, 
               LONGV..V, SHORTV..V.
                N.B. Array dimensions and types are checked by the C compiler.
             For routines changing the values of an argument, the keyword is 
                  prepended by a 'P'.
             = PBYTE, PDOUBLE, PFLOAT, PINT, PLOGICAL, PLONG, PSHORT,
               PSTRING, PSTRINGV, PZTRINGV.
             For EXTERNAL procedures passed as arguments use
             = ROUTINE.
             For exceptional arguments which require no massaging to fit the
                  argument passing mechanisms use
             = PVOID.
                The argument is cast and passed as (void *).
                Although PVOID could be used to describe all array arguments on
                most (all?) machines , it shouldn't be because the C compiler
                can no longer check the type and dimension of the array.
argname_i    = any valid unique C tag, but must be consistent in the definition 
               as shown.
For faster browsing, not all history is shown. View entire blame