Subject: [SWIPL] C++ SWI Prolog interface

From: Antoine GALLAND <[email-masked]>
Date: Thu, 04 Oct 2001 14:33:41 +0200

Hi,

I'm trying to interface Prolog code into C or C++ code.
For the moment I just test little examples.

I've tested with success the C++ package "SWI-cpp.h" from
http://www.swi.psy.uva.nl/projects/SWI-Prolog/packages/cpp/
on linux using gcc 2.95.3 and SWI-prolog 4.0.9
But the example is quite simple.

1) Is someone have some code or example using C/C++ code to interface
with SWI prolog compiler (plld).
-> some example with only C/C++ code
-> some example interfacing C++ code AND prolog code (in prolog file
.pl)

2) Here one of my example which doesn't work and I don't understand why
:
in file main.cpp :
-----
#include "SWI-cpp.h"
#include <iostream.h>

int main(int argc, char **argv)
{

  PlEngine e(argv[0]);

  try {

    PlTermv av(1);
    PlCompound compound("hello",PlTermv("world"));
    av [0] = compound;

    PlQuery q("compound", av);
    while( q.next_solution() )
      cout << "\t" << (char *) av[0] << endl;

    if ( PlCall("compound", av) )
      cout << "YES" << endl;
    else
      cout << "NO" << endl;

  } catch ( PlException &ex )
    {
      cerr << (char *) ex << endl;
      exit(1);
    }
}
------

my compilation line is:
plld -o test -ld g++ -goal true main.cpp

------
And here the log of the call : ./test

bash-2.04$ ./test
        hello(world)
test: pl-wam.c:1491: PL_open_query: Assertion `(unsigned
long)((&PL_local_data)->foreign_environment) > (unsigned
long)((&PL_local_data)->environment)' failed.
Aborted

----

* I don't undertand why the query crash, normaly in SWI-prolog you can
write :
?- compound(hello(world)).

Yes

* In fact my problem is more larger : How to construct in C++ big
(complex) term and transfer them to a prolog file queries.
I've test with sucess some call in C++ to a prolog file but when I try
make more complex term, I have always the same exception (mention
before) :

Thank you by advance for your help,

Cordialy,

Antoine Galland