October 22

Dear community,
I want to create a library which is usable from C and D.
D should be a first class citizen and C because of FFI for other languages.
The lib is compiled with -betterC

Because C does not have namespaces i want to add the prefix of the lib like "leogfx" but in D i don't want that because we don't need this.

So i guess i could do either this

struct BufferDesc {
    uint type;
    uint usage;
    size_t size;
    void* ptr;
}

extern(C):

private {
    bool pInit() {
        return true;
    }
}

version(LibUsedFromDlang) {
    bool init() => pInit();
} else {
    bool leogfxInit() => pInit();
}

Or that

struct BufferDesc {
    uint type;
    uint usage;
    size_t size;
    void* ptr;
}

extern(C):

bool leogfxInit() {
    return true;
}

alias init = leogfxInit;

I don't like the first one because I don't like to wrap everything.
I'm not to keen on the second because of the developer experience with intellisense etc.
Is there something I'm missing ? Do I need to go the template route with even worse developer experience ?

Kind regards,
Max

October 22

On Tuesday, 22 October 2024 at 12:44:22 UTC, Maximilian Naderer wrote:

>

Dear community,
I want to create a library which is usable from C and D.
D should be a first class citizen and C because of FFI for other languages.
The lib is compiled with -betterC

Because C does not have namespaces i want to add the prefix of the lib like "leogfx" but in D i don't want that because we don't need this.

extern(C):

private {
bool pInit() {
return true;
}
}

pragma mangle modifies how the symbol name will appear to the linker.

pragma(mangle, "leogfx_init")
bool pInit() {
...
}

additionally IIRC you can even loop it on itself to self modify it, at least I remember doing something like this to keep its name consistent on code refactoring.

pragma(mangle, "leogfx_" ~ __traits(identifier, pInit))
bool pInit() {
...
}