Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping a C++ library in Objective-C is not hiding the C++ symbols

I am trying to wrap a C++ library (for Sybase Ultralite) with Objective-C so that the library can be imported into MonoTouch. I have created an Objective-C library and included the compiled C++ library in this project, libulrt.a. To get my project to compile I set the path to the User Header Search Path to point to the directory where the C++ header files are located. I then had to set compile source as to Objective-C++.

The problem now is that, although the wrapper library compiles correctly, once I include it in another Xcode project I have to again set compile source as to Objective-C++ otherwise the project consuming my wrapper library gives linking errors. I don't understand why this is, because the header file for my wrapper library contains only Objective-C code and not C++ code at all. C++ code is only found in the implementation (*.mm file) of the wrapper library. What do I need to do to make the C++ implementation completely transparent to the project consuming the wrapper library? In other words, I want to be able to include my wrapper library and compile with compile source as set to Objective-C. Anyone got any ideas how to do this?

The linking errors that I am getting when compiling the project consuming my wrapper are as follows: (I have abridged the error listing because it is LONG!)

"operator delete(void*)", referenced from:
zc3db40339fee::~zc3db40339fee()in libUltralite.a(ee39bf4763.o)
zb4297ee7d543::~zb4297ee7d543()in libUltralite.a(747e80fdad.o)
z33836a0a6f46::~z33836a0a6f46()in libUltralite.a(f240efda30.o)
"___cxa_pure_virtual", referenced from:
vtable for ze78b0ec59364in libUltralite.a(2c50e8e8ff.o)
vtable for ze78b0ec59364in libUltralite.a(2c50e8e8ff.o)
vtable for ze78b0ec59364in libUltralite.a(2c50e8e8ff.o)

The header file for my wrapper library is as follows:

#import <Foundation/Foundation.h>

@interface DataAccess : NSObject {}

// Release objects.
- (void)dealloc;

// Singleton instance of the DataAccess class. 
+ (DataAccess*)sharedInstance; 

// Finalize the Database Manager when done with the DB.
+ (void)fini;

// Adds the given name to the database. 
- (void)addName:(NSString *)name;
@end 

Anyone have any idea how I can compile this as Objective-C++ but still have the projects consuming this library see it as Objective-C?

like image 892
BruceHill Avatar asked Mar 15 '11 13:03

BruceHill


2 Answers

@Mark Bessey is on the right track here, but he's incorrect that there is little difference between setting the language and adding the C++ library. Adding the C++ library is cheap and required (one way or another). Changing the language is expensive because the ObjC++ compiler is slower, and the resulting code is in my experience a bit of a pain to deal with in gdb through Xcode.

All you should need is to add -lstdc++ to your LD_FLAGS (Other Linker Flags) in your consuming project. Do make sure that "C++ Standard Library Type" is set to Dynamic.

like image 76
Rob Napier Avatar answered Sep 21 '22 22:09

Rob Napier


You'll have to build your wrapper as a shared object, not a static library. A static library is not linked, resolution of functions (and specifically the C++ standard library functions used by the code you are wrapping) happens at link time.

like image 45
Ben Voigt Avatar answered Sep 21 '22 22:09

Ben Voigt