[fpc-pascal] C++ and Free Pascal

Florian Klaempfl florian at freepascal.org
Wed Mar 16 08:46:01 CET 2005


Bob Shaffer wrote:

> Peter Vreman said:
> 
>>>While searching the web for the answer to a somewhat common question:
>>>Is it possible to link C++ libraries to Free Pascal programs?
>>>I came across a reply on this mailing list.  The answer is no, and it
>>>won't probably be possible any time soon.  The suggestion I read there
>>>was
>>>fairly simple.  Rewrite the library in Free Pascal.  While I don't
>>>particularly want to do that, it did make me wonder about something
>>>else:
>>>
>>>Is it possible to link Free Pascal to C++?
>>>A little more specifically, is it possible to use object-oriented pascal
>>>code in a C++ program?
>>
>>Only with a wrapper unit that exports only normal functions/procedures
>>(not methods) declared with cdecl it'll be possible.
> 
> 
> Do you mean possible to link the C++ to a pascal unit, or vice-versa?
> 
> As far as linking the C++ into a pascal unit goes, I could possibly manage
> to write a wrapper library (in C++) where I could maybe make a struct for
> the data and pass it to all of the functions, and then link them all into
> pascal with the cdecl stuff, and then maybe even make a pascal object
> that's a wrapper for those things, but it just sounds like it would get
> uglier and uglier as we go...  The general answer of "it's not possible"
> is acceptable to me.
> 
> Maybe we can go the other way, though?  Maybe I can make a unit full of
> objects in pascal, and then link that to C++ programs.  Would that be
> possible without having to put wrappers over wrappers, or am I pretty much
> stuck maintaining the library in both C++ and Pascal if I want it in both?

In general, it requires wrappers, anything else isn't possible. However, if you 
do a good library design, it should be possible. Look at e.g. COM: It has the 
two most important properties of OOP: inheritance and polymorphism and it is 
portable between languages. I.e. creating pure virtual class interfaces makes a 
library portable between languages possible and it's still OOP.




More information about the fpc-pascal mailing list