VR-Link API Documentation for HLA 1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
3.2 - Using VR-Link from C

Table of Contents

The directory .

/examples/simpleC contains an example of using VR-Link with an application written in C. The example is a C version of the listen-only program described in 2.9.1 A Listen-Only Example. For details, please see the header files interface.h and simpleC.h.

3.2.1 Calling C++ from C

Since VR-Link is a C++ toolkit, any code that directly calls a VR-Link function must be compiled by a C++ compiler. You can compile this code in such a way that it may be called from C. To do so, you need at least one C++ file from which all calls to VR-Link functions can be made. In our example, the file interface.cxx serves this purpose. It contains several functions that interact with VR- Link code: initVRLink(), tickVRLink(), and firstEntityPosition().

To alert the C++ compiler that these functions should be compiled to be callable from C, their declarations are surrounded by the following keywords, as seen by the C++ compiler:

extern "C" {
}

The file listenC.c represents the rest of the application. It is compiled by a C compiler, and does not directly call any VR-Link functions. It does, however, call the functions defined in interface.cxx that were compiled to be called from C.

3.2.2 Calling C from C++

You can create a C++ function that interfaces with VR-Link code and can also call C functions. For example, in interface.cxx, the callback on Fire PDUs (fireCb) is a C++ function which calls the C function firePrint().

To alert the C++ compiler that an external function is a C function, its declaration is surrounded by the following keywords, as seen by the C++ compiler:

extern "C" {
}

3.2.3 Header Files that are Included by C and C++

When C and C++ code interact, the declarations of many of the same functions must be included by both C and C++ code. As described above, these declarations need to be surrounded by:

extern "C" {
}

when presented to the C++ compiler. However, these keywords are meaningless to a C compiler, so that they should always be surrounded by:

#ifdef __cplusplus
#endif

For example, listenC.h reads as follows:

#ifdef __cplusplus
extern "C" {
#endif
void firePrint(const char *attacker);
#ifdef __cplusplus
}
#endif

[<< Compiling and Linking a VR-Link Application] [Home] [Top of Page] [Delivering VR-Link Applications to Users >>]


Document ID: Generated on Mon Apr 25 03:39:01 EDT 2022 from SVN revision 242502
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)