Search code examples
c++dllfreepascallazarus

How to use functions defined in a C++-produced .dll or .lib file within Lazarus (freepascal)?


I want to use functions from the 'ANN' library (Approximate Nearest Neighbor) in my Lazarus code. This application is written in C++, and comes with source code. How can I access the functions from within Lazarus/FreePascal?

Edit: Lazarus/FPC is similar to Delphi.


Solution

  • It depends on the library. If it is the "C" subset of C++, or if the interface is specially crafted to be interoperable, see opc0de's answer. An important signal that this is the case is an "extern C {}" block.

    If the library is true C++, you have a problem. There is no universal exporting mechanism for C++, and even two different C++ compilers generally don't understand eachother.

    To use it, you must create a so called wrapper. A piece of code compiled with the /same/ C++ compiler that wraps everything C++ specific and only exports plain C functions (extern C{})

    As an example, have a look at the so called QTPAS wrapper, that wraps QT for use in Free Pascal/Delphi.