Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2005-03

C++ and Free Pascal

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bob Shaffer

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?

If I could rewrite my library in pascal and then link it to a C++ program, it wouldn't be so bad. I would have to rewrite it all, yes - but I wouldn't have to maintain two nearly identical libraries.

_________________________________________________________________________ Bob Shaffer II - Owner, Developer, System Operator - BobShaffer.net Website: http://bobshaffer.net/ BBS: telnet://bobshaffer.net

Peter Vreman
to Bob Shaffer
Show quoted text 10 lines
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.

Bob Shaffer
to Peter Vreman

Peter Vreman said:

Show quoted text 15 lines
> 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?

Thanks,
Bob Shaffer II

Florian Klaempfl
to Bob Shaffer
Bob Shaffer wrote 33 lines
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. : 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.

Marco van de Voort
to Bob Shaffer: Peter Vreman said: Do you mean possible to link the C++ to a pascal unit, or vice-versa?…
Show quoted text 7 lines
Peter Vreman said: > (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++)

(in plain C)

Show quoted text 10 lines
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?

Gives same problem. Object models are simply not the same. It's not just C++ vs Pascal, but e.g. the same problem between two C++ compilers or between FPC and Delphi.