Thread overview
Error: cannot use non-constant CTFE pointer in an initializer `cast(immutable(char)*)TEST`
Jun 11, 2022
test123
Jun 11, 2022
Tejas
Jun 11, 2022
Tejas
June 11, 2022

how to work this around.

__gshared const TEST = import(`onlineapp.d`);
extern(C) void main(){
 	__gshared  bin_ptr = TEST.ptr;
}
dmd2 -betterC -J. onlineapp.d
onlineapp.d(3): Error: cannot use non-constant CTFE pointer in an initializer `cast(immutable(char)*)TEST`

June 11, 2022

On Saturday, 11 June 2022 at 10:07:46 UTC, test123 wrote:

>

how to work this around.

__gshared const TEST = import(`onlineapp.d`);
extern(C) void main(){
 	__gshared  bin_ptr = TEST.ptr;
}
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:

__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 :(

June 11, 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.

__gshared const TEST = import(`onlineapp.d`);
extern(C) void main(){
 	__gshared  bin_ptr = TEST.ptr;
}
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:

__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) instead

__gshared immutable(char)* bin_ptr;

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

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

extern(C) void main()
{
}
(base) [tejasgarhewal@fedora D]$ ldc2 -J. -betterC this.d
(base) [tejasgarhewal@fedora D]$ ./this