www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Function names, Lib used from C and D

reply Maximilian Naderer <maximilian.wallinger outlook.com> writes:
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

```d
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

```d
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
Oct 22
parent evilrat <evilrat666 gmail.com> writes:
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. ```d 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. ```d pragma(mangle, "leogfx_" ~ __traits(identifier, pInit)) bool pInit() { ... } ```
Oct 22