fortc(1)
π fortc(1) β the UNIDATA UTILITIES era π
okay bestie, gather round, because we're about to talk about fortc from the UNIDATA UTILITIES manual, and honestly? it's giving β¨time-travelβ¨ but it's also genuinely useful, no cap.
πΈ NAME πΈ
fortc β a Unidata utility that generates fortran-compatible C code. yes literally, it takes your C and makes it FORTRAN-friendly. iconic behavior fr fr π«Ά
β¨ SYNOPSIS β¨
fortc [-L LibDir] [-O OpSys] file
π¦ DESCRIPTION π¦
so fortc reads C code from file, transforms it into FORTRAN compatible code, and then writes the result straight to standard output. that's it, that's the whole vibe.
the transformation is operating-system dependent, because β and I cannot stress this enough β there are no standards in this matter. none. zero. the wild west, honestly π
π how do we pick the target OS tho?
the target operating-system can be specified in several ways, in this order:
- if the
-Ooption is given, then the operating-system specified by its argument is used; - otherwise, if the environmental variable
OSexists, then the operating-system specified by it's value is used; - otherwise, the default operating-system β determined at installation time β is used.
periodt. that's the priority chain. π
π
global names, aka the FC_NAME() moment
Global names that are not function definitions but that are to be accessible from both C and FORTRAN should be enclosed by the FC_NAME() macro:
extern int FC_NAME(foo)(void); /* FORTRAN function */
extern struct {
float f;
int i;
} FC_NAME(comblk); /* FORTRAN common block */
cfoo()
{
return FC_NAME(foo)(); /* call FORTRAN function */
}
see how clean that is? the way it just works for both languages at once π§
π C functions that FORTRAN is gonna call
C functions which are to be called from FORTRAN are written as follows:
int
/*FORTRAN*/
foo(
character pathname, /* file name of new file */
real amount, /* amount to frobdicate by */
doubleprecision dbl, /* amount to tizzle by */
void function errf, /* error routine */
integer rcode /* returned error code */)
{
...
or, equivalently,
int
/*FORTRAN*/
foo(char* pathname, float* amount, double* dbl,
void (*errf)(), int* rcode)
{
...
same energy, different spelling, both slay π
β the points to note (do not skip these, bestie)
The points to note in writing FORTRAN-compatible functions are:
- Functions to be transformed are marked by a proceeding
/*FORTRAN*/comment, which must appear on the line prior to the function name. Any return-type declarations must proceed the comment. - The formal parameters must be declared within function parentheses and be separated by commas.
- The formal parameters are restricted to the following types:
characterorchar*β For Fortran character-variables.doubleprecisionordouble*"β For double-precision floating-point values.realorfloat*β For single-precision floating-point values.integerorint*β For integer values.type function nameortype (*name)()β For functions returning typetype.
- There must be nothing important after the opening brace,
{, and on the same line.
π«Ά how to actually write the function body
The functions should be written using variables with the same names as the formal parameters, and with the same semantics. In the case of formal parameters of type character (i.e. Fortran character-variables), a variable with the same name will exist and will be of type char*. In addition, for each character-variable argument, a local variable of integral type will be created and its value will be the size of the associated character-variable. The name of this variable will be the name of the associated character variable with _len appended (e.g. pathname_len). These length variables may be used in the function just like any other variable.
π example time: SunOS
For example, under SunOS the previous function would become (if it is contained in the file foo.src):
#line 1 "foo.src"
unsigned int
#line 2 "foo.src"
foo_(pathname, amount, errf, rcode, pathname_len)
char *pathname;
int pathname_len;
float *amount;
void (*errf)();
int *rcode;
{
#line 9 "foo.src"
Note that 1) preprocessor line directives have been added; 2) the function name has been suffixed with an underscore; and 3) a formal parameter, pathname_len, has been created. three little glow-ups, look at her go β¨
π₯ example time: UNICOS
Under UNICOS, the same file would become
#include <fortran.h>
#line 1 "foo.src"
unsigned int
#line 2 "foo.src"
FOO(pathnamed, amount, errf, rcode)
_fcd pathnamed;
float *amount;
void (*errf)();
int *rcode;
{
char *pathname = _fcdtocp (pathnamed);
unsigned pathname_len = _fcdlen (pathnamed);
#line 9 "foo.src"
Note, in particular, the additional #include <fortran.h> and the creation and initialization of the local variables pathname and pathname_len. it's giving extra credit and I'm obsessed ππ
π§ one more lil fact
This utility is implemented as a script and uses the system-supplied utilities sed(1) and m4(1). yeah, it's a shell script wearing a fancy hat. respect. π
π OPTIONS π
-L LibDirβ Defines the directory containing thefortclibrary files. The default is determined at installation time.-O OpSysβ Defines the operating-system as OpSys. Known values includeaix,convexos,domainos,hpux,irix,msoft,next-absoft,newsos,sunos,ultrix,unicos, andvms. To allow for greater flexibility, the known values may have any suffix (e.g.sunos_4.1). suffix freedom, we love to see it π
πΈ ENVIRONMENT πΈ
OSβ Defines the operating system if the-Ooption is not specified. Known values are the same as the-Ooption.
π FILES π
In the following, LIBDIR is the default library directory determined at installation time.
LIBDIR/pre.sedβ A pre-m4,sed(1)script for transforming the input source intom4(1)source.LIBDIR/post.sedβ A post-m4,sed(1)script for transforming them4(1)output into C code.LIBDIR/common.m4β Anm4(1)source common to all Fortran-calling-C implementations.LIBDIR/*.m4β Operating-system-specificm4(1)sources.
π BUGS π
The names of automatically generated variables might conflict with those of other, pre-existing variables.
okay so likeβ¦ watch out for name collisions, bestie. the auto-generated variable names are not out here doing vibe checks before they move in. it's a real one. π
π¦ SEE ALSO π¦
and that's the whole page, bestie β you ate this, fr fr. πβ¨ππ¦πΈπ π«Άππ₯ππβππππ§π
this was fortc(1) from the UNIDATA UTILITIES manual β printed 126-10-12, dated $Date: 2000/08/07 23:15:03 $, and filed under fortc(1) like the icon she is. mwah. π
π¦ where this page comes from
- debian trixie: libxgks-dev 2.6.1+dfsg.2-17
- ubuntu noble: libxgks-dev 2.6.1+dfsg.2-15
- ubuntu questing: libxgks-dev 2.6.1+dfsg.2-19ubuntu1
- ubuntu resolute: libxgks-dev 2.6.1+dfsg.2-19ubuntu2
- ubuntu stonking: libxgks-dev 2.6.1+dfsg.2-19ubuntu2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms