So I’m trying to put together all the bits and pieces of suggestions, but running into some kind of very basic (no pun intended) problem. Probably a newbie mistake – or several.
My test throws an exception, that the function I’m calling can’t be found, but nm shows the function IS in the dylib, with normal C-callable binding:
Stephen-Retina-MacBookPro-59:Debug stephen$ nm libTestLibForXojo.dylib
0000000000000f2c s GCC_except_table1
U __Unwind_Resume
0000000000000de0 T __ZN10MyCPPClass13GetTestStringEv
0000000000000ec0 t __ZN10MyCPPClassC1EPKc
0000000000000ef0 t __ZN10MyCPPClassC2EPKc
U __ZdlPv
U __Znwm
U ___gxx_personality_v0
0000000000000e60 T _deleteMyClass
0000000000000ea0 T _getACStringFromClass
0000000000000e00 T _newMyCPPClass
U dyld_stub_binder
The test app is just the absolute minimum to construct a class, call a function from that class, get a returned string (in this case a static string) and destruct the class. The path to the dylib is absolute. A FunctionNotFoundException being thrown comes as I try to call newMyCPPClass(), below.
XOJO TEST APP: This is the code on the action event of a button:
[code]CONST dylibLocation = “/Users/stephen/dev/XOJO Development/Tests for dynamic library communication/TestLibForXojo/DerivedData/Build/Products/Debug/libTestLibForXojo.dylib”
soft declare function newMyCPPClass lib dylibLocation as Ptr
soft declare function getACStringFromClass lib dylibLocation (clsPtr as Ptr) as CString
soft declare sub deleteMyClass lib dylibLocation (clsPtr as Ptr)
dim classPtr as Ptr = newMyCPPClass()
dim theText as CString = getACStringFromClass(classPtr)
TextArea1.SetString(theText)
deleteMyClass(classPtr)
[/code]
XojoTestCPPLibrary.hpp:
[code]#include <stdio.h>
#ifdef __cplusplus
class MyCPPClass
{
public:
MyCPPClass(void) { }
~MyCPPClass() { }
const char* GetTestString();
};
#endif
// C Interface:
typedef void* HMyCPPClass;
// Need an explicit constructor and destructor.
extern “C” HMyCPPClass newMyCPPClass( void );
extern “C” void deleteMyClass(HMyCPPClass);
// Each public C method. Takes an opaque reference to the object
// that was returned from the above constructor plus the methods parameters.
extern “C” const char * getACStringFromClass(HMyCPPClass);[/code]
XojoTestCPPLibrary.cpp:
[code]#include “XojoTestCPPLibrary.hpp”
const char* MyCPPClass::GetTestString()
{
return (const char*)“This is a test”;
}
// Functions implemented in a cpp file, declared in header as extern “C”
// to give them C linkage and thus are available from a C lib.
HMyCPPClass newMyCPPClass( void )
{
return reinterpret_cast<void*>(new MyCPPClass());
}
void deleteMyClass(HMyCPPClass theClassPtr)
{
delete reinterpret_cast<HMyCPPClass*>(theClassPtr);
}
const char * getACStringFromClass(HMyCPPClass theClassPtr)
{
return reinterpret_cast<MyCPPClass*>(theClassPtr)->GetTestString();
}[/code]
So that seems as simple as I can make it – but it’s obviously not right. Any guesses?