# Foreign code with PL\_assert

**URL:** <https://swi-prolog.discourse.group/t/foreign-code-with-pl-assert/3987>\
**Category:** Help!\
**Tags:** how-to\
**Created:** [May 25, 2021, 2:49am UTC](https://swi-prolog.discourse.group/t/foreign-code-with-pl-assert/3987 "2021-05-25T02:49:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yourein](https://avatars.discourse-cdn.com/v4/letter/y/a88e57/32.png) [@yourein](https://swi-prolog.discourse.group/u/yourein)\
**Post date:** [May 25, 2021, 2:49am UTC](https://swi-prolog.discourse.group/t/foreign-code-with-pl-assert/3987/1 "2021-05-25T02:49:43Z")

</div>

I’m using: SWI-Prolog version ??? swipl 8.3.19

I want the code to: Want to compile c++ code with Prolog.

But what I’m getting is: Compilation complains that PL\_assert function is not undefined reference. I tried both g++ and swipl-ld as the compiler. They have the same error message.  
If PL\_assert does not work, I will try to call assert in Prolog but why PL\_assert function is not working?

My code looks like this:

```prolog
% your code here
  predicate_t consult = PL_predicate("consult", 1, (const char*)MODULE);
  term_t model = PL_new_term_ref();
  term_t submodel = PL_new_term_ref();
  term_t solver = PL_new_term_ref();
  pls = PL_chars_to_term(CPMODEL.data(), model);
  pls = PL_chars_to_term(CPSUBMODEL.data(), submodel);
  pls = PL_chars_to_term(CPSOLVER.data(), solver);
  pls = PL_call_predicate(MODULE, PL_Q_NORMAL, consult, model);
  pls = PL_call_predicate(MODULE, PL_Q_NORMAL, consult, submodel);
  pls = PL_call_predicate(MODULE, PL_Q_NORMAL, consult, solver);
  term_t lb_term = PL_new_term_ref();
  term_t ub_term = PL_new_term_ref();
  term_t nbstates_term = PL_new_term_ref();
  term_t lbval = PL_new_term_ref();
  term_t ubval = PL_new_term_ref();
  term_t nbstatesval = PL_new_term_ref();
  pls = PL_put_integer(lbval, lb);
  pls = PL_put_integer(ubval, ub);
  pls = PL_put_integer(nbstatesval, nb_states);
  functor_t lb_f = PL_new_functor(PL_new_atom("lb"), 1);
  functor_t ub_f = PL_new_functor(PL_new_atom("ub"), 1);
  functor_t nbstates_f = PL_new_functor(PL_new_atom("nbstates"), 1);
  pls = PL_cons_functor(lb_term, lb_f, lbval);
  pls = PL_cons_functor(ub_term, ub_f, ubval);
  pls = PL_cons_functor(nbstates_term, nbstates_f, nbstatesval);
  pls = PL_assert(lb_term, MODULE, PL_ASSERTZ);
  pls = PL_assert(ub_term, MODULE, PL_ASSERTZ);
  pls = PL_assert(nbstates_term, MODULE, PL_ASSERTZ);

```

---

<div class="post-metadata">

**Author:** ![yourein](https://avatars.discourse-cdn.com/v4/letter/y/a88e57/32.png) [@yourein](https://swi-prolog.discourse.group/u/yourein)\
**Post date:** [May 25, 2021, 4:42am UTC](https://swi-prolog.discourse.group/t/foreign-code-with-pl-assert/3987/2 "2021-05-25T04:42:02Z")

</div>

I just got around the problem by calling assert in Prolog side. So this is no longer a problem. But I am still curious about PL\_assert. I looked into the documentation about the explanation of the function, but is it not implemented? Or is the document obsolete?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [May 25, 2021, 7:00am UTC](https://swi-prolog.discourse.group/t/foreign-code-with-pl-assert/3987/3 "2021-05-25T07:00:52Z")

</div>

You’re not saying what “not works” means. Crash? What else? One issue seems that `MODULE` is a char\* while PL\_assert() accepts a module handle. As not all declarations are in place it is hard to tell. Surely PL\_assert() works. If it doesn’t for you, make a minimal example anyone can compile without having to guess on how to complete it. In this case it is particularly hard as you claim to use g++, but you use the C API. That is of course possible. It is not clear whether the cast operators from `SWI-cpp.h` are available or not.

---

<div class="post-metadata">

**Author:** ![yourein](https://avatars.discourse-cdn.com/v4/letter/y/a88e57/32.png) [@yourein](https://swi-prolog.discourse.group/u/yourein)\
**Post date:** [May 28, 2021, 8:13am UTC](https://swi-prolog.discourse.group/t/foreign-code-with-pl-assert/3987/4 "2021-05-28T08:13:44Z")

</div>

When I used PL\_assert, the linker complained that it could not find the reference. So to get around this problem, I just call assert inside of the prolog engine. I was not sure what the problem is. So I was suspicious whether there is a kind discrepancy between documents and the version I am using.

I did not use SWI-cpp.h because I used C-implementation. I called all functions in the C interface.  
Thank you for your answer.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [May 28, 2021, 8:35am UTC](https://swi-prolog.discourse.group/t/foreign-code-with-pl-assert/3987/5 "2021-05-28T08:35:02Z")

</div>

> [@yourein](#):
>
> When I used PL\_assert, the linker complained that it could not find the reference

The symbol is there:

```prolog
> nm -D src/libswipl.so | grep PL_assert
0000000000079390 T PL_assert

```

Could it be that the linker picks up some old version? PL\_assert() was added in 8.1.32.
