[fpc-devel] Linking to C++

Peter Popov ppopov at tamu.edu
Tue Jan 23 00:16:32 CET 2007

About the name mangling: this thing is compiler specific. To have really  
portable code, whoever compiled the DLL should include flattened  
procedural API model without name mangling. See the Kylix QT libraries:  
borland have compiled their own version of Qt2.3.0 and have exported  
things in a portable fashion. As things should be done.

Now, there is now way to extend an already compiled C++ class, so whoever  
did the DLL MUST PROVIDE factory constructor and destructor. There is now  
way to mix the construction and destruction of classes (C++ and Pascal  
VMTs are different). So, a single member function cannot be exported  
alone. You have to export all members of the entire class. Therefore, the  
DLL creator should provide a flattened interface + constructor and  
destructor wrappers. This may, or may not be done automatically by the  
compiler. It is a matter of choice. Look here  
http://andy.jgknet.de/oss/kylix/wiki/index.php/Qt3Clx for some enthisiasts  
who apparently interfaced kylix with QT3 in some automatic fasion.

Mangled names are just not portable: consider the idiots who maintain the  
gcc standard libraries on linux. I have a lingering suspicion that these  
morons itentionally make the binaries incompatible from version to  
version. This way they force everybody to recompile their SOURCE CODE  
against the new gcc libraries version. Moreover, they force  
C++ programmers to use gcc, when compiling complex programs under linux.  
This prevents people from the distribute and forget model of supplying  
binaries. You simply have to maintain whatever program you compiled  
against gcc. This is why you cannot write some nice variant of solitaire,  
distribute and forget about it. The next major version of QT will break  
it. So essentially, a fully portable API should treat the class instances  
as "Handles" (e.g. win32, libborqt.so) and you use them as standard  
functions. The code on the back side may or may not use classes, but the  
front end is always transparent. So, unfortunately, the only thing you can  
do is file a complaint with whoever is maintaining this Symbian API.


PS. If a library forces you to mix objects in different languages, it  
simply is a badly written library.

On Mon, 22 Jan 2007 15:18:11 -0600, Felipe Monteiro de Carvalho  
<felipemonteiro.carvalho at gmail.com> wrote:

> Maybe the compiler could hide the
> procedurization/re-object-orientation, thus making interfacing easier.
> So this:
> type
>  User = class
>  public
>    function Sum(a, b: cint): cint; cdecl; external 'cpplib.dll';
>  end;
> Would be the exact equivalent of:
> function User_Sum(Self: Pointer; a, b: cint): cint; cdecl; external
> 'cpplib.dll' name '_ZN4User3SumEii';
> type
>  User = class
>  private
>     InternalHandle: Pointer;
>  public
>    function Sum(a, b: cint): cint; cdecl;
>  end;
> function User.Sum(a, b: cint): cint;
> begin
>   Result := User_Sum(InternalHandle, a, b);
> end;

More information about the fpc-devel mailing list