Skip to main content

IBM Research


C Interface Primer

The C Interface presents the NAO class library as a C subroutine library. Constructors and member functions are provided via subroutines, and the conversion from C to C++ is almost entirely one to one. Operator overloading cannot be provided, so operations between objects is also done by way of subroutine calls.

A constructor becomes a subroutine which returns a pointer to an object. Constructor routines have names which begin with NACreate. The C program does not know that the pointer is really to a C++ object, so some care must be taken not to perform any arithmetic on the pointer. For example, to create a new NABall using the C interface

  NACManifold B=NACCreateNABall(3);

The type NACManifold is a pointer to a void object.

When an object is created, memory is allocated for it. To release the memory, the user should call NACUnReference(obj).

Member functions are invoked by a subroutine with the same name as the member function, but with a first argument which is a pointer to the object whose member function is to be invoked. The remaining arguments are unchanged. For example, to get the base space dimension of the Ball we created

  int base;

  base=NACManifoldGetBaseSpaceDimension(B);

The corresponding C++ code is
  int base=B->getBaseSpaceDimension();

Operators between objects must be done using explicit subroutine calls. The user must act as the C++ parser, by breaking up expressions into a sequence of binary and unary operations which will produce the desired result. The user must also make sure to delete the temporaries that are created when this is done.

The biggest difficulty in providing an interface to C (or any other traditional language), is the way that C++ names subroutines and member functions. Assigning a name to the equivalent C subroutine is not easy. There is a solution, and that is to mangle the names the way that C++ does. This is easy for us, but difficult for the user. For example, the C++ routines


would cause the C compiler to issue an error (if a prototype exists), or to use the same routine for both calls. The alternative is to mangle and use the following prototypes for the C subroutines:

Not pretty.

The C routines are automatically generated from a template file, which is also used to generate the other interfaces. Each member function and routine has a template, and we have manually specified names for the routines with duplicate names.


[Comments | NAO Home Page]

[Research home page]

[ IBM home page | Order | Privacy | ContactIBM | Legal ]