ImportC and #include files

bachmeier no at spam.net
Fri May 5 22:56:20 UTC 2023


On Thursday, 4 May 2023 at 13:34:25 UTC, jmh530 wrote:
> On Friday, 28 April 2023 at 14:35:25 UTC, bachmeier wrote:
>> [snip]
>> Compiling with dmd:
>>
>> ```
>> dmd test.c -P-I../../gsl-2.7.1 -I/usr/include 
>> -I/usr/lib/x86_64-linux-gnu/ -L-lgsl -L-lgslcblas -L-lm
>> ```
>
> Alright, I built dmd from source on WSL2. I was able to get a 
> simple importc program working, but then I got a little stuck 
> when I try to make it a little more complicated.
>
> What I want to do is the equivalent of `#include <math.h>` from 
> C, but in a D file. I tried using `import math;` with a small 
> program that uses it and compiled `dmd file.d -L-lm` but I get 
> a message that it can't find the math module.
>
> It seemed to do a bit better when I renamed the file to have a 
> .c ending, but then I would have to put the code back in C 
> terms so I get errors related to that. When I fix those and 
> replace the `import math;` with the include, then it works. But 
> this is just compiling the C code with a D compiler. I want to 
> use the C code in a D program.

It would help to have your code. My examples using the R 
standalone math library are somewhat more involved by the nature 
of that library.

Here's a file test.d:

```
import qnorm;
import std.stdio;

void main() {
   writeln(Rf_qnorm5(0.3,1.2, 0.8, 0, 0));
}
```

The equivalent R code is `qnorm(0.3, 1.2, 0.8, FALSE)`.

The `import qnorm` line tells DMD to pull in the code from 
qnorm.c in that same directory. I don't need to add qnorm.c in 
the compilation command, which is

```
dmd test.d -P-I. -P-I../gnuwin32/fixed/h -P-I../include 
-L/usr/lib/libR.so
```

Is DMD doing something with qnorm.c? I don't know, but if I 
comment out `import qnorm;`, I get the error message

```
test.d(5): Error: undefined identifier `Rf_qnorm5`
```

so I assume it's working. Maybe I can come up with a simpler/more 
practical example without the complexity of this library. This is 
the first time ImportC has worked well enough for me to compile C 
files.


More information about the Digitalmars-d mailing list