Stupido's question: How to use link to SWI-Prolog's new Strings


I am the principal developer of the database system ( The server is written in SWI-Prolog and C++.
The C++ part is storing the database and the SWI-Prolog part does all the interesting stuff. We started the project long time ago with BIM-Prolog and then migrated to SWI-Prolog 5.6. The tricky issue was linking the SWI-Prolog binaries with our C+± database store. This used to work until SWI-Prolog 6.6. But with the new String type introduced in SWI-Prolog 7.2 (?), the problems started. After some adaptions in the source code (mostly for avoiding special characters in the Prolog code), I can compile and link everything with SWI-Prolog 7.2. But then the binary crashes when it first calls the interface to the C++ object store.

I am not an expert in C++. Is there an easy way to translate C++ strings to the new SWI-Prolog strings?

I’m using: SWI-Prolog version 5.6 and 6.6

Kind greetings, Manfred

1 Like

PL_unify_chars() is typically the solution. AFAIK you can get a base char* and a length from the C++ string. The flags argument tells the system the preferred Prolog type and the character encoding of the C++ string such that it can be translated to Unicode.

6.6 is really old. 8.1 gives way better indexing, much more reliable and scalable concurrency and the tabling support could be pretty handy when reasoning about graphs.

1 Like

You can get a C-style (null-terminated) string from a C++ std::string by using the c_str() method. (Also the length)

This pointer may be to a copy of the string, depending on the implementation. And note the caveat: “The pointer returned may be invalidated by further calls to other member functions that modify the object.”