# Calling Prolog from C

**URL:** <https://swi-prolog.discourse.group/t/calling-prolog-from-c/3125>\
**Category:** General\
**Created:** [October 31, 2020, 3:28pm UTC](https://swi-prolog.discourse.group/t/calling-prolog-from-c/3125 "2020-10-31T15:28:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![damiazz94](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@damiazz94](https://swi-prolog.discourse.group/u/damiazz94)\
**Post date:** [October 31, 2020, 3:28pm UTC](https://swi-prolog.discourse.group/t/calling-prolog-from-c/3125/1 "2020-10-31T15:28:14Z")

</div>

I’m trying to call prolog from C. Consider this simple program:

```
#include <stdio.h>
#include <SWI-Prolog.h>

int main() {        
    static predicate_t pred;
    term_t result = PL_new_term_ref();
    printf("test\n");
}

```

and compiled with  
`gcc -I /usr/local/lib/swipl/include/ -L /usr/local/lib/swipl/bin/x86_64-linux/swipl main.c -shared -fPIC`  
The compilation succeed however, when i run the program, i get a segmentation fault. I’ve just updated SWI by pulling from the main repository on GitHub. What i’m i doing wrong?

Thanks

---

<div class="post-metadata">

**Author:** ![volker-wysk](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@volker-wysk](https://swi-prolog.discourse.group/u/volker-wysk)\
**Post date:** [October 31, 2020, 3:41pm UTC](https://swi-prolog.discourse.group/t/calling-prolog-from-c/3125/2 "2020-10-31T15:41:06Z")

</div>

Don’t you have to initialise the Prolog engine first?

---

<div class="post-metadata">

**Author:** ![damiazz94](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@damiazz94](https://swi-prolog.discourse.group/u/damiazz94)\
**Post date:** [October 31, 2020, 3:53pm UTC](https://swi-prolog.discourse.group/t/calling-prolog-from-c/3125/3 "2020-10-31T15:53:46Z")

</div>

I don’t know. According to the example at the following link [https://www.swi-prolog.org/pldoc/man?section=calling-prolog-from-c](https://www.swi-prolog.org/pldoc/man?section=calling-prolog-from-c) there are no other functions i should call

---

<div class="post-metadata">

**Author:** ![volker-wysk](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@volker-wysk](https://swi-prolog.discourse.group/u/volker-wysk)\
**Post date:** [October 31, 2020, 4:13pm UTC](https://swi-prolog.discourse.group/t/calling-prolog-from-c/3125/4 "2020-10-31T16:13:07Z")

</div>

It’s just guesswork here, but I think you need to initialise the Prolog engine, when the main() is on the C side.

---

<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:** [October 31, 2020, 4:30pm UTC](https://swi-prolog.discourse.group/t/calling-prolog-from-c/3125/5 "2020-10-31T16:30:53Z")

</div>

Yip. See [Embedding SWI-Prolog in other applications](https://www.swi-prolog.org/pldoc/man?section=embedded)
