Lazarus

Programming => General => Topic started by: regg on May 09, 2021, 03:10:09 pm

Title: Can I mix pascal and C together?
Post by: regg on May 09, 2021, 03:10:09 pm
I want to make a few programs using pascal, but might need to use C for some low level stuff. How can I easily mix C and pascal together? Can I just rewrite what I want to do with C in pascal instead? I might need to interface with some hardware, that's why C might come into the picture.
Title: Re: Can I mix pascal and C together?
Post by: MarkMLl on May 09, 2021, 03:41:42 pm
Start off looking at the h2pas utility.

MarkMLl
Title: Re: Can I mix pascal and C together?
Post by: Warfley on May 09, 2021, 04:09:12 pm
Usually you can pretty easiely rewrite C to Pascal, but you can also link pascal and c code together and call C functions from pascal.

There are just a few things to keep in mind when linking pascal and C together, Pascal has managed memory (strings, dynamic arrays), C doesn't, these won't survive the transition between C and Pascal code. Also when linking C and pascal together, you should use the C memory manager from the cmem unit (must be the first unit in the uses clause of your lpr)
Title: Re: Can I mix pascal and C together?
Post by: PascalDragon on May 09, 2021, 05:12:18 pm
I want to make a few programs using pascal, but might need to use C for some low level stuff. How can I easily mix C and pascal together? Can I just rewrite what I want to do with C in pascal instead? I might need to interface with some hardware, that's why C might come into the picture.

You can just as well use Pascal for that. Usually you only need to worry about C if you need to interface with some library written in C.
Title: Re: Can I mix pascal and C together?
Post by: Blade on May 09, 2021, 05:37:19 pm
I want to make a few programs using pascal, but might need to use C for some low level stuff.... I might need to interface with some hardware, that's why C might come into the picture.

Pascal can do low level stuff and interface with hardware.  So you might want to be more specific.  If you are just talking in the more generalized sense and as a future possibility, probably best to just focus on Pascal for the present and immediate future.  Maybe don't worry about it until the use case shows itself.
Title: Re: Can I mix pascal and C together?
Post by: Graham1 on May 11, 2021, 04:34:33 am
Also when linking C and pascal together, you should use the C memory manager from the cmem unit (must be the first unit in the uses clause of your lpr)
I see in the documentation of cmem that it must be the first unit (https://wiki.freepascal.org/CMem (https://wiki.freepascal.org/CMem)), and that cthreads should preferably be the very first unit (https://www.freepascal.org/docs-html/rtl/cthreads/index.html (https://www.freepascal.org/docs-html/rtl/cthreads/index.html)) which is all OK, until I get to this page https://wiki.lazarus.freepascal.org/Multithreaded_Application_Tutorial (https://wiki.lazarus.freepascal.org/Multithreaded_Application_Tutorial) where the "Units needed for a multi-threaded application" section states:

Code: Text  [Select][+][-]
  1. You donĀ“t need any special unit for this to work with Windows. However with Linux, macOS and FreeBSD, you need the cthreads unit and it must be the first used unit of the project (the program source, usually the .lpr file)!

So both cmem and cthreads need to be the first unit?!
Title: Re: Can I mix pascal and C together?
Post by: PascalDragon on May 11, 2021, 08:27:50 am
So both cmem and cthreads need to be the first unit?!

Don't forgot cwstrings which needs to be the first as well. ;)

Joking aside, due to how the units work a sensible order is cmem, cthreads and then cwstrings.
Title: Re: Can I mix pascal and C together?
Post by: trev on May 11, 2021, 08:38:58 am
Joking aside, due to how the units work a sensible order is cmem, cthreads and then cwstrings.

What about clocale? Does its order matter too?
Title: Re: Can I mix pascal and C together?
Post by: PascalDragon on May 12, 2021, 09:06:50 am
Joking aside, due to how the units work a sensible order is cmem, cthreads and then cwstrings.

What about clocale? Does its order matter too?

It might be best to put it after cwstrings.
Title: Re: Can I mix pascal and C together?
Post by: del on May 13, 2021, 06:32:55 am
Hi Regg. I call a lot of opencv stuff (compiled C++ binaries) from a Lazarus built GUI. It's been great. Complete separation of Pascal and C++. I use TProcess. Here's a chunk of code to give you the idea. The code that's missing is code I use to parse the output stream and run a progress bar. But that's another story for another time. You get the basics from this:

Code: Pascal  [Select][+][-]
  1. procedure BatchFarneback(frameList: string);
  2. var
  3.   xFarneback: TProcess;
  4.  
  5. begin
  6.   xFarneback := TProcess.Create(nil);
  7.   xFarneback.Executable:= '/PROJECTS/MiscCpp/BatchFarneback/bin/release/BatchFarneback';
  8.   xFarneback.Parameters.Add('-paramfile');
  9.   xFarneback.Parameters.Add(frameList);
  10.   xFarneback.Options := [poUsePipes, poStderrToOutPut];
  11.   xFarneback.Execute;
  12.  
  13.   // blah blah blah ... engaging with output stream ...
  14.  
  15.   xFarneback.Free;
TinyPortal © 2005-2018