www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - ImportC is becoming usable for some things

reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
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.
Oct 22 2023
parent reply Adam Wilson <flyboynw gmail.com> writes:
On Sunday, 22 October 2023 at 17:29:49 UTC, Imperatorn wrote:
 Let's take a look at a simple example (this example is without 
 using .di-files):
 https://github.com/Mashpoe/c-hashmap

 [...]
Auto-complete works if you can build a DI file out of it.
Oct 24 2023
parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Wednesday, 25 October 2023 at 00:33:03 UTC, Adam Wilson wrote:
 On Sunday, 22 October 2023 at 17:29:49 UTC, Imperatorn wrote:
 Let's take a look at a simple example (this example is without 
 using .di-files):
 https://github.com/Mashpoe/c-hashmap

 [...]
Auto-complete works if you can build a DI file out of it.
Yeah, it works. Would just be nice.
Oct 24 2023