Recent

Author Topic: [SOLVED] How to define a constant based on Lazarus Build Mode ?  (Read 722 times)

440bx

  • Hero Member
  • *****
  • Posts: 5479
Hello,

Consider a program that has two (2) build modes defined in Lazarus "Win64 Debug" and "Win64 Release".

Is it possible to define the value of a compiler constant based on that build mode, e.g,
Code: Pascal  [Select][+][-]
  1. {$if BuildMode = "Win64 Debug"}   // something functionally like this but that works!
  2. const
  3.   MODE = 'Debug';
  4. {$endif}
  5.  

Thank you for your help.
« Last Edit: May 21, 2025, 03:02:37 am by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

440bx

  • Hero Member
  • *****
  • Posts: 5479
Re: How to define a constant based on Lazarus Build Mode ?
« Reply #1 on: May 21, 2025, 02:48:49 am »
After a little more digging, I can answer my own question (which hopefully will be useful to someone in the future.):

Simply declare a compiler define depending on the build mode.  That define can then be tested in the code.

Refer to the attachment for an example.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

ASBzone

  • Hero Member
  • *****
  • Posts: 724
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: [SOLVED] How to define a constant based on Lazarus Build Mode ?
« Reply #2 on: May 21, 2025, 03:59:12 am »
You can even do it more effectively on the "Additions and Overrides" page.


See attached.

-ASB: https://www.BrainWaveCC.com/

Lazarus v4.1.0.0 (c067bd336e) / FreePascal v3.2.3-1411-g8c665e3128 (aka fixes)
(Windows 64-bit install w/Win32 and Linux on ARM and x64 cross-compilers via FpcUpDeluxe)

My Systems: Windows 10/11 Pro x64 (Current)

440bx

  • Hero Member
  • *****
  • Posts: 5479
Re: [SOLVED] How to define a constant based on Lazarus Build Mode ?
« Reply #3 on: May 21, 2025, 04:14:08 am »
Good to know that other option too.

Thank you ASBzone. 
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 17213
  • Ceterum censeo Trump esse delendam
Re: [SOLVED] How to define a constant based on Lazarus Build Mode ?
« Reply #4 on: May 21, 2025, 09:51:51 am »
A better way is
Code: Pascal  [Select][+][-]
  1. {$ifopt D+}// + is debug mode - is release mode
That is better because it does not rely on the Lazarus IDE.
This works in other editors too, since it is compiler configuration based.

If you did not know that, it was not solved, but now it is.....

(I can complicate things on request, but that is not the general issue)

It is all about how your fpc.cfg options are defined.
Has nothing to do with Lazarus.
« Last Edit: May 21, 2025, 10:02:29 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

440bx

  • Hero Member
  • *****
  • Posts: 5479
Re: [SOLVED] How to define a constant based on Lazarus Build Mode ?
« Reply #5 on: May 21, 2025, 10:34:24 am »
I need to know the Lazarus build mode.  Simply knowing the debug mode is insufficient.

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

Thaddy

  • Hero Member
  • *****
  • Posts: 17213
  • Ceterum censeo Trump esse delendam
Re: [SOLVED] How to define a constant based on Lazarus Build Mode ?
« Reply #6 on: May 21, 2025, 10:47:24 am »
I need to know the Lazarus build mode.  Simply knowing the debug mode is insufficient.
Then you can use {$if defined()} // which is the same in this particular case
Lazarus itself does not do anything that is not defined in the config file, except for its own IDE options.
I thought you knew that....

The build mode is simply passed to the compiler. That is not rocket science.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

440bx

  • Hero Member
  • *****
  • Posts: 5479
Re: [SOLVED] How to define a constant based on Lazarus Build Mode ?
« Reply #7 on: May 21, 2025, 10:53:14 am »
The build mode is simply passed to the compiler. That is not rocket science.
The compiler doesn't know what the Lazarus build mode is which is what's needed in this particular case.

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

Thaddy

  • Hero Member
  • *****
  • Posts: 17213
  • Ceterum censeo Trump esse delendam
Re: [SOLVED] How to define a constant based on Lazarus Build Mode ?
« Reply #8 on: May 21, 2025, 10:57:50 am »
Of course it knows.
There are no secret options that Lazarus can send to the compiler.
THINK
This deserves my record number of devils... :P
 >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D
 ;D ;D ;D

Are you being stupid on purpose?
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

440bx

  • Hero Member
  • *****
  • Posts: 5479
Re: [SOLVED] How to define a constant based on Lazarus Build Mode ?
« Reply #9 on: May 21, 2025, 11:29:40 am »
LOL... it's amazing how ridiculous you can get.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

n7800

  • Sr. Member
  • ****
  • Posts: 368
Re: [SOLVED] How to define a constant based on Lazarus Build Mode ?
« Reply #10 on: May 22, 2025, 02:07:29 am »
By the way, "build modes" can be defined at the level of the compiler configuration file. This works for all projects and without the IDE at all. When compiling from the command line, you can specify:

Code: Bash  [Select][+][-]
  1. fpc -dMyBuildMode main.pp
  2.  

and the desired "build mode" will be used.

440bx

  • Hero Member
  • *****
  • Posts: 5479
Re: [SOLVED] How to define a constant based on Lazarus Build Mode ?
« Reply #11 on: May 22, 2025, 02:14:21 am »
By the way, "build modes" can be defined at the level of the compiler configuration file. This works for all projects and without the IDE at all. When compiling from the command line, you can specify:

Code: Bash  [Select][+][-]
  1. fpc -dMyBuildMode main.pp
  2.  

and the desired "build mode" will be used.
Having a Lazarus-independent method can come in very handy.  Thank you for pointing that out @n7800.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018