質問

I am trying to call SWI-Prolog from a C++ application.

I am currently trying to run some of the code provided in the official documentation:

#include <SWI-Prolog.h>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    term_t a0 = PL_new_term_refs(2);
    static predicate_t p;

    if ( !p )
    p = PL_predicate("listing", 1, "database");

    PL_put_atom_chars(a0, "member");
    cout << PL_open_query(NULL, PL_Q_NORMAL, p, a0);
    cout << a0;

    return 0;
}

It compiles fine, but when I try to run it, I receive this error:

[FATAL ERROR: PL_new_term_refs(): No foreign environment]

May I know what is the problem? I cannot find anything on Google related to the topic.

Thank you very much.

This is the documentation that I have followed: http://www.swi-prolog.org/pldoc/doc_for?object=section%283,%279.4.9%27,swi%28%27/doc/Manual/foreigninclude.html%27%29%29

役に立ちましたか?

解決

I think you should call PL_initialise(argc, argv) before, but that apart, take a look at the C++ interface. I'm using this last extensively in pqConsole project, and I must say it's really much simpler than the C interface.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top