Recent

Author Topic: [SOLVED] Is there a macro that captures the name of the current project?  (Read 1329 times)

EganSolo

  • Sr. Member
  • ****
  • Posts: 394
I tried $(ProjectName), but it wasn't recognized.
I'd like to use that macro in defining paths for the compiler.
« Last Edit: August 01, 2025, 01:07:59 am by EganSolo »

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: Is there a macro that captures the name of the current project?
« Reply #1 on: July 30, 2025, 07:28:49 am »
No, you have to do that manually.
One way of doing this is like so:
Code: Pascal  [Select][+][-]
  1. program testproject;
  2. uses prgname; //unit that demo's the availability.
  3. const
  4.   projectname:pchar = 'testproject'; export name 'projectname';
  5. begin
  6. end.
This constant is now visible in any unit, like so:
Code: Pascal  [Select][+][-]
  1. unit prgname;
  2. interface
  3.  var projectname :Pchar external name 'projectname';
  4. implementation
  5. initialization
  6.   writeln(projectname);
  7. end.
  8.  
The reason for the export is that the constant would otherwise not be visible in the units. The exported name is - officially - Case sensitive, as with any other exports, although it does not seem to matter.

Two other options, both more cumbersome:
{$define}
resourcestring (but the resourcestring must be loaded by hand in other units. Or with a hackery trick)
The above is clean and precise.
The reason that I use a PChar is that it is not a refcounted string type. shortstring will likely also work.

Note that this technique would be very easy to implement in the compiler where it can expand to the filename that is already available to the compiler anyway. Maybe a feature request?

People such as Warfely implement this likely in seconds.. (hint, Frederic ;) )
« Last Edit: July 30, 2025, 01:10:11 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

ccrause

  • Hero Member
  • *****
  • Posts: 1083
Re: Is there a macro that captures the name of the current project?
« Reply #2 on: July 30, 2025, 07:42:03 am »
I tried $(ProjectName), but it wasn't recognized.
I'd like to use that macro in defining paths for the compiler.

Assuming you are referring to using macros inside the Lazarus IDE, then look at the wiki page for IDE macros.
I suspect you need to use $NameOnly($(ProjFile)).

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: Is there a macro that captures the name of the current project?
« Reply #3 on: July 30, 2025, 08:04:30 am »
Yes, the Lazarus ide has support for it, but I use often other editors, hence I filed a feature request:
#41348
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: Is there a macro that captures the name of the current project?
« Reply #4 on: July 30, 2025, 03:20:42 pm »
Response from Michael, boils down to this:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. begin
  3. writeln(Paramstr(0));
  4. end.
The Paramstr is also available in units.

I think that is enough? Maybe not for every and all scenario's, e.g. like libraries.
« Last Edit: July 30, 2025, 03:27:29 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

EganSolo

  • Sr. Member
  • ****
  • Posts: 394
Re: Is there a macro that captures the name of the current project?
« Reply #5 on: July 31, 2025, 05:16:55 am »
ccrause: You hit it on the nail. I wanted a macro I could use while setting the compiler path. That does it and thank you for the reference. Much appreciated.

 

TinyPortal © 2005-2018