www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - pragma(mangle_prefix, "myclib_")

reply ryuukk_ <ryuukk.dev gmail.com> writes:
Due to lack of proper namespacing in C, library authors prefix 
their symbols with the name of the library

Example: 
https://github.com/floooh/sokol/blob/master/sokol_gfx.h#L2471 
(large file)

```C
SOKOL_GFX_API_DECL void sg_setup(const sg_desc* desc);
SOKOL_GFX_API_DECL void sg_shutdown(void);
SOKOL_GFX_API_DECL bool sg_isvalid(void);
SOKOL_GFX_API_DECL void sg_reset_state_cache(void);
SOKOL_GFX_API_DECL sg_trace_hooks sg_install_trace_hooks(const 
sg_trace_hooks* trace_hooks);
SOKOL_GFX_API_DECL void sg_push_debug_group(const char* name);
SOKOL_GFX_API_DECL void sg_pop_debug_group(void);
```

Would be cool to be able to do something like this:


```D
module sokol;

pragma(mangle_prefix, "sg_"):
extern(C):

void setup(const(desc)* desc);
void shutdown();
bool isvalid();
void reset_state_cache();
trace_hooks install_trace_hooks(const trace_hooks* trace_hooks);
void push_debug_group(const(char)* name);
void pop_debug_group();
```

And use them this way:

```D
import sg = sokol; // i love this D feature, so nice to use

void main()
{
     sg.desc description;

     sg.setup(&description);

     sg.shutdown();

}
```

This way we get a nice way to contain C functions into their own 
scope without polluting the global scope

Got the idea from: 
https://odin-lang.org/docs/overview/#linking-and-foreign-attributes

If nobody finds it bad, then i can try to implement it myself, 
that doesn't look as intimidating to do as the .Enum dip
Jul 22 2022
parent Tim <tim.dlang t-online.de> writes:
On Friday, 22 July 2022 at 22:47:35 UTC, ryuukk_ wrote:
 Due to lack of proper namespacing in C, library authors prefix 
 their symbols with the name of the library

 Example: 
 https://github.com/floooh/sokol/blob/master/sokol_gfx.h#L2471 
 (large file)

 ```C
 SOKOL_GFX_API_DECL void sg_setup(const sg_desc* desc);
 SOKOL_GFX_API_DECL void sg_shutdown(void);
 SOKOL_GFX_API_DECL bool sg_isvalid(void);
 SOKOL_GFX_API_DECL void sg_reset_state_cache(void);
 SOKOL_GFX_API_DECL sg_trace_hooks sg_install_trace_hooks(const 
 sg_trace_hooks* trace_hooks);
 SOKOL_GFX_API_DECL void sg_push_debug_group(const char* name);
 SOKOL_GFX_API_DECL void sg_pop_debug_group(void);
 ```

 Would be cool to be able to do something like this:


 ```D
 module sokol;

 pragma(mangle_prefix, "sg_"):
 extern(C):

 void setup(const(desc)* desc);
 void shutdown();
 bool isvalid();
 void reset_state_cache();
 trace_hooks install_trace_hooks(const trace_hooks* trace_hooks);
 void push_debug_group(const(char)* name);
 void pop_debug_group();
 ```
This could be useful. An even more flexible approach could be to allow a template, which generates the mangling for a symbol, like this: ```D template MangleCallback(alias Symbol) { enum MangleCallback = "myclib_" ~ __traits(identifier, Symbol); } pragma(mangle_callback, MangleCallback): extern(C): void f(); ``` This could be useful for libraries like OpenGL, which use suffixes with type information. Functions like glUniform1ui and glUniform4uiv could then use the same overload in D, but the mangle callback template would generate the correct name in C. Maybe it would even be possible to implement the mangling for extern(C++) in druntime instead of dmd with this.
Jul 23 2022