I'm working on a swift project which incorporates a C++ library (bullet physics). Currently I achieve this by wrapping the C++ library with Objective-C, and then using the Objectve-C Code in Swift via bridging header.
I would like to eliminate the Objective-C wrapper, and access Bullet through a pure C interface in the interest of making the code more platform agnostic.
Conceptually, I understand how to create a C interface for C++ code by using extern "C", but I don't know how to make the types from the C++ library available in my Swift Code.
For instance, I would like to be able to do something like this:
// MyPhysicsObject.swift
class MyPhysicsObject {
let rigidBody: btRigidBody
init() {
self.rigidBody = createRigidBody() //called from C interface
}
}
With a C header file:
// BulletCInterface.h
#ifdef __cplusplus
extern "C" {
#endif
btRigidBody* createRigidBody();
#ifdef __cplusplus
}
#endif
And a C++ implementation file:
//BulletCInterface.cpp
#import <BulletDynamics/btBulletCollisionCommon.h>
#import <BulletDynamics/btBulletDynamicsCommon.h>
#ifdef __cplusplus
extern "C" {
#endif
btRigidBody* createRigidBody() {
//invoke the C++ bullet API to produce a btRigid body
//...
}
#ifdef __cplusplus
}
#endif
But when I attempt this, the compiler complains that btRigidBody is an undefined type in BulletCInterface.h. At a minimum I would like to be able to hold references to these objects in swift, and at best I would like to be able to access their data members (like mass etc.)
How can I expose these C++ types to my Swift code? Or do I have to just make everything a void* and keep track of types myself?