Recent

Author Topic: Can I mix pascal and C together?  (Read 1510 times)

regg

  • Newbie
  • Posts: 1
Can I mix pascal and C together?
« 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.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2843
Re: Can I mix pascal and C together?
« Reply #1 on: May 09, 2021, 03:41:42 pm »
Start off looking at the h2pas utility.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Warfley

  • Sr. Member
  • ****
  • Posts: 451
Re: Can I mix pascal and C together?
« Reply #2 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)

PascalDragon

  • Hero Member
  • *****
  • Posts: 3170
  • Compiler Developer
Re: Can I mix pascal and C together?
« Reply #3 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.

Blade

  • Full Member
  • ***
  • Posts: 113
Re: Can I mix pascal and C together?
« Reply #4 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.

Graham1

  • New Member
  • *
  • Posts: 17
Re: Can I mix pascal and C together?
« Reply #5 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), 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][+][-]
  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?!
Windows 10 Home 64-bit
Lazarus 2.0.12 / FPC 3.2.0

PascalDragon

  • Hero Member
  • *****
  • Posts: 3170
  • Compiler Developer
Re: Can I mix pascal and C together?
« Reply #6 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.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1492
  • Former Delphi 1-7, 10.2 user
Re: Can I mix pascal and C together?
« Reply #7 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?
Lazarus 2.1 r65061 FPC 3.3.1 r49223 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.3 r65500 FPC 3.3.1 r49634 macOS 11.5 aarch64 Xcode 12.5.1
Lazarus 2.1 r61574 FPC 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 FPC 3.2.0 Win10 Parallels VM

PascalDragon

  • Hero Member
  • *****
  • Posts: 3170
  • Compiler Developer
Re: Can I mix pascal and C together?
« Reply #8 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.

del

  • Full Member
  • ***
  • Posts: 214
Re: Can I mix pascal and C together?
« Reply #9 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;
« Last Edit: May 13, 2021, 02:38:58 pm by del »

 

TinyPortal © 2005-2018