Sign in Join

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

Mailing lists / lazarus / 2014-03

How to access C and C++ libraries?

9 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

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

Chavoux Luyt

Hi everybody!

Years ago I used Delphi (on Windows) to access a C or C++ API (for talking directly with embedded device) and I can remember that there were a number of gotchas (e.g. the calling convention had to be changed form Pascal to something else (extern?) etc. ).

Now I am wondering about writing a simple Lazarus GUI to the Telegram API (all C header files, AFAICT) on Linux (and maybe Windows, if not too difficult to port). Any gotchas to keep in mind for using C and C++ libraries from Lazarus/Free Pascal (on Linux and maybe Windows) or good guides?

Thanks
Chavoux
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140306/337d6668/attachment.html>

Reply by email

Sven Barth
to Chavoux Luyt
Chavoux Luyt wrote 12 lines
Hi everybody! Years ago I used Delphi (on Windows) to access a C or C++ API (for talking directly with embedded device) and I can remember that there were a number of gotchas (e.g. the calling convention had to be changed form Pascal to something else (extern?) etc. ). Now I am wondering about writing a simple Lazarus GUI to the Telegram API (all C header files, AFAICT) on Linux (and maybe Windows, if not too difficult to port). Any gotchas to keep in mind for using C and C++ libraries from Lazarus/Free Pascal (on Linux and maybe Windows) or good guides?

Basically: use cdecl calling convention (on Windows it *might* be stdcall though), do not use Pascal types like classes, strings, etc. for accessing the C API, better use the types in the ctypes unit instead.

Additional help is available here:

http://wiki.freepascal.org/Creating_bindings_for_C_libraries

Regards,
Sven

Reply by email

Luca Olivetti
to Chavoux Luyt: Hi everybody! Years ago I used Delphi (on Windows) to access a C or C++ API (for talking…

El 06/03/14 20:31, Chavoux Luyt ha escrit:

Show quoted text 5 lines
Now I am wondering about writing a simple Lazarus GUI to the Telegram API (all C header files, AFAICT) on Linux (and maybe Windows, if not too difficult to port). Any gotchas to keep in mind for using C and C++ libraries from Lazarus/Free Pascal (on Linux and maybe Windows) or good guides?

Take a look here
ftp://ftp.freepascal.org/fpc/docs-pdf/CinFreePascal.pdf

and here
http://wiki.lazarus.freepascal.org/Creating_bindings_for_C_libraries

Bye

Luca Olivetti Wetron Automation Technology http://www.wetron.es Tel. +34 935883004 Fax +34 935883007

Reply by email

Danny Weldon
to Michael Schnell
You can't access a C++ API.

Not yet. But it is planned for the future:

http://freepascal.org/future.var
"Linking with C++ code"

Michael Schnell wrote 15 lines
On 03/06/2014 08:31 PM, Chavoux Luyt wrote: > > > Years ago I used Delphi (on Windows) to access a C or C++ API You can't access a C++ API. -Michael -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Regards Danny

Reply by email

Daniel Gaspary
to Sven Barth: And with quite some restrictions it already works, see $fpc/tests/test/cg/tcppcl{1,2}.pp.…
Sven Barth wrote 10 lines
Am 07.03.2014 10:13, schrieb Danny Weldon: >> You can't access a C++ API. > > Not yet. But it is planned for the future: > > http://freepascal.org/future.var > "Linking with C++ code" And with quite some restrictions it already works, see $fpc/tests/test/cg/tcppcl{1,2}.pp.

Is this feature at trunk?

Further examples of use.. ?

Reply by email

Sven Barth
to Daniel Gaspary
Daniel Gaspary wrote 10 lines
On Fri, Mar 7, 2014 at 6:38 AM, Sven Barth wrote: > Am 07.03.2014 10:13, schrieb Danny Weldon: >>> You can't access a C++ API. >> Not yet. But it is planned for the future: >> >> http://freepascal.org/future.var >> "Linking with C++ code" > And with quite some restrictions it already works, see > $fpc/tests/test/cg/tcppcl{1,2}.pp. Is this feature at trunk?

I haven't worked on it since some time, so the current state is even in 2.6.0.

Further examples of use.. ?

A cppclass is declared like this:

=== code begin ===

type
   TTest = cppclass external 'MyCLib' name 'Test' (TParent)
     procedure Test;
     class procedure StaticTest;
   end;

=== code end ===

Notes:
- if TParent is defined it needs to be a cppclass as well
- for sizeof to work correctly you need to specifiy the fields the same way as in C++ (though this might still be buggy)
- cppclass instances are not implicit pointers like classes, so you need to use "myvar^.Test" instead

Current restrictions:
- currently no namespaces supported
- currently no support for virtual metods (static methods work, instance methods *should* work)
- no support for construction/destruction yet (so you need to have the library instantiate the object)
- no support for multiple inheritance or virtual inheritance
- no support for operators (especially not the C++ specific ones)

In general this feature is experimental/in development, so your mileage may vary. Bug reports are welcome though :)

Regards,
Sven

Reply by email