Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: Calling a C++ library with ORA_FFI?

Re: Calling a C++ library with ORA_FFI?

From: Ivan Krivyakov <IvanK_at_optimed-tech.com>
Date: Thu, 10 Feb 2000 16:49:40 GMT
Message-ID: <D_Bo4.36135$ox5.9014636@tw11.nn.bcandid.com>


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.



Very short answer:

Use extern "C" functions.

Look for description of 'extern' keyword in your compiler documentation to find more info about extern "C".



Longer answer:

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.



Very long answer:

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:

  1. Linkers don't understand classes, namespaces and other C++ crap :-) E.g. on most operating systems they won't accept names like MyClass::Method2, because they don't allow a colon inside a name.
  2. Linkers don't understand function overloading. Let's say you have two functions

    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

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US