macros in importC

Emmanuel emmankoko519 at gmail.com
Thu Dec 11 10:27:12 UTC 2025


I have spent a lot of time finding "ways" to tightly insert C's 
macro system in importC.

```
#define macro function_calls()
```
maybe a real example in zlib, C programmers are found to write 
code like

```
#define zlibversion zlibVersion()
```
initially, I was trying to do something like this globally
```
auto zlibversion = zlibVersion();
```

`auto` because such functions can return anything any type, which 
we cannot magically know, and then use the variable in our D 
programs. I tested that and it worked for simple local setups for 
ctfe possible functions.

but then, obviously D tries ctfe on the function being a global 
variable initializer. but, C programs like that aren't 
necessarily built for compile time evaluations. the literal text 
replacements are found to sometimes pass runtime bound functions. 
like time functions etc. D's strong type system and using global 
variables for compatibility with that macros looks like an uphill 
battle.

At this point, I have come to the realization that,

we need to go manual this time. I have decided to alias the 
function using the macro as the id.

so alias zlibversion = zlibVersion;

then we manually lookup the library we are building with D's 
importC, and then pass the desired arguments. so if you want to 
use zlibversion in your D program, maybe you have to look up the 
API and pass it to the alias.

in some D program
```
  if (zlibversion() == whatever)
     // do something;
```

Thoughts ???


More information about the Digitalmars-d mailing list