exern (C) linkage problem

Charles Hixson charleshixsn at earthlink.net
Sun Jul 18 13:08:57 PDT 2010


I'm trying to link a C routine to a D program, passing string 
parameters, but I keep getting segmentation errors.
As you can see, these are simple test routines, so the names don't 
reflect current status, but merely where I intend to arrive...but I've 
hit severe roadblocks.
(FWIW, I've tried including -fpic in the gcc command, and it didn't 
appear to make any difference.)

Makefile:
biblio: biblio.d sqlitebase.o
	dmd	biblio.d sqlitebase.o -ofbiblio

sqlitebase.o: sqlitebase.c sqlitebase.h
	gcc -c sqlitebase.c

biblio.d:
import   std.stdio;

//extern (C)   void   dbdefine (char[] str);
extern (C)   void   dbdefine (char[] inStr, ref char[255] outStr);

void   main()
{  char[255]   retVal;
    char[]   msg   =   cast(char[])"Hello from C\0";
    dbdefine   (msg, retVal);
    writeln ("Hello, World");
}

sqlitebase.h:

//void   dbdefine (char str[]);
void   dbdefine (char inStr[], char outStr[255]);

sqlitebase.c:

#include   "sqlitebase.h"

//void   dbdefine (char str[])
void   dbdefine (char inStr[], char outStr[255])
{   //int   i   =   0;
    //while (str[i] != 0)   i++;
    //printStr   (i, str);
    //^^--segmentation fault--^^
    //   printf ("%s/n", str);
    //^^--warning: incompatible implicit declaration of built-in 
function ‘printf’--^^
    //int   i   =   str[0];
    //putchar(i);
    //^^--segmentation fault--^^
    int   i   =   -1;
    while (++i < 255)
    {   if (inStr[i] == 0)   break;
       outStr[i]   =   inStr[i];
    }

}


More information about the Digitalmars-d-learn mailing list