Error: cannot use non-constant CTFE pointer in an initializer `cast(immutable(char)*)TEST`

Tejas notrealemail at gmail.com
Sat Jun 11 12:04:12 UTC 2022


On Saturday, 11 June 2022 at 11:51:43 UTC, Tejas wrote:
> On Saturday, 11 June 2022 at 10:07:46 UTC, test123 wrote:
>> how to work this around.
>>
>> ```d
>> __gshared const TEST = import(`onlineapp.d`);
>> extern(C) void main(){
>>  	__gshared  bin_ptr = TEST.ptr;
>> }
>> ```
>>
>> ```sh
>> dmd2 -betterC -J. onlineapp.d
>> onlineapp.d(3): Error: cannot use non-constant CTFE pointer in 
>> an initializer `cast(immutable(char)*)TEST`
>>
>> ```
>
> Maybe try putting `__gshared  bin_ptr = TEST.ptr;` inside a 
> `shared static this(){}`?
>
> I mean, write it like:
> ```d
> __gshared  bin_ptr;
> shared static this()
> {
>     bin_ptr = TEST.ptr;
> }
>
> __gshared const TEST = import(`onlineapp.d`);
>
>
> extern(C) void main()
> {
> }
> ```
>
> But `bin_ptr` becomes a global in this case :(

Oh wait, `-betterC` is being used ;(

hmm...

Use 
[pragma(crt_constructor)](https://dlang.org/spec/pragma.html#crtctor) instead

```d
__gshared immutable(char)* bin_ptr;

pragma(crt_constructor)
void init_ptr()
{
     bin_ptr = TEST.ptr;
}

__gshared TEST = import("this.d");

extern(C) void main()
{
}
```

```sh
(base) [tejasgarhewal at fedora D]$ ldc2 -J. -betterC this.d
(base) [tejasgarhewal at fedora D]$ ./this
```



More information about the Digitalmars-d-learn mailing list