Search code examples
cd

C and D communication


I read this in order to do my approach

I have a file: software_pluginInterface.di

Here I declare :

extern (C): void performComputation(char lib[], char func[], void* ptr[], int varNum );
// lib and func will be used later

Then I have the corresponding C file: software_pluginInterface.c, where I declare :

#include "stdio.h"
#include "string.h"

void performComputation(char lib[], char func[], void * v[], int varNum)
{

  printf("there are %d variables \n", varNum);

}

Then I call this with :

performComputation(A, B, V, to!int(v.count()/3));

A, B are '\0' terminated char arrarys V is a void pointer array with 6 elements

So I would expect a output like : there are 2 variables

But I am getting : there are 1557197040 variables

I have 64 bit OS, and I compile it all using (as in the other question)

gcc -c software_pluginInterface.c
dmd software.d software_pluginInterface.o

Then I call them using : ./software

PS: according to this page, D ints are same as C ints.


Solution

  • In D, an array consists of a pointer to some data, and a length, in C++, it's basically just a pointer. This means that your D function should be declared as:

    extern (C): void performComputation(char* lib, char* func, void** ptr, int varNum );