linking C and Fortran

Daniel R. Levy levy at ttrdc.UUCP
Sun Aug 3 07:44:32 AEST 1986


In article <2668 at brl-smoke.ARPA>, moss at BRL.ARPA (Gary S. Moss (SLCBR-VLD-V)) writes:
>Bill,
>	CC prepends an underscore to the function name, and F77 appends
>an underscore.  So, to call Fortran from C, name the function _forsub in
>the Fortran module, invoke it as forsub_ in the C module.  You may have
>to shorten the name to allow for the extra characters, depending on your
>linker.  Conversely, define it as forsub_ in the C module, and invoke it
>as _forsub in the Fortran module.  Be sure to define/invoke the function
>from C with pointers (call by reference) exclusively.
>-moss.

Close but no cigar.  Fortran (at least f77 which mostly follows the ANSI
standard) doesn't grok '_' in identifiers and even if it did (a la VMS FORTRAN)
the above explanation has an error in it.  The actual situation might look
like this:

      subroutine forsub(arg1, arg2,...)
      ....
      return
      end

main(argc,argv);
int argc;
char **argv;
{
	void forsub_();
	void f_init();
	void f_exit();
	void exit();
	...	/* other declarations */
	f_init();	/* a MUST for many UNIX f77's if the subroutines */
			/* do I/O; there have been many net inquiries about */
			/* this from time to time */
	....
	forsub_(&arg1, &arg2,...);
	...
	f_exit();	/* flushes, truncates, and closes opened files */
			/* among other things; should be done if fortran */
			/* I/O was done in the program */
	exit(0);
}

Anyone who has been having trouble interfacing C and f77 can send further
questions my way.  I am not an expert nor am I involved with f77 or C design
but I have had quite a bit of experience with this kind of interface under
several different versions of UNIX.

UNIX is a registered trademark of AT&T Bell Laboratories.
-- 
 -------------------------------    Disclaimer:  The views contained herein are
|       dan levy | yvel nad      |  my own and are not at all those of my em-
|         an engihacker @        |  ployer or the administrator of any computer
| at&t computer systems division |  upon which I may hack.
|        skokie, illinois        |
 --------------------------------   Path: ..!{akgua,homxb,ihnp4,ltuxa,mvuxa,
	   go for it!  			allegra,ulysses,vax135}!ttrdc!levy



More information about the Comp.unix.wizards mailing list