www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use ImportC?

reply Leonardo <leotada523 gmail.com> writes:
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?
Mar 03 2022
next sibling parent forkit <forkit gmail.com> writes:
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".
Mar 03 2022
prev sibling parent reply bachmeier <no spam.net> writes:
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 ```
Mar 03 2022
parent reply Leonardo <leotada523 gmail.com> writes:
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)`

```
Mar 03 2022
next sibling parent bachmeier <no spam.net> writes:
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.
Mar 03 2022
prev sibling next sibling parent reply MoonlightSentinel <moonlightsentinel disroot.org> writes:
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](https://dlang.org/download.html#dmd_beta) or [nightly build](https://github.com/dlang/dmd/releases/tag/nightly). I think your bug was already fixed since 2.098.1.
Mar 04 2022
next sibling parent bachmeier <no spam.net> writes:
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](https://dlang.org/download.html#dmd_beta) or [nightly build](https://github.com/dlang/dmd/releases/tag/nightly). I think your bug was already fixed since 2.098.1.
Just had a chance to try this. Works with the beta.
Mar 04 2022
prev sibling parent Leonardo <leotada523 gmail.com> writes:
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](https://dlang.org/download.html#dmd_beta) or [nightly build](https://github.com/dlang/dmd/releases/tag/nightly). I think your bug was already fixed since 2.098.1.
Thank you all. In v2.099.0 it works.
Mar 18 2022
prev sibling parent dangbinghoo <dangbinghoo gmail.com> writes:
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
Mar 06 2022