Problem with D calling C.

Bob Cowdery bob at bobcowdery.plus.com
Mon Jan 3 11:52:32 PST 2011


Hi all,
I have a strange problem with D calling C.  I have written a small C
wrapper to the speex codec. In my application I was getting a
segmentation fault. Having done some tests if I test my wrapper from a
simple C main it works. If I test it from an equally simple D main it
seg faults. I have lots of other C code in my app which appears to work
fine. I've added the two blocks of calling code below and attached the
wrapper (hope that's allowed). It's all only test code at present so
forgive the mess.

Environment is Ubuntu 10.04, DMD Compiler V 2.050, and GCC for the C code.

'C' main
#include <stdio.h>
#include <stdlib.h>

extern int lib_speex_init();
extern int lib_speex_term();
extern int lib_speex_get_frame_sz();
extern int lib_speex_encode(short *samples, char*encoded_bytes);

int main()
{
    short smpls[640];
    char bytes[500];
    int r;
    lib_speex_init();
    r = lib_speex_encode(smpls, bytes);
    printf("Bytes written: %d\n", r);
    return 0;
}

'D' main
import std.stdio;
// In codec.c
extern (C) int lib_speex_init();
extern (C) int lib_speex_resample(short* smpls, int* in_sz, short*
resmpls, int* out_sz);
extern (C) int lib_speex_get_frame_sz();
extern (C) int lib_speex_encode(short *samples, char* encoded_bytes);

int main(char[][] args)
{
    short smpls[640];
    char bytes[500];
    int r;
    lib_speex_init();
    r = lib_speex_encode(cast(short*)smpls, cast(char*)bytes);
    writeln("Bytes written: ", r);
    return 0;
}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: DcSdr.zip
Type: application/x-zip-compressed
Size: 1234 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20110103/d3b50881/attachment.bin>


More information about the Digitalmars-d-learn mailing list