HPlogo HP C++ Programmer's Guide: HP 9000 Series Workstations and Servers > Chapter 5 Inter-Language Communication

Calling HP C++ from HP C

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

Examples 5-3 and 5-4 show an example of calling HP C++ from HP C. Example 5-3 is the C++ module and example 5-4 is the C program. These examples illustrate the following points:

  • To prevent a function name from being mangled, the function definition and all declarations used by the C++ code must use extern "C".

  • The C programmer must generate a call to function _main as the first executable statement in main(). Object libraries require this as _main calls the static constructors to initialize the libraries' static data items.

  • Member functions of classes in C++ are not callable from C. If a member function routine is needed, a non-member function in C++ can be called from C which in turn calls the member function.

  • Since the C program cannot directly create or destroy C++ objects, it is the responsibility of the writer of the C++ class library to define interface routines that call constructors and destructors, and it is the responsibility of the C user to call these interface routines to create such objects before using them and to destroy them afterwards.

  • The C user should not try to define an equivalent struct definition for the class definition in C++. The class definition may contain bookkeeping information that is not guaranteed to work on every architecture. All access to members should be done in the C++ module.

  • This example also illustrates reference parameters in the interface routine to the constructor.

Figure 5-3 Example 5-3. A C++ Module Called by a C Program

//**************************************************

//  C++ module that manipulates object obj.        *

//**************************************************

#include <iostream.h>



typedef class obj* obj_ptr;



extern "C" void initialize_obj (obj_ptr& p);

extern "C" void delete_obj (obj_ptr p);

extern "C" void print_obj (obj_ptr p);



struct obj {

private:

     int x;

public:

     obj() {x = 7;}

     friend void print_obj(obj_ptr p);

};



// C interface routine to initialize an 

// object by calling the constructor.

void initialize_obj(obj_ptr& p) {

     p = new obj;

}



// C interface routine to destroy an 

// object by calling the destructor.

void delete_obj(obj_ptr p) {

     delete p;

}



// C interface routine to display 

// manipulating the object.

void print_obj(obj_ptr p) {

cout << "the value of object->x is " << p->x << "\n";

}

Example 5-4 is a C program that calls the C++ module in example 5-3 to manipulate the object:

Figure 5-4 Example 5-4. A C Program Calling a C++ Module

/***************************************************/

/* C program to demonstrate an interface to the    */ 

/* C++ module.  Note that the application needs    */

/* to be linked with the CC driver.                */

/***************************************************/

typedef struct obj* obj_ptr;



main () {

     /* C++ object. Notice that none of the 

        routines should try to manipulate the fields.

     */

     obj_ptr f;



/* The first executable statement needs to be a call 

   to _main so that static objects will be created in 

   libraries that have constructors defined.  In this 

   application, the stream library contains data 

   elements that match the conditions.

*/

     _main();



     /* Initialize the data object. Notice taking 

        the address of f is compatible with the 

        C++ reference construct.

     */

     initialize_obj(&f);



     /*  Call the routine to manipulate the fields */

     print_obj(f);



     /*  Destroy the data object */

     delete_obj(f);

}

To compile the programs in examples 5-3 and 5-4, enter the following commands:

cc -c cfilename.c

CC -c C++filename.C

CC -o executable cfilename.o C++filename.o
CAUTION: During the linking phase, the CC driver program performs several functions to support the C++ class mechanism. Linking programs that use classes with the C compiler driver cc leads to unpredictable results at run time.
© Hewlett-Packard Development Company, L.P.