[fpc-devel] Linking to C++

Peter Popov ppopov at tamu.edu
Mon Jan 22 21:31:42 CET 2007


Further reading on some limited portability of C++ classes into Delphi:

http://www.rvelthuis.de/articles/articles-cppobjs.html

Take a specific look at the second part, with pure virtual classes. The  
trouble is it only works for virtual C++ classes (have at least on virtual  
function - hence a VMT) with single inheritance (single VMT with virtual  
functions at offset 0). The C++ dll must provide a factory function and a  
wrapper for deleting the class. No further extension of this class is  
possible in Delphi.


On Mon, 22 Jan 2007 13:17:48 -0600, Felipe Monteiro de Carvalho  
<felipemonteiro.carvalho at gmail.com> wrote:

> Hello,
>
> I was studing how to write the system unit for Symbian OS, and
> everything on Symbian is C++, so you actually import lot´s of methods
> from classes, not procedures. So I decided to study the task of
> linking from Free Pascal to C++
>
> Maybe it´s easy to be implemented, and that would be very nice. Here is  
> my test:
>
> To start with I created a trivial C++ DLL:
>
> File: cpplib.cpp
>
> class User
> {
>   public:
>      __declspec(dllexport) int Sum(int a, int b);
> };
>
> int User::Sum(int a, int b)
> {
>     return a + b;
> }
>
> File: build.bat
>
> PATH=C:\Programas\Dev-Cpp\bin
> g++ -c cpplib.cpp -o cpplib.o
> dllwrap.exe --output-def cpplib.def --driver-name c++ --implib
> cpplib.a cpplib.o -L"c:/Programas/dev-cpp/lib" --no-export-all-symbols
> --add-stdcall-alias -o cpplib.dll
>
> Calling build.bat will of course produce cpplib.dll It also produces a
> cpplib.def
>
> Now, I utilized dllview software I created and saw that cpplib exports
> this function: _ZN4User3SumEii
>
> Then, I created a simple pascal software to call that function:
>
> program loadcpp;
>
> {$mode objfpc}{$H+}
>
> {$apptype console}
>
> uses
>   SysUtils, ctypes;
>
> function User_Sum(Self: Pointer; a, b: cint): cint; cdecl; external
> 'cpplib.dll' name '_ZN4User3SumEii';
>
> begin
>   WriteLn('7 + 8 = ' + IntToStr(User_Sum(nil, 7, 8)));
> end.
>
> And it works!
>
> So, basically, what I mean with this is: What is needed to add C++
> linking to Free Pascal? It seams to me that it should be easy, we just
> need to understand C++ name mangling and choose a sintax to implement
> it. Maybe something like this:
>
> type
>   User = class
>   public:
>     function Sum(a, b: cint): cint; cdecl; external 'cpplib.dll';
> cpp_name_mangling;
>   end;
>
> One possible problem is C++ multiple inheritance. I would propose
> that, to start with, just disconsider it.
>
> Disclaimer: I know very little on the subject, so feel free to correct  
> me =)
>
> thanks,



-- 
|***********************************************|
|                                               |
|  Peter Popov,                                 |
|                                               |
|  608N Blocker Bldg.                           |
|  Institute for Scientific Computation,        |
|  Texas A&M University                         |
|  College Station, Texas 77843-3404            |
|-----------------------------------------------|
|  Phone: +1 (979) 458-4644,                    |
|  Fax: +1 (979) 845-5827                       |
|                                               |
|***********************************************|



More information about the fpc-devel mailing list