[fpc-devel] Linking to C++
Felipe Monteiro de Carvalho
felipemonteiro.carvalho at gmail.com
Mon Jan 22 20:17:48 CET 2007
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,
--
Felipe Monteiro de Carvalho
More information about the fpc-devel
mailing list