2

I want to build an executable based on a C-File in which Prolog predicates are used. I want to use GNU Prolog.

I succeed (based on the gnu prolog tutorial) to build the examp_c.c, examp.pl:

#include <string.h>
#include <gprolog.h>

PlBool
my_call(PlTerm goal)

{
  PlTerm *arg;
  int functor, arity;
  int result;

  arg = Pl_Rd_Callable_Check(goal, &functor, &arity);
  Pl_Query_Begin(PL_FALSE);
  result = Pl_Query_Call(functor, arity, arg);
  Pl_Query_End(PL_KEEP_FOR_PROLOG);
  return (result == PL_SUCCESS);
}

Compiled using

gplc examp.pl examp_c.c

When calling the executable examp the Prolog interpreter is started / emulated:

GNU Prolog 1.4.4 (64 bits)
Compiled Aug  3 2013, 20:06:22 with gcc
By Daniel Diaz
Copyright (C) 1999-2013 Daniel Diaz
| ?- 

But what I want is to have a C Program internally calling Prolog without communicating with the user via the Prolog interpreter. Either the Prolog predicates should be submitted as arguments of/within the C Program or are internally generated.

Do you know examples for this? Or did I misunderstood something, is it not possible?

You see, I just start as newbie... I would be happy to get some help :-) Many thanks in advance.

kiw
  • 155
  • 6

2 Answers2

1

You can use an initialization/1 directive in the Prolog file to define which query to execute at startup. There's also a --no-top-level linker option for suppressing the top-level interpreter. Check the GNU Prolog documentation for both.

Paulo Moura
  • 18,373
  • 3
  • 23
  • 33
1

You can also directly write a main() in C and then inkove Prolog from C. See Defining a new C main function in the manual.

didou
  • 692
  • 3
  • 7
  • Thanks a lot to you both, @Paulo, too. It works! :-) One last question: using gnuprolog can I produce a library (as a dll on windows) and not only a "exe"-File? Or would this only be possible based on SWI Prolog? – kiw Sep 22 '14 at 07:25