It is the use of these features, as opposed to whether the class keyword is used rather than struct, that introduces incompatibilities with C structs.

HP aC++ Calling HP C

Calling between C and C++ is a normal operation, since C++ is for the most part a superset of C. You should, however, be aware of the following:

Using the extern "C" Linkage Specification

Differences in Argument Passing Conventions

The main() Function

Using the extern "C" Linkage Specification

To handle overloaded function names the HP aC++ compiler generates new, unique names for all functions declared in a C++ program. To geneate these names, the compiler uses a function-name encoding scheme that is implementation dependent. A linkage directive tells the compiler to inhibit this default encoding of a function name for a particular function.

To call a C function from a C++ program, you must disable the usual encoding scheme when you declare the C function. When you do not disable the usual encoding scheme, the function name declared in your C++ program will not match the function name in your C module defining the function.

When the names do not match, the linker cannot resolve them. To avoid these linkage problems, use a linkage directive when you declare the C function in the C++ program.

Syntax of extern "C"

All HP aC++ linkage directives must have either of the following formats:

extern “C” function_declaration

extern “C”

{

function_declaration1 function_declaration2

...

function_declarationN

}

Examples of extern "C"

The following declarations are equivalent:

extern

“C” char* get_name(); // declare the external C module

and

 

 

extern

“C”

 

{

 

 

char*

get_name();

// declare the external C module

}

 

 

You can also use a linkage directive with all the functions in a file, as shown in the following example. This is useful when you use C library functions in a C++ program.

extern “C”

{

#include “myclibrary.h”

}

NOTE: Do not use extern "C" when you include standard C header files. These header files already contain extern "C" directives.

Data Compatibility between C and C++ 189

Page 189
Image 189
HP C/aC++ for PA-RISC Software manual HP aC++ Calling HP C, Using the extern C Linkage Specification, Syntax of extern C