# SWI-Prolog Interface to C++/C

**URL:** <https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671>\
**Category:** General\
**Created:** [August 8, 2024, 3:05am UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671 "2024-08-08T03:05:08Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![konokaiden](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/konokaiden/32/6708_2.png) [@konokaiden](https://swi-prolog.discourse.group/u/konokaiden)\
**Post date:** [August 8, 2024, 3:05am UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671/1 "2024-08-08T03:05:09Z")

</div>

I have been all over the internet trying to make this work, so I will keep things brief via bullet points.

- I am pretty new to Prolog and logic programming, but I have programmed with other languages before.
- I want to use Prolog with other languages (ideally I’d like to get it working with C, C++, and C#)
- From what I’ve found, I can’t get swipl to work with C# on even the most basic script. Could it be user error? Absolutely, but also the interfaces I found were very old, and possibly deprecated. I can’t tell.
- I’ve been trying to get things working with either C or C++ recently, but I can’t get that to work either.
- I have been on the github page for swipl-cpp interface ([GitHub - SWI-Prolog/packages-cpp: The SWI-Prolog C++ interface](https://github.com/SWI-Prolog/packages-cpp)) and we can’t get it working either for their test script.
- My goal would be to learn both how to use foreign language scripts within swipl as a parent, AND using swipl as a logic engine within a foreign program. I already have a program where the latter would be helpful, but the former is probably easier.
- I am on Windows 64-bit, and since my swipl folder is in Program Files, not Program Files (x86), I think I have the 64-bit SWI-Prolog as well.
- I have no idea how to make stuff work, but if someone could provide a super basic walkthrough, or just a test.cpp, test.pl, \<here’s the code to compile them\> so that I can get the gist of how to make it work together, that would be superb. Plus any needed installations and setup procedures. That would be fantastic…

So yeah, I know this is super vague, but the manual hasn’t been very helpful, I have gotten far enough to compile a cpp and a pl program into an exe with swipl commands from the terminal (something like \<swipl-ld -o filename.exe, file1.cpp, file1.pl\>), but it doesn’t run because it seems like it’s missing a dll somewhere, and the cpp interface people only knew how to make it work on Linux, so any help you can give would be appreciated!

---

<div class="post-metadata">

**Author:** ![maren](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/maren/32/6605_2.png) [@maren](https://swi-prolog.discourse.group/u/maren)\
**Post date:** [August 8, 2024, 3:22am UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671/2 "2024-08-08T03:22:37Z")

</div>

The manual is extensive on the foreign language interface: [SWI-Prolog -- Foreign Language Interface](https://www.swi-prolog.org/pldoc/man?section=foreign)  
Did you find this particular set of pages?

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [August 8, 2024, 3:27am UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671/3 "2024-08-08T03:27:27Z")

</div>

Details of the problem are here: [Can't find entry point...? · Issue #88 · SWI-Prolog/packages-cpp · GitHub](https://github.com/SWI-Prolog/packages-cpp/issues/88)

---

<div class="post-metadata">

**Author:** ![maren](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/maren/32/6605_2.png) [@maren](https://swi-prolog.discourse.group/u/maren)\
**Post date:** [August 8, 2024, 3:48am UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671/4 "2024-08-08T03:48:06Z")

</div>

Interesting. I wonder if this is related to the -Bstatic flag you’re using in the swipl-ld invocation there (in the screenshot in the github thread).  
With -Bstatic, libstdc++ will static link, but it can’t work on its own. it also needs pthreads. maybe adding `-lpthread` (and probably also `-lm`) will do something for you. Or removing -Bstatic.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [August 8, 2024, 4:20am UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671/5 "2024-08-08T04:20:50Z")

</div>

> [@maren](#):
>
> I wonder if this is related to the -Bstatic flag

`-Bstatic` was my suggestion; but it doesn’t seem to do anything. (The original problem was reported without `-Bstatic.)

In case it isn’t clear – the problem occurs on Windows, not on Linux. My theory is that the needed DLLs aren’t on the `%PATH%` (or whatever Windows uses) but I don’t know how to either diagnose or fix this (on Linux, I’d use `ldd` and `LIBRARY_PATH`; possibly some “path” options to the link step).

---

<div class="post-metadata">

**Author:** ![konokaiden](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/konokaiden/32/6708_2.png) [@konokaiden](https://swi-prolog.discourse.group/u/konokaiden)\
**Post date:** [August 8, 2024, 1:19pm UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671/6 "2024-08-08T13:19:41Z")

</div>

I went throught the executable with Dependencies (updated Dependency Walker, link here:  
[GitHub - lucasg/Dependencies: A rewrite of the old legacy software “depends.exe” in C# for Windows devs to troubleshoot dll load dependencies issues.](https://github.com/lucasg/Dependencies?tab=readme-ov-file)

Copied all the dependent libraries to the folder with the EXE, double-checked that my Path does include swipl/bin (it does), and still nothing worked.

Also re-compiled with the slightly longer code from likes.cpp:

```prolog
swipl-ld -o likes -ld g++ -goal true likes.cpp likes.pl

```

This changed nothing. I also confirmed my SWI-Prolog is the 64-bit version. It is, version 9.2.5.

---

<div class="post-metadata">

**Author:** ![konokaiden](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/konokaiden/32/6708_2.png) [@konokaiden](https://swi-prolog.discourse.group/u/konokaiden)\
**Post date:** [August 8, 2024, 1:51pm UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671/7 "2024-08-08T13:51:50Z")

</div>

Okay, we have a starting point. The code from here does in fact work:  
[https://www.swi-prolog.org/pldoc/man?section=foreign-example](https://www.swi-prolog.org/pldoc/man?section=foreign-example)

I simply copied the Prolog to a new prolog script (pl.pl, how imaginitive!), and copied the C file info into a new calc.c file (Visual Studio 2022 if anyone cares).

Then ran `swipl-ld -goal true -o calc calc.c pl.pl`

After that, ran `./calc X` using various values instead of X (52/3, pi, etc).

At the very least, this does work.

---

<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:** [August 8, 2024, 2:20pm UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671/8 "2024-08-08T14:20:29Z")

</div>

Ok, so the problem is C++ ☹ I do not have access to Windows right now. Can you add `-v` to `swipl-ld` to see what commands it executes?

---

<div class="post-metadata">

**Author:** ![konokaiden](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/konokaiden/32/6708_2.png) [@konokaiden](https://swi-prolog.discourse.group/u/konokaiden)\
**Post date:** [August 8, 2024, 2:21pm UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671/9 "2024-08-08T14:21:26Z")

</div>

Hey, I ran that before and here’s what the output was:

 ![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/5/521e27a5163efdf31cd8ac807725b48f2bd7aa70.png)

I’m also trying to brute force troubleshoot things myself. I added these lines to likes.cpp:

![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/b/ba78900dbd72f1e916b80b0475ffe3d6d9a1651d.png)

(I also got rid of the using statement and did some format cleanup, but the same issue happened before I touched anything)

Anyway, the point in question is that it should print a message just after as PlEngine is called.  
As you can see, it didn’t. Could it be running and std::cout doesn’t print to the terminal? Or is PlEngine failing more likely?

 ![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/7/725ccdb39b7f4a45dcd8e79aa0f441a62ba9c0a7.png)

---

<div class="post-metadata">

**Author:** ![konokaiden](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/konokaiden/32/6708_2.png) [@konokaiden](https://swi-prolog.discourse.group/u/konokaiden)\
**Post date:** [August 8, 2024, 2:40pm UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671/10 "2024-08-08T14:40:12Z")

</div>

Not sure if this means much, but printf() doesn’t seem to affect the lack of behavior. So it probably isn’t the functions/namespaces themselves. I don’t know if there is a problem linking, or with Prolog being initialized, or what…

---

<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:** [August 8, 2024, 2:48pm UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671/11 "2024-08-08T14:48:48Z")

</div>

PlEngine() may throw an exception. That may tell us what is bothering? Does a print before PlEngine() work, so at least we know that loading all DLLs worked?

---

<div class="post-metadata">

**Author:** ![konokaiden](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/konokaiden/32/6708_2.png) [@konokaiden](https://swi-prolog.discourse.group/u/konokaiden)\
**Post date:** [August 8, 2024, 2:53pm UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671/12 "2024-08-08T14:53:20Z")

</div>

That would make sense, as I tried to see if Prolog would print to the console, and it didn’t.  
Putting the print statement before the PlEngine did not seem to change anything, so I assume it’s the DLLs?

---

<div class="post-metadata">

**Author:** ![konokaiden](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/konokaiden/32/6708_2.png) [@konokaiden](https://swi-prolog.discourse.group/u/konokaiden)\
**Post date:** [August 11, 2024, 4:08am UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671/13 "2024-08-11T04:08:47Z")

</div>

So, a few things.

First, I went ahead and tried again on a different Windows computer to see if something I had done was causing problems. This is also Windows 11, x64, and the C example still works. I did have trouble compiling this time, so I wonder if I wasn’t including something I should have been (though I’m not sure why I didn’t still have at least one of these errors).

First up was this one:

 ![Screenshot 2024-08-10 225731](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/f/ff24b22f45ecaaad5fed941838b8fcf7adccf9d9.png)

Fixed with double quotes, telling it that this file is mine, not something native to C++. After that, there was a whole host of issues. I stopped for a moment on each section of error code, but you’ll need to pause to read it all.

> **[Windows PowerShell 2024-08-10 22-58-42.mp4](https://drive.google.com/file/d/1ZMmbdEB7dRom3q116XYUQ3jGAds7Fi66/view?usp=sharing)**
>
> Google Drive file.

---

<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:** [August 11, 2024, 9:46am UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671/14 "2024-08-11T09:46:43Z")

</div>

Something strange is happening. PL\_free\_term\_ref() is introduced in 9.3.7 and backported (with a partial implementation) to 9.2.6. It not being defined hints you are running some combination of versions?

---

<div class="post-metadata">

**Author:** ![konokaiden](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/konokaiden/32/6708_2.png) [@konokaiden](https://swi-prolog.discourse.group/u/konokaiden)\
**Post date:** [August 11, 2024, 8:32pm UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671/15 "2024-08-11T20:32:15Z")

</div>

Okay, it definitely seems to be a version issue. I was on SWI-Prolog 9.0.something on the second computer (I already had Prolog on it, so I didn’t think to check the version). Everything compiled perfectly and is working now that I updated to 9.2.6.

This makes me think that the other computer might also be a different version, though I thought it was also 9.2, so I’ll need to double check that. Anyway, thank you for helping to get this sorted out!

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [August 12, 2024, 5:57pm UTC](https://swi-prolog.discourse.group/t/swi-prolog-interface-to-c-c/7671/16 "2024-08-12T17:57:39Z")

</div>

I would recommend using the “development” version (currently 9.3.8) if you’re using C++. Any bug fixes will happen there first; and @jan does a great job of ensuring that the core functionality doesn’t break.
