www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use ImportC to import WebGPU header

reply JN <666total wp.pl> writes:
I would like to use ImportC to automatically import a C header 
into my D project. I've been using Dstep so far which works ok, 
but requires some manual fixes to get the resulting D file to 
compare and it doesn't let me to just drop the C header file.

I created a fresh dub project to try. I try to import this header 
https://github.com/webgpu-native/webgpu-headers/blob/main/webgpu.h . I put
webgpu.h file next to app.d.

My code is:

```
import std.stdio;
import webgpu;

void main()
{
}
```

it builds.

Then I tried to output one of the enums to see if it works:

```
import std.stdio;
import webgpu;

void main()
{
	writeln(WGPUBlendFactor_Dst);
}
```

and it says:

     Starting Performing "debug" build using 
C:\D\dmd2\windows\bin64\dmd.exe for x86_64.
     Building test_importd ~master: building configuration 
[application]
     source\app.d(6,10): Error: undefined identifier 
`WGPUBlendFactor_Dst`
     Error C:\D\dmd2\windows\bin64\dmd.exe failed with exit code 1.

I tried to do the other commandline but I get this error instead:

     C:\Users\haxx\Desktop\dev\test_importd\source>dmd -c webgpu.c 
-Hf=webgpu.di
     
C:\D\dmd2\windows\bin64\..\..\src\druntime\import\importc.h(134): 
fatal error C1034:
     sal.h: no include path set
     Error: C preprocess command C:\Program Files (x86)\Microsoft 
Visual
     
Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX64\x64\cl.exe failed
for file
     webgpu.c, exit status 2
Jan 10
next sibling parent reply Sergey <kornburn yandex.ru> writes:
On Wednesday, 10 January 2024 at 23:36:33 UTC, JN wrote:
 I would like to use ImportC to automatically import a C header 
 into my D project.
It was already done. Use it https://code.dlang.org/packages/wgpu-d Don't reinvent the wheel :)
Jan 11
parent reply Bkoie <bkoie049 gmail.com> writes:
On Thursday, 11 January 2024 at 15:18:08 UTC, Sergey wrote:
 On Wednesday, 10 January 2024 at 23:36:33 UTC, JN wrote:
 I would like to use ImportC to automatically import a C header 
 into my D project.
It was already done. Use it https://code.dlang.org/packages/wgpu-d Don't reinvent the wheel :)
or if you push a package atleast document the code or give example D is not like rust where you can just dive into a lib without docs or example
Jan 12
parent Sergey <kornburn yandex.ru> writes:
On Friday, 12 January 2024 at 11:06:39 UTC, Bkoie wrote:
 On Thursday, 11 January 2024 at 15:18:08 UTC, Sergey wrote:
 On Wednesday, 10 January 2024 at 23:36:33 UTC, JN wrote:
 I would like to use ImportC to automatically import a C 
 header into my D project.
It was already done. Use it https://code.dlang.org/packages/wgpu-d Don't reinvent the wheel :)
or if you push a package atleast document the code or give example D is not like rust where you can just dive into a lib without docs or example
Not my package, but I see it has several separate sub-repos for examples: This package provides sub packages which can be used individually: wgpu-d:enumerate - Enumerate GPU adapters example wgpu-d:headless - Headless (windowless) rendering example wgpu-d:triangle - Triangle (windowed) example wgpu-d:cube - Cube (windowed) example
Jan 12
prev sibling parent ryuukk_ <ryuukk.dev gmail.com> writes:
You need to use a .c file that include it


--- webgpu.c

```c
#include "webgpu.h"

```


--- app.d

```d
import std.stdio;
import webgpu;

void main()
{
     writeln(WGPUBlendFactor_Dst);
}
```


result:

```
$ dmd -run app.d webgpu.c
WGPUBlendFactor_Dst
```
Jan 11