C/C++ Programmer's Guide (G06.27+, H06.08+, J06.03+)

8 Mixed-Language Programming for TNS/R and TNS/E
Native Programs
This chapter describes the Common Run-Time Environment (CRE) and the interface declarations
that are necessary to interface from native C or C++ to other programming languages. The CRE
and the interface declarations are HP features for NonStop systems that are not available in
standard ISO/ANSI C.
You can write native mixed-language programs targeted for the NonStop environment. Programs
can contain routines written in native C, C++, pTAL, and native COBOL. The main() function in a
native mixed-language program can be written in native C or C++, native COBOL, but not pTAL.
NOTE: TNS/R native objects cannot be linked with TNS/E native objects.
Introducing the CRE
The Common Run-Time Environment (CRE) is a set of services that support mixed-language programs.
The CRE library is a collection of routines that implement the CRE. The CRE library enables the
language-specific run-time libraries to coexist peacefully with each other. User routines and run-time
libraries call CRE library routines to access shared resources managed by the CRE, such as the
standard files (input, output, and log) and the user heap, regardless of language.
The CRE does not support all possible operations. For example, the CRE supports file sharing only
for the three standard files: standard input, standard output, and standard log. The language-specific
run-time libraries access all other files by calling Guardian system procedures directly, whether or
not a program uses the CRE.
All native mixed-language programs run in the CRE environment. Existing TAL code that is converted
to pTAL must also be converted to run in the CRE. For more details on writing programs that use
the services provided by the CRE, see the Common Run-Time Environment (CRE) Programmer’s
Guide.
Using Standard Files in Mixed-language Programs
In a mixed-language program, if a C function is to be the main function, it should be compiled
with the NOSTDFILES pragma to keep it from automatically opening the three standard C files:
stdin, stdout, and stderr.
If the main routine is not written in C, the three standard C files will not be automatically opened.
If you want any or all the standard files to be opened for C, you must explicitly open them by
calling the fopen_std_file() function.
Declaring External Routines
Your native C and C++ programs can call procedures written in COBOL and pTAL, in addition to
procedures written in an unspecified language type. You cannot mix TNS and native mode
languages.
All external procedures must be declared. When interfacing to pTAL or an unspecified language,
you must declare the external procedure appropriately because lexical and operational features
differ between C or C++ and other languages. There are two ways to declare an external function:
using a function prototype and a FUNCTION pragma (the preferred method), and using an interface
declaration (the traditional method).
Writing Interface Declarations
The interface declaration is an HP extension of the function declaration as defined by ISO/ANSI
C. The interface declaration indicates the correct language or indicates unspecified if the language
122 Mixed-Language Programming for TNS/R and TNS/E Native Programs