10 Mixing C++ with Other Languages

This chapter provides guidelines for linking HP aC++ modules with modules written in HP C and HP FORTRAN 90 on HP 9000 Series 700/800 systems. It discusses the following topics:

“Calling Other Languages” (page 188)

“Data Compatibility between C and C++” (page 188)

“HP aC++ Calling HP C” (page 189)

“HP C Calling HP aC++” (page 191)

“Calling HP FORTRAN 90 from HP aC++” (page 193)

Calling Other Languages

A module is a file that contains one or more variable or function declarations, one or more function definitions, or similar items logically grouped together. Mixing modules written in C++ with modules written in C is relatively straightforward since C++ is for the most part a superset of C. Mixing C++ modules with modules in languages other than C is more complicated.

When creating an executable file from a group of programs of mixed languages, one of them being C++, you must be aware of the following:

In general, the overall control of the program must be written in C++. In other words, the main function should appear in a C++ module and no other outer block should be present.

You must pay attention to case sensitivity conventions for function names in the different languages.

You must make sure that the data types in the different languages correspond. Do not mismatch data types for parameters and return values.

Storage layouts for aggregates differ among languages.

You must use the extern "C" linkage specification to declare modules that are not written in C++; this is true whether or not the module is written in C.

You must use the extern "C" linkage specification to declare modules that are written in C++ and called from other languages.

Do not use extern "C" when you include standard C header files because these header files already contain extern "C" directives.

NOTE: HP aC++ classes are not accessible to non-C++ routines.

Data Compatibility between C and C++

Many of the data types between C and C++ are identical as C++ is, for most part, a superset of C. Both languages support char, short, int, long, float, and double data types. ANSI C and HP C++ also support a long double type. In addition, HP aC++ supports bool, wchar_t, long long, and unsigned long long data types.

Pointers, structs, and unions that can be declared in C are also compatible. Arrays composed of any of the above types are compatible.

C++ classes are generally incompatible with C structs. The following features of the C++ class facility may cause the compiler to generate extra code, extra fields, or data tables:

Multiple visibility of members (including both private and public data members in a class)

Inheritance, either single or multiple

Virtual functions

188 Mixing C++ with Other Languages