GNU Prolog and C mixing problem

Charles Christophe Thomart <[email protected]>
Newsgroups gmane.comp.gnu.prolog.bugs
Organization AXLOG
Message-ID <[email protected]>
Hello,

I've got some problems when trying to build a GNUProlog engine as a shared 
library.

##### Here's my main program (main.c) :
#include <stdio.h>
#include "libCtoProlog.h"

int main(int argc, char *argv[]) {
    printf("In main!\n");
    doPrologDistribution("distribute", "prologdistribution.dist");
    return 0;
}

##### The library header (libCtoProlog.h) :
#include <stdio.h>
#include <string.h>
#include "gprolog.h"

//void saveDistribution(FILE *outstream, PlTerm *List);
int doPrologDistribution(char *PrologFileName, char *distributionfile);
void testlib(void);

##### The library source code (libCtoProlog.c) :
#include <stdio.h>
#include <string.h>
#include "gprolog.h"
#define MAX_ELEMENT_NUMBER 200

/*
void saveDistribution(FILE *outstream, PlTerm *List) {
    ...
}
*/

int doPrologDistribution(char *PrologFileName, char *distributionfile) {
    int distribute, loadMyFile;
    WamWord args[3];
    PlTerm *solution;
    long solduration = 0;
    Bool res;
//    FILE *outputfile;

    printf("Launching prolog engine...\n");
    printf("Number of directives loaded : %d\n", Start_Prolog(0, NULL)); 

    // Initialisation
    loadMyFile = Find_Atom("consult");
    printf("Launching request...\n");
    Pl_Query_Begin(FALSE);
    args[0] = Mk_String(PrologFileName);
    printf("Arguments : %ld\n", args[0]);

    res = Pl_Query_Call(loadMyFile, 1, args);
    printf("Initialisation check : %d\n", res);
    Pl_Query_End(PL_CUT);

    if(res == 2)
    { //Case of an exception
        printf("Cannot find file %s.pl\n", PrologFileName);
        Stop_Prolog();
        return res;
    }

    distribute = Find_Atom("distribute");

    Pl_Query_Begin(FALSE);
    args[0] = Mk_Variable();
    args[1] = Mk_Variable();

    res = Pl_Query_Call(distribute, 2, args);
    printf("Request result : %d\n", res);

    if(res == 2)
    { //Cas of an exception
        printf("Terms are of type PLV:%d FDV:%d INT:%d FLT:%d ATM:%d LST:%d 
STC:%d\n", PLV, FDV, INT, FLT, ATM, LST, STC);
        printf("Exception of type %d\n", Type_Of_Term(Pl_Get_Exception()));;
        Stop_Prolog();
        return res;
    }

    solution = Rd_List_Check(args[0]);
    solduration = Rd_Integer(args[1]);

    Pl_Query_End(PL_CUT);

    printf("Solution founded with a maximum of %ld\n", solduration);
/*
    outputfile = fopen(distributionfile, "w+t");
    saveDistribution(outputfile, solution);
    fclose(outputfile);
*/

    Stop_Prolog();
    return 0;
}

void testlib(void) {
    printf("Testing library...\n");
}

##### The distribute.pl file could be as :
% Empty file

##### The called distribution.pl file :
distribute([[a, b]], 10).

##### The Makefile :
CC = g++ -g2
PROLOGCOMPILER = gplc

PROLOG_LIBS_BEFORE = -L${GNUPROLOGHOME}/lib ${GNUPROLOGHOME}/lib/obj_begin.o
PROLOG_LIBS_AFTER = ${GNUPROLOGHOME}/lib/all_pl_bips.o 
${GNUPROLOGHOME}/lib/all_fd_bips.o ${GNUPROLOGHOME}/lib/top_level.o 
${GNUPROLOGHOME}/lib/debugger.o -lbips_fd -lengine_fd -lbips_pl 
${GNUPROLOGHOME}/lib/obj_end.o -lengine_pl -llinedit -lm
PROLOG_FLAGS = ${GNUPROLOGHOME}/lib/obj_begin.o 
${GNUPROLOGHOME}/lib/all_pl_bips.o ${GNUPROLOGHOME}/lib/all_fd_bips.o 
${GNUPROLOGHOME}/lib/top_level.o ${GNUPROLOGHOME}/lib/debugger.o 
${GNUPROLOGHOME}/lib/libbips_fd.a  ${GNUPROLOGHOME}/lib/libengine_fd.a 
${GNUPROLOGHOME}/lib/libbips_pl.a ${GNUPROLOGHOME}/lib/obj_end.o 
${GNUPROLOGHOME}/lib/libengine_pl.a ${GNUPROLOGHOME}/lib/liblinedit.a -lm

all: libCtoProlog.so CtoProlog

CtoProlog: libCtoProlog.h distribution.o main.o libCtoProlog.so
	${CC} ${PROLOG_LIBS_BEFORE} \
	 main.o distribution.o -o CtoProlog \
	-L. -lCtoProlog ${PROLOG_LIBS_AFTER}

main.o: main.c
	${CC} -Wall -c $^ -o main.o ${PROLOG_FLAGS}

distribution.o: distribution.pl
	${PROLOGCOMPILER} -c distribution.pl

libCtoProlog.o: libCtoProlog.c
	${CC} -Wall -c $^ `./gprolog.conf --cflags`

libCtoProlog.so: libCtoProlog.o distribution.o
	${CC} -shared -o libCtoProlog.so \
	${PROLOG_LIBS_BEFORE} $^ \
	${PROLOG_LIBS_AFTER}

clean:
	rm -f distribution.o main.o libCtoProlog.o CtoProlog *~
	rm -f libCtoProlog.so*

############################################

The compilation seems to work : no errors, links look ok (with ldd), the 
library has the requested function (nm).
But, when executing, I've got a "Global stack overflow (size 8193Kb)" error 
message, which sounds strange, since there's quite nothing to load.

Using gdb, we see that the libraries are loaded, but the libCtoProlog.so 
hasn't got any start adress.
The testlib() function is there only to test if the library is loaded, and 
seems to work.

In fact, the error occurs during the Start_Prolog() call, or, to be precise, 
just after. This functions returns correctly, but the error appear a bit 
later (you can see it by putting a pause).

Any ideas?
Thanks in advance.

Andalariel.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.