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.
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!
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.
-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).
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).
I’m also trying to brute force troubleshoot things myself. I added these lines to likes.cpp:
(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?
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…
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?
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?
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).
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.
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?
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!
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.