Linking C code with D...

rikki cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 21 23:10:07 PDT 2016


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.


More information about the Digitalmars-d-learn mailing list