ImportC define attribute

ryuukk_ ryuukk.dev at gmail.com
Sat Aug 24 15:50:14 UTC 2024


D makes it possible to import C files transparently and "it just 
works"

According to walter, users should be doing this when wanting to 
pass a version conditional to C files:

`dmd -i -version=ENABLE_X -P=-DENABLE_X app.d`

I find this just bad, i shouldn't have to repeat anything, it is 
error prone and makes build scripts an unreadable mess, specially 
if you have multiple -version

One other suggestion was to split my files.. worse..


Well then, how are other languages managing that?

Zig for example:

```zig
const c = @cImport({
     if (X) {
         @cDefine("ENABLE_X");
     }
     @cInclude("lib.h");
});
```

Wow very nice, they thought about it beforehand, they gives you 
control so it can be nicely integrated with your other Zig code! 
great, no wonder it is a popular language, it has nice UX

D should match that great user experience to avoid walter's bad 
status quo

What about an attribute?


```D
version(X) @cDefine("ENABLE_X")
import lib;
```

API would be:
```D
struct cdefine
{
     string key;
     string value; // optional
}
```

What do you think?


More information about the dip.ideas mailing list