getting __DIR__ and __TIME__ of compilation?

Ali Çehreli acehreli at yahoo.com
Fri Dec 27 03:56:07 PST 2013


On 12/27/2013 03:51 AM, Lemonfiend wrote:

 > module main;
 >
 > import std.stdio;
 >
 > enum file = __FILE__;
 > enum time = __TIME__;
 >
 > void main()
 > {
 >      writeln(file);
 >      writeln(time);
 > }
 >
 > This works for me.

And the reason is D's CTFE: Anything that needs to be and can to be 
evaluated at compile time is evaluated at compile time. Since manifest 
constants and the initial values of static constants must be known at 
compile time both enum and 'static const' will work.

However, __FILE__ happens to be the current source file that is being 
compiled but I think the OP wants the current compilation directory. 
Being a C library file, getcwd() does not work at compile time:

import std.stdio;

void main()
{
     import std.path: dirName;

     enum compiledFile = __FILE__;
     writeln(compiledFile);

     static const compileTime = __DATE__ ~ " " ~ __TIME__;
     writeln(compileTime);

     /*
     import std.file: getcwd;
     static const compilationDir = getcwd();
     writeln(compilationDir);

     Error: getcwd cannot be interpreted at compile time, because it has 
no available source code
     */
}

Ali



More information about the Digitalmars-d-learn mailing list