PDA

View Full Version : Library interfaces in pascal



chronozphere
19-04-2010, 01:25 PM
Hey, :)

I've had a discussion recently, with a friend of mine, which ended with the question: "How can you write an interface for an object-oriented library in pascal".

In C/C++, you have separate *.h and *.cpp files. If you want to distribute your library, you compile your code and you just distribute the binaries and the *.h header files.

But In pascal, everything (interface and implementation) is combined into one file. So, you have to do it in a different way. ???

Let's say someone writes a totally awesome object-oriented library in C++. Someone else would like to make pascal headers for this, but how? Pascal classes and C++ classes aren't compatible. Would the only option be to write a C++ wrapper that flattens all OOP to a C-style kind of interface (like dglOpenGL.pas), and have pascal code link against that wrapper? ???

Thanks. :)

JSoftware
19-04-2010, 01:29 PM
Until the cppclass FPC extension becomes a reality then yes, the only sane way is to write a C interface

Brainer
19-04-2010, 02:39 PM
If you're talking about DLL libraries, then there's probably no way to implement Pascal headers.
But if the source code is available, there's absolutely no problem to convert the code. :)

tpascal
19-04-2010, 08:38 PM
Let's say someone writes a totally awesome object-oriented library in C++. Someone else would like to make pascal headers for this, but how? Pascal classes and C++ classes aren't compatible.


There is a interesting article about how to use C++ clases into delphi,

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

But its is always required a DLL as Delphi's linker cant link c++ .OBJ.

chronozphere
19-04-2010, 09:11 PM
Wow.. cool... Thanks for that link :)

NecroDOME
21-04-2010, 10:59 AM
afaik c++ and pascal "interfaces" are compatible.

chronozphere
21-04-2010, 05:18 PM
afaik c++ and pascal "interfaces" are compatible.


That's true. Otherwise, the current DX headers for pascal wouldn't work. ;)

Do note, that it's alot more work to write and maintain interfaces for all the classes you want to export. Also, you have to deal with the lifecycle issue (COM interfaces have reference counting, which can cause problems when objects are deleted). All in all, I don't think it's the nicest way of interfacing with a dynamic library. I'd rather just use classes. :)