Forcing my module to be initialized first

dan dan.hitt at gmail.com
Mon Oct 16 18:28:52 UTC 2023


On Monday, 16 October 2023 at 10:23:54 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
> Okay, after looking at gtkd, I don't think this can be solved 
> with module constructors or swapping out to lazy initialization.
>
> One way that might work however is to use a crt_constructor as 
> that runs before the D stuff. However it would now hard code 
> your program to your system. Over all my suspicion is that 
> there is something wrong with your system related to library 
> lookup paths and that should be fixed instead.
>
> ```d
> void main() {
>     import std.stdio;
>     import std.process;
>     writeln(environment["GTK_BASEPATH"]);
> }
>
> pragma(crt_constructor) extern(C) void myEnvironmentVarSetter() 
> {
>     import core.sys.posix.stdlib : putenv;
>     putenv(cast(char*)"GTK_BASEPATH=~/bin/gtk".ptr);
> }
> ```

Really awesome, Rikki, your code does the trick.  Thank you so 
much for your pragma.


Since you are so right about the code, maybe you are also right 
about my system.  For reference, i'll describe it in case anybody 
else somehow wanders into the same situation.

My system is a Mac Catalina (OSX 10.15).

I installed the latest dmd i could find, directly from the 
Digital Mars website.  This was so that i could use any ports 
system (macports, fink, or brew) and keep the same d compiler.  
The dmd compiler is in /usr/local/bin/dmd, and the files are in 
some standard location 
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/.....

I installed the gtk stuff (but not gtkd) from macports, and that 
all went into /opt/local, which is where macports puts things.

I believe that gtkd does not have a 'configure' script, so i 
directly edited the top level GNUMakefile in one spot, to specify 
a prefix; my changed line was
prefix?=/opt/some-particular-path-distinct-from-local-and-all-others

I wanted the installation of gtkd to be in some parallel location 
so that there could be multiple versions if needed, or it could 
be removed if needed without any chance of disturbing macports or 
anything else.

It built ok, and the demo worked ok (setting GTK_BASEPATH of 
course, per gtkd's README.md).  Note that i built it on an 
account for which the PATH has /opt/local/bin and /opt/local/sbin 
at the start.  So when gtkd was under construction, it knew just 
where to find all the gtk files.

Thanks again for pointing out the crt_constructor pragma.

(Now, i still think that when module initialization order is not 
forced, it should be something a programmer or systems integrator 
can choose, but i don't want to be too greedy.)

Thanks again for your help!!

dan


More information about the Digitalmars-d-learn mailing list