Thread overview
How to use ImportC?
Mar 03, 2022
Leonardo
Mar 03, 2022
forkit
Mar 04, 2022
bachmeier
Mar 04, 2022
Leonardo
Mar 04, 2022
bachmeier
Mar 04, 2022
MoonlightSentinel
Mar 04, 2022
bachmeier
Mar 18, 2022
Leonardo
Mar 07, 2022
dangbinghoo
March 03, 2022

I saw the new feature called ImportC, it's cool to be able to use C code/libraries, but I'm not much experience in C and didn't understand this incomplete documentation: https://dlang.org/spec/importc.html
How to use ImportC?

March 03, 2022
On Thursday, 3 March 2022 at 19:05:22 UTC, Leonardo wrote:
> I saw the new feature called ImportC, it's cool to be able to use C code/libraries, but I'm not much experience in C and didn't understand this incomplete documentation: https://dlang.org/spec/importc.html
> How to use ImportC?

I think you nailed it... with "incomplete".
March 04, 2022

On Thursday, 3 March 2022 at 19:05:22 UTC, Leonardo wrote:

>

I saw the new feature called ImportC, it's cool to be able to use C code/libraries, but I'm not much experience in C and didn't understand this incomplete documentation: https://dlang.org/spec/importc.html
How to use ImportC?

You just add the C files like D files when you compile and import them in your D code as you would a D module. At least that is all I've been doing. Simple example:

foo.c:

double twice(double x) {
  return 2.0*x;
}

program.d:

import foo;
import std.stdio;

void main() {
  writeln(twice(6.8));
}

Compilation:

dmd program.d foo.c
March 04, 2022

Thanks but not worked here.

[leonardo@leonardo-pc dimportc]$ dmd --version
DMD64 D Compiler v2.098.1

Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved written by Walter Bright
[leonardo@leonardo-pc dimportc]$ ls
foo.c  program.d
[leonardo@leonardo-pc dimportc]$ cat foo.c
double twice(double x) {
  return 2.0*x;
}
[leonardo@leonardo-pc dimportc]$ cat program.d
import foo;
import std.stdio;

void main() {
  writeln(twice(6.8));
}

[leonardo@leonardo-pc dimportc]$ dmd program.d foo.c
/usr/include/dlang/dmd/core/stdc/stdio.d(1256): Error: function `core.stdc.stdio.vfprintf` `pragma(printf)` functions must be `extern(C) int vfprintf([parameters...], const(char)*, va_list)`
/usr/include/dlang/dmd/core/stdc/stdio.d(1259): Error: function `core.stdc.stdio.vfscanf` `pragma(scanf)` functions must be `extern(C) int vfscanf([parameters...], const(char)*, va_list)`
/usr/include/dlang/dmd/core/stdc/stdio.d(1262): Error: function `core.stdc.stdio.vsprintf` `pragma(printf)` functions must be `extern(C) int vsprintf([parameters...], const(char)*, va_list)`
/usr/include/dlang/dmd/core/stdc/stdio.d(1265): Error: function `core.stdc.stdio.vsscanf` `pragma(scanf)` functions must be `extern(C) int vsscanf([parameters...], const(char)*, va_list)`
/usr/include/dlang/dmd/core/stdc/stdio.d(1268): Error: function `core.stdc.stdio.vprintf` `pragma(printf)` functions must be `extern(C) int vprintf([parameters...], const(char)*, va_list)`
/usr/include/dlang/dmd/core/stdc/stdio.d(1271): Error: function `core.stdc.stdio.vscanf` `pragma(scanf)` functions must be `extern(C) int vscanf([parameters...], const(char)*, va_list)`
/usr/include/dlang/dmd/core/stdc/stdio.d(1485): Error: function `core.stdc.stdio.vsnprintf` `pragma(printf)` functions must be `extern(C) int vsnprintf([parameters...], const(char)*, va_list)`

March 04, 2022

On Friday, 4 March 2022 at 01:30:00 UTC, Leonardo wrote:

>

Thanks but not worked here.

[leonardo@leonardo-pc dimportc]$ dmd --version
DMD64 D Compiler v2.098.1

Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved written by Walter Bright
[leonardo@leonardo-pc dimportc]$ ls
foo.c  program.d
[leonardo@leonardo-pc dimportc]$ cat foo.c
double twice(double x) {
  return 2.0*x;
}
[leonardo@leonardo-pc dimportc]$ cat program.d
import foo;
import std.stdio;

void main() {
  writeln(twice(6.8));
}

[leonardo@leonardo-pc dimportc]$ dmd program.d foo.c
/usr/include/dlang/dmd/core/stdc/stdio.d(1256): Error: function `core.stdc.stdio.vfprintf` `pragma(printf)` functions must be `extern(C) int vfprintf([parameters...], const(char)*, va_list)`
/usr/include/dlang/dmd/core/stdc/stdio.d(1259): Error: function `core.stdc.stdio.vfscanf` `pragma(scanf)` functions must be `extern(C) int vfscanf([parameters...], const(char)*, va_list)`
/usr/include/dlang/dmd/core/stdc/stdio.d(1262): Error: function `core.stdc.stdio.vsprintf` `pragma(printf)` functions must be `extern(C) int vsprintf([parameters...], const(char)*, va_list)`
/usr/include/dlang/dmd/core/stdc/stdio.d(1265): Error: function `core.stdc.stdio.vsscanf` `pragma(scanf)` functions must be `extern(C) int vsscanf([parameters...], const(char)*, va_list)`
/usr/include/dlang/dmd/core/stdc/stdio.d(1268): Error: function `core.stdc.stdio.vprintf` `pragma(printf)` functions must be `extern(C) int vprintf([parameters...], const(char)*, va_list)`
/usr/include/dlang/dmd/core/stdc/stdio.d(1271): Error: function `core.stdc.stdio.vscanf` `pragma(scanf)` functions must be `extern(C) int vscanf([parameters...], const(char)*, va_list)`
/usr/include/dlang/dmd/core/stdc/stdio.d(1485): Error: function `core.stdc.stdio.vsnprintf` `pragma(printf)` functions must be `extern(C) int vsnprintf([parameters...], const(char)*, va_list)`

They must have introduced a bug. Works for me with DMD 2.098.0 and LDC 1.28.0. ImportC is under heavy development right now. Most of the 2.098.1 changelog was related to ImportC.

March 04, 2022

On Friday, 4 March 2022 at 01:30:00 UTC, Leonardo wrote:

>

Thanks but not worked here.

[leonardo@leonardo-pc dimportc]$ dmd --version
DMD64 D Compiler v2.098.1

Please retry with the beta or nightly build. I think your bug was already fixed since 2.098.1.

March 04, 2022

On Friday, 4 March 2022 at 17:17:17 UTC, MoonlightSentinel wrote:

>

On Friday, 4 March 2022 at 01:30:00 UTC, Leonardo wrote:

>

Thanks but not worked here.

[leonardo@leonardo-pc dimportc]$ dmd --version
DMD64 D Compiler v2.098.1

Please retry with the beta or nightly build. I think your bug was already fixed since 2.098.1.

Just had a chance to try this. Works with the beta.

March 07, 2022

On Friday, 4 March 2022 at 01:30:00 UTC, Leonardo wrote:

>

Thanks but not worked here.

[leonardo@leonardo-pc dimportc]$ dmd --version
DMD64 D Compiler v2.098.1

Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved written by Walter Bright
[leonardo@leonardo-pc dimportc]$ ls

...

>

/usr/include/dlang/dmd/core/stdc/stdio.d(1485): Error: function core.stdc.stdio.vsnprintf pragma(printf) functions must be extern(C) int vsnprintf([parameters...], const(char)*, va_list)

LDC works with DMD fronted the same version v2.098.1

LDC - the LLVM D compiler (1.28.1):
based on DMD v2.098.1 and LLVM 13.0.1

March 18, 2022

On Friday, 4 March 2022 at 17:17:17 UTC, MoonlightSentinel wrote:

>

On Friday, 4 March 2022 at 01:30:00 UTC, Leonardo wrote:

>

Thanks but not worked here.

[leonardo@leonardo-pc dimportc]$ dmd --version
DMD64 D Compiler v2.098.1

Please retry with the beta or nightly build. I think your bug was already fixed since 2.098.1.

Thank you all. In v2.099.0 it works.