On Friday, 15 October 2021 at 20:45:35 UTC, jfondren wrote:

There's no option, you just use a normal import statement when the module is named .c instead of .d

I say 'just' but typical C uses the C preprocessor and can't be imported as-is.

[ ... ]


First of all, I see you answering questions (not only mine) very often so thanks ;). Now, I tried to do what you said but I'm getting an error. Now I'm just give you the code so you can also check it (if you want of course) and see what's going on. Let's see:

```
// Filename: test.d
import test_c;

void main() {
  hello_world();
}

// Filename: test_og.c
#include <stdio.h>
#include <stdlib.h>

void hello_world() {
  puts("Hello world!!!");
}
```

After that, I'm using: `gcc -E -P test_og.c > test_c.c` to preprocess just like you shown and then I'm using the final command with DMD: `dmd test.d test_c.c` and I'm getting the following error message:

```
/usr/include/stdio.h(246): Error: found `__filename` when expecting `,` /usr/include/stdio.h(247): Error: found `__modes` when expecting `,` /usr/include/stdio.h(252): Error: found `__filename` when expecting `,` /usr/include/stdio.h(253): Error: found `__modes` when expecting `,` /usr/include/stdio.h(254): Error: found `__stream` when expecting `,` /usr/include/stdio.h(304): Error: found `__stream` when expecting `,`
/usr/include/stdio.h(304): Error: found `__buf` when expecting `,`
/usr/include/stdio.h(308): Error: found `__stream` when expecting `,`
/usr/include/stdio.h(308): Error: found `__buf` when expecting `,`
/usr/include/stdio.h(314): Error: found `__stream` when expecting `,`
/usr/include/stdio.h(314): Error: found `__buf` when expecting `,`
/usr/include/stdio.h(326): Error: found `__stream` when expecting `,` /usr/include/stdio.h(327): Error: found `__format` when expecting `,` /usr/include/stdio.h(332): Error: found `__format` when expecting `,`
/usr/include/stdio.h(334): Error: found `__s` when expecting `,`
/usr/include/stdio.h(335): Error: found `__format` when expecting `,`
/usr/include/stdio.h(341): Error: found `__s` when expecting `,`
/usr/include/stdio.h(341): Error: found `__format` when expecting `,` /usr/include/stdio.h(347): Error: found `__format` when expecting `,`
/usr/include/stdio.h(349): Error: found `__s` when expecting `,`
```

Checking the actual source code, it seems like a parsing error. Any ideas?


The next inconvenient thing is: what about when you want a c `#define` in d? Say, fstat's potential errors. You have to smuggle those as well, and because the C preprocessor fights you, you have to not just stuff those people in a box but also prepare new names for them. (And if platforms vary in errors? More build system work.)

```c
// sys_stat_wrapper.c, continued
#include <errno.h>
enum errors {
    ebadf = EBADF,
    eio = EIO,
    eoverflow = EOVERFLOW,
};
```

```d
// fstat.d, continued
    import sys_stat : errors, ebadf;
    writeln(ebadf);
    writeln(errors.eoverflow);
}
```

Of course you can rename when importing as usual, or have a separate .d module that cleans this interface up where the C preprocessor can't interfere.

For function-like `#defines`, perhaps you'll want to write a C function that uses it.

In conclusion, ImportC exists and you can use it, and complications like smuggling structs are discussed in that page. If you're going to wrap a new C library especially, it can take on 99% the tedium for you. If you're going to burn down an importc-by-hand work that you have already and replace it with ImportC, it might be better to wait for dub and gdc to catch up. As annoying as it might be to have C constants in your code, they do compile with fewer build steps.

Agree, having to do it manually will just be a pain in the ass...

Reply via email to