Recent

Author Topic: Why is -Cg forced and cannot be removed on Lazarus?  (Read 1023 times)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1250
  • Professional amateur ;-P
Why is -Cg forced and cannot be removed on Lazarus?
« on: June 23, 2025, 12:15:17 pm »
Hey Y'All,

So, in my quest to suss out why my ncurses programs, when compiled under Lazarus, were misbehaving I FINALLY got it down to one thing:
The -Cg or Generate PIC code is hardcoded in the options and cannot be removed!!!  >:(

I then had a nose around the project options and I found quite the issue!!!

This is what I get when I press the "Show Options" button under Project Options->Compiler Options->Custom Options:
Code: [Select]
/home/gcarreno/FreePascal/fpc/bin/x86_64-linux/fpc.sh
-MObjFPC
-Scghi
-Cg
-O1
-gw3
-gl
-l
-vewnhibq
-Fi../../bin/lib/x86_64-linux
-Fu../../src/application
-Fu../../src/forms
-Fu../../src/base
-Fuforms
-Fu../../src/messages
-Fu../../src/components
-Fu.
-FU../../bin/lib/x86_64-linux
-FE../../bin
-o../../bin/oneform
-FcUTF8

Now, if I press the the "All Options" button on the same thing, a dialog with all the options is shown with appropriate check-boxes and drop-downs for all the options.
I scroll down to find the -Cg option, and to my utmost confusion, it's check-box is NOT SELECTED.
I then select that check-box to add the -Cg option.
Now the contents of the "Show Options" button, looks like this:
Code: [Select]
/home/gcarreno/FreePascal/fpc/bin/x86_64-linux/fpc.sh
-MObjFPC
-Scghi
-Cg
-O1
-gw3
-gl
-l
-vewnhibq
-Fi../../bin/lib/x86_64-linux
-Fu../../src/application
-Fu../../src/forms
-Fu../../src/base
-Fuforms
-Fu../../src/messages
-Fu../../src/components
-Fu.
-FU../../bin/lib/x86_64-linux
-FE../../bin
-o../../bin/oneform
-FcUTF8
-Cg

Do you see my confusion?!?!? There are now 2, TWO entries on that dialog that contain -Cg.
And if I deselect the -Cg check-box, we revert to the first example!!!

So... My question is:
Why is -Cg being hardcoded ??
Especially when there's an entry on the "All Options" dialog that is NOT SELECTED!!!

This makes no sense at all and it's giving a huge pain in the tuchus on my ncurses projects!!!

Can someone please explain why this has been done this way?

I'm quite sorry if I'm coming out a bit unhinged, but this has been a thorn in my brain for the past 3 days, and now that I know what's the issue, it's even more frustrating !!!
I apologise in advance if my anger is coming through and I'm being a bit disrespectful!!  O:-)

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Thaddy

  • Hero Member
  • *****
  • Posts: 17414
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: Why is -Cg forced and cannot be removed on Lazarus?
« Reply #1 on: June 23, 2025, 12:20:05 pm »
-Cg- will override any hardcoded option from the command line. -Cg- with minus sign.
Must appear after -Cg of course.
Usually PIC code is what you want on platforms that support it, only maybe not while debugging.
« Last Edit: June 23, 2025, 12:23:11 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1250
  • Professional amateur ;-P
Re: Why is -Cg forced and cannot be removed on Lazarus?
« Reply #2 on: June 23, 2025, 12:28:00 pm »
Hey Thaddy,

-Cg- will override any hardcoded option from the command line. -Cg- with minus sign.
Must appear after -Cg of course.

Yeap, I got that part from another discussion about ncurses and the same issue I'm having with the borders not working using ACS_* functions.

And yes, if I add the -Cg- to my "Custom Options", it all works as intended.

But you haven't answered my question: Why is it hardcoded? And WHY is there no way to manage that via the User Interface?
Also: Is there any special reason why enforcing the "Generate PIC code" has been thought as mandatory?

If I'm doing a framework, that kinda relies on using the default options on Lazarus, but due to this issue I have to specify in some sort of documentation that one has to turn it off with a hack, that is a bit troublesome, no?

Just my 2c while being a bit hotheaded about it, sorry!!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Thaddy

  • Hero Member
  • *****
  • Posts: 17414
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: Why is -Cg forced and cannot be removed on Lazarus?
« Reply #3 on: June 23, 2025, 12:31:16 pm »
I don't like hard coded too. All I can say. It is not hard coded in the sense of pascal code. It seems an IDE issue specific to ncurses.
If I use it with Geany, everything works fine.
« Last Edit: June 23, 2025, 12:37:06 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1250
  • Professional amateur ;-P
Re: Why is -Cg forced and cannot be removed on Lazarus?
« Reply #4 on: June 23, 2025, 12:36:21 pm »
Hey Thaddy,

I don't like hard coded too. All I can say.

Thanks for that!! And I do really appreciate you saying that, REALLY !!!

On the verge of being frantic, may I ask if you're aware of the decision to make it so?
Cuz I have the faintest memory that it was something one could toggle somewhere in the Project Options, and now I can't seem to find it.

Nonetheless, and again, many thanks for the kind words. I think you were able to defuse most of my anger !!  :-*

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

PascalDragon

  • Hero Member
  • *****
  • Posts: 6035
  • Compiler Developer
Re: Why is -Cg forced and cannot be removed on Lazarus?
« Reply #5 on: June 23, 2025, 02:30:26 pm »
It is much more likely that user ask for the solution of a problem that leads to "you need to enable PIC" than the other way round (especially when libraries are involved), so it's better to enable that by default and deal with the less common issues. Though I don't know why Lazarus doesn't provide that as a toggleable option (if it indeed doesn't and you didn't just miss it somewhere 😉).

n7800

  • Sr. Member
  • ****
  • Posts: 399
Re: Why is -Cg forced and cannot be removed on Lazarus?
« Reply #6 on: June 23, 2025, 06:33:36 pm »
The -Cg or Generate PIC code is hardcoded in the options and cannot be removed!!!  >:(

I don't have the "-Cg" option in the "Show Options" window. I checked it in two OS.

What version of Lazarus and FPC do you have? Did you install them via the installer or FpcUpDeluxe?

If you create a new empty project, is this option there?

n7800

  • Sr. Member
  • ****
  • Posts: 399
Re: Why is -Cg forced and cannot be removed on Lazarus?
« Reply #7 on: June 23, 2025, 06:42:42 pm »
This is what I get when I press the "Show Options" button under Project Options->Compiler Options->Custom Options:
...
Now, if I press the the "All Options" button on the same thing, a dialog with all the options is shown with appropriate check-boxes and drop-downs for all the options.

Note that the "Show Options" button is not on the "Compiler Options->Custom Options" page. It's a separate panel at the bottom of the window. If you switch to any other category, this button will remain, since it does not belong to any specific options page.

But the "All Options" button is actually on the "Compiler Options->Custom options" page. And since the "-Cg" option is not in this memo (next to "All Options" button), the checkbox is correctly unchecked.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1250
  • Professional amateur ;-P
Re: Why is -Cg forced and cannot be removed on Lazarus?
« Reply #8 on: June 24, 2025, 08:54:54 am »
Hey PascalDragon,

It is much more likely that user ask for the solution of a problem that leads to "you need to enable PIC" than the other way round (especially when libraries are involved), so it's better to enable that by default and deal with the less common issues. Though I don't know why Lazarus doesn't provide that as a toggleable option (if it indeed doesn't and you didn't just miss it somewhere 😉).

While I see your point, and it's a valid one, I still don't understand why it's hard coded.
Lazarus already comes with a set of default settings. This could be just another one. And in the project options we could turn it off/on at will.
Again, my confusion is with the hard coded not the fact that it's convenient to have it on all the time. Do you understand me now?

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1250
  • Professional amateur ;-P
Re: Why is -Cg forced and cannot be removed on Lazarus?
« Reply #9 on: June 24, 2025, 09:06:34 am »
Hey n7800,

I don't have the "-Cg" option in the "Show Options" window. I checked it in two OS.
That's rather odd !!

What version of Lazarus and FPC do you have? Did you install them via the installer or FpcUpDeluxe?
I've checked all my previous fpcupdeluxe installs, way up to 2.0.10 and they all have the -Cg in them.
And, yes, I only do fpcupdeluxe installs. So maybe I need to check if it's coming from there.

If you create a new empty project, is this option there?
Yes, it is.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

tetrastes

  • Hero Member
  • *****
  • Posts: 666
Re: Why is -Cg forced and cannot be removed on Lazarus?
« Reply #10 on: June 24, 2025, 10:18:06 am »
The -Cg or Generate PIC code is hardcoded in the options and cannot be removed!!!  >:(

I don't have the "-Cg" option in the "Show Options" window. I checked it in two OS.

Was x86_64-linux among them?

n7800

  • Sr. Member
  • ****
  • Posts: 399
Re: Why is -Cg forced and cannot be removed on Lazarus?
« Reply #11 on: June 24, 2025, 12:47:26 pm »
I checked Windows 7 and 8.1. Now I've launched a virtual Ubuntu, and the option is there indeed.

At first I thought that the difference might be in the fpc.cfg files, but I couldn't find anything. In addition, I tried to specifically add the option there, and it still wasn't reflected in the IDE interface.

In the end, I searched the IDE code, and indeed found it. I should have done this right away, but I still thought that the issue was in the compiler settings.

This lines was first added in b78a550572 ("IDE,lazbuild: append -Cg on targets linux, freebsd, openbsd, netbsd, solaris, bug #17412"). It has a lot of makefile changes, the only lines of code are:
Code: Diff  [Select][+][-]
  1. @@ -2798,6 +2798,9 @@ function TBaseCompilerOptions.MakeOptionsString(
  2.      switches := switches + ' -CX';
  3.    if RelocatableUnit and (CurSrcOS='win') then
  4.      switches := switches + ' -WR';
  5. +  if (CurTargetOS='linux') or (CurTargetOS='freebsd') or (CurTargetOS='netbsd')
  6. +  or (CurTargetOS='openbsd') or (CurTargetOS='solaris') then
  7. +    switches := switches + ' -Cg'; // see bug 17412
  8.  
  9.    { Checks }
  10.    tempsw := '';
  11.  

There was a small addition later in 0910e1bd5b ("IDE: checking Makefile.compiled: ignore -Cg on Linux"), with the following comment:
Code: Diff  [Select][+][-]
  1. @@ -562,6 +562,15 @@ function ExtractSearchPathsFromFPCParams(const CompParams: string;
  2.        'B':
  3.          // build clean
  4.          continue;
  5. +      'C':
  6. +        if (StartPos+2<=length(CompParams)) and (CompParams[StartPos+2]='g')
  7. +        and TargetNeedsFPCOptionCG(GetCompiledTargetOS,GetCompiledTargetCPU)
  8. +        then begin
  9. +          // the -Cg parameter is added automatically on Linux, but not in the
  10. +          // Makefile.compiled, because that is platform independent.
  11. +          // -> ignore
  12. +          continue;
  13. +        end;
  14.        end;
  15.      end;
  16.      if Reduced<>'' then
  17.  

By the way, now (in the modern version) the condition has been moved to a separate function and slightly changed (a CPU check has been added):
Code: Pascal  [Select][+][-]
  1. function TargetNeedsFPCOptionCG(TargetOS, TargetCPU: string): boolean;
  2. begin
  3.   Result:= (TargetCPU='x86_64')
  4.     and ((TargetOS='linux') or (TargetOS='freebsd') or (TargetOS='netbsd')
  5.       or (TargetOS='openbsd') or (TargetOS='dragonfly') or (TargetOS='solaris'));
  6. end;
  7.  

Finding the report by issue number was not easy, as it turns out it is not a Lazarus or FPC report, but a CCR from 2010 year:

https://gitlab.com/freepascal.org/lazarus/ccr/-/issues/17412

Personally I didn't understand anything about this option (I'm not a Linux user and not very interested), hope this will help others.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1250
  • Professional amateur ;-P
Re: Why is -Cg forced and cannot be removed on Lazarus?
« Reply #12 on: June 25, 2025, 03:43:44 am »
Hey n7800,

WOW, that was quite the report !!!
I couldn't hope for a better explanation of what happened!!

Thanks you soooooo very much for the leg work. It's really appreciated !!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

 

TinyPortal © 2005-2018