On Monday, 8 January 2024 at 19:17:06 UTC, Lance Bachmeier wrote:
On Monday, 8 January 2024 at 18:53:47 UTC, Renato wrote:
Is it possible to use C header-only libs from D?

In C, I would need to do this:

```c
#define STB_DS_IMPLEMENTATION
#include "stb_ds.h"
```

The definition must be done in a single C file before including the h file.

I tried this in D:

```d
enum STB_DS_IMPLEMENTATION = 1;
import stb_ds;
```

But it doesn't work. Any suggestions? Perhaps using an intermediate C file to do this would work, but I wanted to know if D can do it.

Without knowing the specifics of what you're trying to do, this automatic translation of C headers to D might be what you want:

I am trying to use a header-only C library. A h file from this project, specifically: https://github.com/nothings/stb

https://forum.dlang.org/post/ugvc3o$5t3$1...@digitalmars.com

The way "header-only" is usually used suggests you should change the file extension to .c and compile it directly.

That doesn't work without the `#define`, as I mentioned. Header libraries like this are designed to only be included once, given that they have impl code, so they rely on the C file defining a variable to explicitly "enable" the impl code to be included.

Based on that thread you've linked, I tried generating a di file from a basic C file that just has the define and include:

```c
#define STB_DS_IMPLEMENTATION
#include "stb_ds.h"
```

But that gets me a seg fault with DMD:

```
▶ dmd -c import_stb_ds.c -Hf=stb_ds.di
[1] 73719 segmentation fault dmd -c import_stb_ds.c -Hf=stb_ds.di
(dmd-2.106.1)
```

LDC also does:

```
▶ ldc2 -c import_stb_ds.c -Hf=stb_ds.di
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it): 0 ldc2 0x000000010f860d87 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39 1 ldc2 0x000000010f85fce6 llvm::sys::RunSignalHandlers() + 198 2 ldc2 0x000000010f8613e0 SignalHandler(int) + 288
3  libsystem_platform.dylib 0x00007ff8115bf37d _sigtramp + 29
4  libsystem_c.dylib        0x00007ff81143772b __sfvwrite + 355
[1] 73787 segmentation fault ldc2 -c import_stb_ds.c -Hf=stb_ds.di
(ldc-1.35.0)
```

Should I report a bug?

Reply via email to