Chapter Contents

Previous

Next
Simple Interlanguage Communication

Calling a C main Function from Another Language

Calling a C main function from another high-level language is the easiest case of interlanguage communication. Provided that the other language's compiler produces a call-by-reference parameter list, as the IBM FORTRAN, PL/I, and COBOL Compilers do, you can simply invoke $MAINC or $MAINO, as appropriate, from the other language, as described in Communication with Assembler Programs. Calling one of these entry points initializes the C execution framework, and that framework is accessible when the C program assumes control. A FORTRAN Call to a C main Function (via $MAINO) shows a FORTRAN call to a C main function. (The function called is the same function shown in C main Function Called from an Assembler Driver via $MAINO. The FORTRAN call has exactly the same effect as the assembler call included in that example.)


A FORTRAN Call to a C main Function (via $MAINO)
CHARACTER*17 COPTS INTEGER*2 OPTLEN EQUIVALENCE (COPTS, OPTLEN) C C CALL A MAIN C ROUTINE, PASSING THE RUN-TIME OPTIONS =FILLMEM AND C =FDUMP. THE OPTIONS STRING MUST BE PRECEDED BY A HALFWORD C CONTAINING THE STRING LENGTH. C COPTS = 'NN=FILLMEM =FDUMP' OPTLEN = 15 CALL $MAINO(COPTS, 42, 67.4242D0, 'HELP')

Your C main function can, in turn, call other C functions or assembler language routines. However, main cannot call subroutines written in the other language. If you need to do this, refer to the SAS/C Compiler Interlanguage Communication Feature User's Guide or Using the indep Option for Interlanguage Communication.

Your C main function should return to the other language when it has finished by executing a return statement or by calling exit . Either terminates the C execution framework and returns control to the other language. The exit or return value is passed back in register 15, where it can be accessed if the other language provides this capability (as do COBOL and PL/I). Note that you can use exit in any C function to return to the calling program.

A C execution framework is created and destroyed (on return) every time you call a C main function. This could possibly create a significant overhead if the function is called many times. If this overhead is a problem in your application, you should consider using the interlanguage communication (ILC) feature.

Note that you can call only one main C function per load module in this manner because of the need to route all calls through one of the $MAINC or $MAINO entry points. However, you can call multiple C functions from main , passing main a code so that it knows which function to call.


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.