Load dynamic libraries with no hand-written bindings!
Andrej Mitrovic
andrej.mitrovich at gmail.com
Wed Sep 7 15:35:29 UTC 2022
On Wednesday, 7 September 2022 at 13:23:51 UTC, Paul Backus wrote:
> I'd say it's definitely a bug of some kind. This simplified
> example worked for me:
>
> ```
> --- lib.c
> int f(int x) { return x + 1; }
>
> --- app.d
> import lib;
>
> typeof(&__traits(getMember, lib, "f")) fptr;
> pragma(msg, typeof(fptr)); // extern (C) int function(int x)
> ```
Reduced example:
```
--- lib.c
int foo( void );
typedef int Bar(const void *input);
--- main.d
import lib;
import std.meta;
import std.traits;
template FuncType(alias symbol) {
alias FuncType = typeof(&symbol);
}
template GetFunctionList(alias Module) {
alias GetFunctionList = AliasSeq!();
static foreach (idx, member; __traits(allMembers, Module)) {
static if (isFunction!(__traits(getMember, Module,
member))) {
GetFunctionList = AliasSeq!(GetFunctionList,
typeof(&__traits(getMember, Module, member)));
}
}
}
alias X = GetFunctionList!lib;
void main() { }
```
Running:
```
$ dmd lib.c main.d
main.d(14): Error: `extern (C) int(const(void)* input)` is a
`function` definition and cannot be modified
main.d(19): Error: template instance `main.GetFunctionList!(lib)`
error instantiating
```
As mentioned in the previous reply it seems that a function
typedef trips it up. If there was a way to filter it out, that'd
be great.
More information about the Digitalmars-d
mailing list