gfortran with OpenCOBOL

Easily link to gfortran FORTRAN libraries with OpenCOBOL …

Well met,

As OpenCOBOL generates C suitable for gcc, and gfortran is a GNU Compiler Collection member, integration of these two venerable languages is pretty straight forward in a GNU/Linux environment. Too easy.

Within a few minutes, and a quick scan of some Fortran code repositories for something meaty enough to be suitable. I found one at Fortran repo, RGB HSA CMY color spec conversion and tried it. gfortran compiled it happily. I saved the linked source code as colors.for

This OpenCOBOL then interfaced to the functions with CALL and the float-short data usage clause. gfortran generates link names with a trailing underscore, so we play that game too. Not too bad on the tectonic voodoo scale.


OCOBOL >>SOURCE FORMAT IS FIXED
*> ***************************************************************
*> Author: Brian Tiffin
*> Date: 20110411
*> Purpose: Call a FORTRAN color unit converter, rgb, hsv, ...
*> Tectonics:
*> gfortran -ffree-form -shared -fPIC -o libcolors.so colors.for
*> cobc -x rgbcobol.cob -lcolors -L .
*> ***************************************************************
identification division.
program-id. rgbcobol.

data division.
working-storage section.
01 r usage float-short.
01 g usage float-short.
01 b usage float-short.

01 h usage float-short.
01 l usage float-short.
01 s usage float-short.

01 st usage binary-long.

*> ***************************************************************
procedure division.
move 000.0 to h
move 050.0 to l
move 100.0 to s
display "Calling FORTRAN with " h space l space s end-display
call "jucolor_" using 'hls', h, l, s, 'rgb', r, g, b, st end-call
display "Returned " r space g space b end-display
display "Status of " st end-display
call "showit_" end-call
goback.
end program rgbcobol.

With a Makefile of


all: rgbcobol

libcolors.so: colors.for
gfortran -ffree-form -shared -fPIC -o libcolors.so colors.for

rgbcobol: rgbcobol.cob libcolors.so
cobc -g -debug -x rgbcobol.cob -lcolors -L .

and a sample of


$ make
[btiffin@home fortran]$ ./rgbcobol
Calling FORTRAN with 0.000000000000000000 50.000000000000000000 100.000000000000000000
inside jucolor_: 0.0000000 0.0000000 50.000000 0.0000000 100.00000 0.0000000
Returned 100.000000000000000000 0.000000000000000000 0.000000000000000000
Status of +0000000000
inside jucolor_: 0.0000000 0.0000000 50.000000 595.19684 100.00000 4.57103559E-41
INPUT HLS PURE RED ==> OUTPUT RGB values are 100.00000 0.0000000 0.0000000
================================================================================
inside jucolor_: 120.00000 100.00000 50.000000 0.0000000 100.00000 0.0000000
INPUT HLS PURE GREEN OUTPUT RGB values are 0.0000000 100.00000 0.0000000
================================================================================

And that's it. Minutes later publicly available Fortran snippets, compiled and linked to OpenCOBOL, data back and forth, done.

Cheers,
Brian

Leave a Reply

Your email address will not be published. Required fields are marked *