Recent

Author Topic: Has anyone converted the C librarys to FreePascal?  (Read 16498 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Has anyone converted the C librarys to FreePascal?
« Reply #60 on: October 10, 2019, 09:43:30 am »
It is a fact that there are many more libraries written for one thing or another in C than in Pascal.  That said, there are plenty of well implemented libraries for "basic" stuff, such as math, trees, sorts, etc, in Pascal.
I completely agree, but I hope your "basic" stuff is written with a pinch of salt... - know it is -
E.g. said Erik van Bilsen - that wrote Chet - wrote complete video codecs in Pascal! and wrote a series in The Delphi Magazine on that. I have an MP3 codec fully implemented in Pascal (not by me alone!). And well written, easy to use data abstractions are nay absent in C and plentiful in Object Pascal.  Those are not "really" basic... The power of C is that it can do basic stuff really good... :D .... If you have time on hand...
« Last Edit: October 10, 2019, 09:46:34 am by Thaddy »
Specialize a type, not a var.

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Has anyone converted the C librarys to FreePascal?
« Reply #61 on: October 10, 2019, 10:17:51 am »
I hope your "basic" stuff is written with a pinch of salt...
Yes.  Even now, I'm not sure how to characterize the difference between the libraries available in Pascal and C.  Because there are many more written in C, there are some very specialized ones (e.g, "not basic") not available in Pascal.  That said, there are some very specialized libraries written in Pascal, just not as many.

Put another way, hopefully a bit more accurate, the probability (keyword: probability) of finding a very specialized library written in C is higher than finding it written in Pascal.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Has anyone converted the C librarys to FreePascal?
« Reply #62 on: October 10, 2019, 11:04:17 am »
Put another way, hopefully a bit more accurate, the probability (keyword: probability) of finding a very specialized library written in C is higher than finding it written in Pascal.

The probability that something works directly, specially on windows, however is much lower. If I could only could get back the days spent on cygwn/mingw installations, autoconf/make and configure scripts, and debugging makefiles........

Which is why these kind of discussions are ridiculous anyway. They overfocus on language and ignore the rest. But in the rest quite often there are problems too, and maybe even bigger ones. (like C code being *nix only etc)

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Has anyone converted the C librarys to FreePascal?
« Reply #63 on: October 10, 2019, 11:55:48 am »
They overfocus on language and ignore the rest. But in the rest quite often there are problems too, and maybe even bigger ones. (like C code being *nix only etc)
I agree.  Even if the mythical, always perfect, C to Pascal translator existed, such a translator would know the languages but, without the translator taking into consideration the environment/OS the program will run on, it is rather unlikely to produce a perfectly functioning program and,  that doesn't even take into account that C compilers have a number of "implementation defined" behaviors.

That's why I consider translators of one kind or another simply "saves typing" tools, because the programmer has to understand what is at the starting and ending points.  Without that understanding, the result is rather unlikely to be desirable regardless of how or what did the translation/porting.


A suggestion to the OP, focus on learning programming first, Pascal is an excellent choice for that - maybe even the best - worry about libraries later.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Has anyone converted the C librarys to FreePascal?
« Reply #64 on: October 10, 2019, 12:00:29 pm »
I agree.  Even if the mythical, always perfect, C to Pascal translator existed,

It will never exist, since macro expressions are partially defined by usage.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Has anyone converted the C librarys to FreePascal?
« Reply #65 on: October 10, 2019, 12:52:17 pm »
It will never exist, since macro expressions are partially defined by usage.
macro's can be expanded. cpp -E will do that for you, generating huge files, but with proper judgement the expanded macro's can be copied out.
But I agree that still needs (quite a lot, sometimes, manual labor)
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Has anyone converted the C librarys to FreePascal?
« Reply #66 on: October 10, 2019, 01:33:37 pm »
It will never exist, since macro expressions are partially defined by usage.
macro's can be expanded. cpp -E will do that for you, generating huge files, but with proper judgement the expanded macro's can be copied out.

But more important, that means the resulting program is no longer independent of the headers. Which usually means it is useless.
 

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Has anyone converted the C librarys to FreePascal?
« Reply #67 on: October 10, 2019, 04:47:09 pm »
But more important, that means the resulting program is no longer independent of the headers. Which usually means it is useless.
???? I use that for umpteen years: expanded macro's are easier to make into Pascal functions.
For which h2pas creates a prototype...
The advantage is NOT that you can automate it, but you can cut the crap: it makes you see the trees from the wood.
(KHRONOS macro's come to mind as a good anti pattern)
« Last Edit: October 10, 2019, 04:57:44 pm by Thaddy »
Specialize a type, not a var.

del

  • Sr. Member
  • ****
  • Posts: 258
Re: Has anyone converted the C librarys to FreePascal?
« Reply #68 on: October 10, 2019, 06:58:28 pm »
I wouldn't recommend that a newby learn C cuz it's not object oriented. Learn the Free Pascal. If you need small scale stuff, translate it. If you need ffmpeg then you're probably gonna have to get your feet wet with C++. But Free Pascal will give you as much, if not more, of a head start in that direction than would the humbly procedural C. IMHO.

Akira1364

  • Hero Member
  • *****
  • Posts: 561
Re: Has anyone converted the C librarys to FreePascal?
« Reply #69 on: October 10, 2019, 07:22:44 pm »
C definitely does not have particularly "good" libraries, IMO, due to the rather limited overall nature of the language.

C++ (which makes far more sense as a language to compare to Pascal as FPC currently implements it) on the other hand, sure.

As far as the question mark operator people were talking about earlier, it literally just does the same thing as "IfThen".

So this code:

Code: C  [Select][+][-]
  1. const int n = (x != 0) ? 10 : 20;

is basically equivalent to:

Code: Pascal  [Select][+][-]
  1. n := ifthen(x <> 0, 10, 20);

The one advantage of the C version is that it can be used in constant expressions since it's part of the language and not a function, while obviously "IfThen" can't.

That said, the compiler patch by Ryan Joseph for generic constants which is currently awaiting review does make stuff like that possible, which is one of the reasons I'm particularly looking forward to the feature.
« Last Edit: October 10, 2019, 07:31:05 pm by Akira1364 »

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Has anyone converted the C librarys to FreePascal?
« Reply #70 on: October 10, 2019, 07:28:43 pm »
I wouldn't recommend that a newby learn C cuz it's not object oriented.
It's quite likely no one will agree with this but, just to let the OP know that not everyone is a "passenger" on the OOP bandwagon, I would recommend staying away from OOP. 

As I recommended in the first post I made in this thread, get the book "Oh Pascal" by Doug Cooper, no OOP in there and, for someone interested in seeing what a program is supposed to be, get Per Brinch Hansen's "On Pascal Compilers", a beginner will probably have to read it several times before being able to fully appreciate it but, the effort is really worth it.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

del

  • Sr. Member
  • ****
  • Posts: 258
Re: Has anyone converted the C librarys to FreePascal?
« Reply #71 on: October 10, 2019, 07:45:11 pm »
C definitely does not have particularly "good" libraries, IMO, due to the rather limited overall nature of the language.
I just used one this morning for geographical transformations. Love those "structs". It's like Fortran - there's stuff out there. They didn't throw it all away.
 :D

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Has anyone converted the C librarys to FreePascal?
« Reply #72 on: October 11, 2019, 09:23:28 am »
C definitely does not have particularly "good" libraries, IMO, due to the rather limited overall nature of the language.

C++ (which makes far more sense as a language to compare to Pascal as FPC currently implements it) on the other hand, sure.

As far as the question mark operator people were talking about earlier, it literally just does the same thing as "IfThen".

So this code:

Code: C  [Select][+][-]
  1. const int n = (x != 0) ? 10 : 20;

is basically equivalent to:

Code: Pascal  [Select][+][-]
  1. n := ifthen(x <> 0, 10, 20);

The one advantage of the C version is that it can be used in constant expressions since it's part of the language and not a function, while obviously "IfThen" can't.

That said, the compiler patch by Ryan Joseph for generic constants which is currently awaiting review does make stuff like that possible, which is one of the reasons I'm particularly looking forward to the feature.
Even then there is a difference: the ternary operator works like an if-expression that only the part taken is evaluated. The IfThen function itself will never support that. Nor will it any function that can be written in Pascal. This needs a compiler provided functionality (like the IfThen intrinsic that I had added a year or two back, but had to remove again after other devs complained).

Akira1364

  • Hero Member
  • *****
  • Posts: 561
Re: Has anyone converted the C librarys to FreePascal?
« Reply #73 on: October 11, 2019, 08:04:56 pm »
This needs a compiler provided functionality (like the IfThen intrinsic that I had added a year or two back, but had to remove again after other devs complained).

Not sure I remember that. How did it work exactly? And what were the complaints?

Would it have allowed something similar to this, with constant record members, if combined with Ryan's generic constants patch?

Code: C  [Select][+][-]
  1. #include <iostream>
  2.  
  3. template <const auto A, const auto B>
  4. struct Min
  5. {
  6.   static constexpr auto Result = (A < B) ? A : B;
  7. };
  8.  
  9. int main()
  10. {
  11.   std::cout << Min<-9, 5>::Result;
  12. }

Seems like it would have been quite useful if so.

The fact that you can't really express that many things as compile-time constants is basically the one aspect of other languages that I find myself missing in Pascal.

Edit: Just went back and found the mailing list thread... it seems like the intrinsic was basically just an objectively better version of the normal function version?

There's various things you cannot use the normal function version for at all, like this:

Code: Pascal  [Select][+][-]
  1. program Example;
  2.  
  3. {$mode Delphi}
  4.  
  5. uses Classes, SysUtils;
  6.  
  7. const Value = (2 * 3) div 3;
  8.  
  9. begin
  10.   // ↓↓↓ Creates a TObject no matter what, and thus a memory leak!
  11.   // ↓↓↓ Why would anyone *want* it to work like that if it could be avoided with the intrinsic version?
  12.   with IfThen<TObject>(Value = 2, TPersistent.Create, TObject.Create) do begin
  13.     WriteLn(ClassName);
  14.     Free;
  15.   end;
  16. end.
« Last Edit: October 12, 2019, 03:19:25 am by Akira1364 »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Has anyone converted the C librarys to FreePascal?
« Reply #74 on: October 11, 2019, 08:27:57 pm »
But more important, that means the resulting program is no longer independent of the headers. Which usually means it is useless.
???? I use that for umpteen years: expanded macro's are easier to make into Pascal functions.

Yes, but if the macro's are ifdefed (e.g. for differing targets) it won't work. And the times that I tried that was usually the case.

 

TinyPortal © 2005-2018