www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Linking C code with D...

reply Era Scarecrow <rtcvb32 yahoo.com> writes:
  While trying to make a benchmark for comparing results of my 
possible compression replacement, I'm finding it quite difficult 
to link the c code with the d code.

  So I've managed to compile the compress.c file using dmc, but 
when I link the obj file to reference it I don't seem to see it...

I've linked a prototype before trying to call it

What am I hopelessly doing wrong?
unittest {
     string haystack = "...";
     auto f1 = (){
         RawBuffer rb;
         compress(rb, haystack);
     };

     auto f2 = (){
         size_t plen;
         extern (C) char *id_compress(char *id, int idlen, size_t 
*plen);
         id_compress(cast(char*) haystack.ptr, haystack.length, 
&plen);
     };

     auto memoryhungry_compress = benchmark!(f1)(100_000);
     auto original_compress = benchmark!(f2)(100_000);
     ...
}

$ dmd compress_c.obj compress.d -g -O -unittest -main

  Error 42: Symbol Undefined 
__D8compress16__unittestL366_5FZ9__lambda2MFZ11id_compressUPaiPkZPa
May 21 2016
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 22/05/2016 6:08 PM, Era Scarecrow wrote:
  While trying to make a benchmark for comparing results of my possible
 compression replacement, I'm finding it quite difficult to link the c
 code with the d code.

  So I've managed to compile the compress.c file using dmc, but when I
 link the obj file to reference it I don't seem to see it...

 I've linked a prototype before trying to call it

 What am I hopelessly doing wrong?
 unittest {
     string haystack = "...";
     auto f1 = (){
         RawBuffer rb;
         compress(rb, haystack);
     };

     auto f2 = (){
         size_t plen;
         extern (C) char *id_compress(char *id, int idlen, size_t *plen);
         id_compress(cast(char*) haystack.ptr, haystack.length, &plen);
     };

     auto memoryhungry_compress = benchmark!(f1)(100_000);
     auto original_compress = benchmark!(f2)(100_000);
     ...
 }

 $ dmd compress_c.obj compress.d -g -O -unittest -main

  Error 42: Symbol Undefined
 __D8compress16__unittestL366_5FZ9__lambda2MFZ11id_compressUPaiPkZPa
Move id_compress out of the unittest block.
May 21 2016
parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Sunday, 22 May 2016 at 06:10:07 UTC, rikki cattermole wrote:
 Move id_compress out of the unittest block.
Well that certainly did it...
May 21 2016