ImportC is becoming usable for some things

Imperatorn johan_forsberg_86 at hotmail.com
Sun Oct 22 17:29:49 UTC 2023


Let's take a look at a simple example (this example is without 
using .di-files):
https://github.com/Mashpoe/c-hashmap

Clone it and put the files somewhere convenient.

Now, let's try using it.

In this example map.c and app.d are located in the same directory.

```d
import std.stdio : writeln;

// Use map.c
import map;

import std.string : toStringz;

alias c = toStringz;

void main()
{
	hashmap* m = hashmap_create();

	string key = "hello";

	hashmap_set(m, key.c, key.length, 400);

	uintptr_t result;

	if (hashmap_get(m, key.c, key.length, &result))
	{
		writeln(key, " = ", cast(int) result);
	}
	else
	{
		writeln("Could not locate entry ", key);
	}
}

```

If you have your paths setup correctly, you can now run

```
dmd map.c app.d
```

dmd has now produced an executable. Run it and see the results.

![ImportC](https://i.ibb.co/dBdtMvx/importc.gif)

Nice. Now if we also could get autocompletion etc it would be 
even better. It's probably possible with some tweaks already, but 
I didn't look into it.


More information about the Digitalmars-d mailing list