Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Calling a C++ library with ORA_FFI?
Hi!
> The only thing Oracle can tell me is a registered bug 516590:
> @ This is not a bug. Interfacing foreign functions via the ORA_FFI
> @ builtin package is only supported toward languages complying with
> @ the C or Pascal calling standards (see page 6-2 of the OPB
> @ documentation). C++ does not fall in such category.
>
> I guess this means we won't succeed. Is there anyone out there using the
>same configuration?
>We succeeded in working with a shared library with only ANSI-C objects.
>So what could be the difference?
>
> Thanks for any suggestions,
I don't know anything about Oracle, but I know a lot about C++, and this problem sounds familiar to me.
Use extern "C" functions.
Look for description of 'extern' keyword in your compiler documentation to find more info about extern "C".
If you can change the library, build your interface to external world using functions declared as extern "C". You should be able to call extern "C" functions (and only them) from your Oracle code, since these functions follow C calling standards, even though they are written in C++.
If you can't change the library, you may be able to write another ("thunk") C++ library that exports extern "C" functions and calls your original library.
I think the problem is in so-called "name mangling" or "decorated names".
This is described in various C++ books and numeruous articles, but I don't have particular pointer right now. Probably, Stroustrup mensioned it in his "The C++ Programming Language".
Basically, the names of objects (mostly functions) used inside C++ program are different from "external" names of those functions visible to linkers and other operating system tools.
This happens for three reasons:
void MyFunc( int x, int y);
and
int MyFunc( double a );
They both cannot simply have external name "MyFunc", because linker will complain about duplicate symbols. Therefore, C++ compiler must somehow encorporate function arguments into function name in order to make function overloading possible. The way this encoding is done is not standard, and it is different from compiler to compiler. External function name for first version of MyFunc might look like @MyFunc$ii, why the second one may have a name like @MyFunc$d.
3. Linkers don't understand "special" functions like destructors or overloaded operators.
We must have a valid external names for functions like
MyClass operator+(MyClass const& a, MyClass const& b);
Such a function may receive an external name like @$operator_plus$7MyClasscr7MyClasscr
In order to avoid name mangling you can declare functions as extern "C".
For example
// function definition
extern "C" void MyFunc( int x, int y)
{
...
};
// function declaration
extern "C" void MyFunc( int x, int y );
Of course, only stand-alone functions (i.e. not class methods) can be declared extern "C". If you try to overload a function declared as extern "C", compiler will complain.
Extern "C" functions follow "C" calling convention, and thus you should be able to call them from Oracle. Extern "C" functions may be written in C++, not in C. They may use classes and call C++ functions.
Best wishes and good luck
Ivan
Received on Thu Feb 10 2000 - 10:49:40 CST
![]() |
![]() |