Can I mix pascal and C together? lazarus / 21-General ---------------------------------------------------------------------- regg · 2021-05-09 13:10 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. ---------------------------------------------------------------------- MarkMLl · 2021-05-09 13:41 Start off looking at the h2pas utility. MarkMLl ---------------------------------------------------------------------- Warfley · 2021-05-09 14:09 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) ---------------------------------------------------------------------- PascalDragon · 2021-05-09 15:12 Quote from: regg on May 09, 2021, 03:10:09 pmI 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. ---------------------------------------------------------------------- Blade · 2021-05-09 15:37 Quote from: regg on May 09, 2021, 03:10:09 pmI 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. ---------------------------------------------------------------------- Graham1 · 2021-05-11 02:34 Quote from: Warfley on May 09, 2021, 04:09:12 pmAlso 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), and that cthreads should preferably be the very first unit (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 where the "Units needed for a multi-threaded application" section states: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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?! ---------------------------------------------------------------------- PascalDragon · 2021-05-11 06:27 Quote from: Graham1 on May 11, 2021, 04:34:33 amSo 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. ---------------------------------------------------------------------- trev · 2021-05-11 06:38 Quote from: PascalDragon on May 11, 2021, 08:27:50 amJoking aside, due to how the units work a sensible order is cmem, cthreads and then cwstrings. What about clocale? Does its order matter too? ---------------------------------------------------------------------- PascalDragon · 2021-05-12 07:06 Quote from: trev on May 11, 2021, 08:38:58 amQuote from: PascalDragon on May 11, 2021, 08:27:50 amJoking 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. ---------------------------------------------------------------------- del · 2021-05-13 04:32 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure BatchFarneback(frameList: string);var  xFarneback: TProcess; begin  xFarneback := TProcess.Create(nil);  xFarneback.Executable:= '/PROJECTS/MiscCpp/BatchFarneback/bin/release/BatchFarneback';  xFarneback.Parameters.Add('-paramfile');  xFarneback.Parameters.Add(frameList);  xFarneback.Options := [poUsePipes, poStderrToOutPut];  xFarneback.Execute;   // blah blah blah ... engaging with output stream ...   xFarneback.Free;