How to use swipl within my C++ program?

I compiled and built swipl-devel in this way:


raphy@raphy:~$ git clone --recursive https://github.com/SWI-Prolog/swipl-devel.git

raphy@raphy:~/swipl-devel$ cmake -DCMAKE_INSTALL_PREFIX=/home/raphy/MyPrj/src/swipl-devel/ -DCMAKE_BUILD_TYPE=PGO -DSWIPL_PACKAGES_JAVA=OFF -DBUILD_TESTING=OFF -DINSTALL_TESTS=OFF -DINSTALL_DOCUMENTATION=OFF -GNinja -B builddir -S .

raphy@raphy:~/swipl-devel$ cmake --build builddir --target install -j9

In the CMakeLists.txt file of my project I’ve put :


target_link_directories(${PROJECT_NAME} PUBLIC
    ./src/swipl-devel/lib/swipl/lib
    ./src/swipl-devel/lib/swipl/lib/x86_64-linux
    ./src/swipl-devel/lib/swipl/bin/x86_64-linux
)

target_include_directories(${PROJECT_NAME} PUBLIC
    ./src/swipl-devel/lib/swipl/include
)

target_link_libraries (${PROJECT_NAME} PUBLIC
    swipl
)

In ./src/main.cpp :

#include "SWI-Prolog.h"


            char *av[10];
            int ac = 0;
            av[ac++] = "./swipl-devel/bin/swipl";
            av[ac++] = "-x";
            av[ac++] = "./swiple-devel/lib/swipl/boot.prc";
            av[ac] = NULL;

            PL_initialise(ac, av);
            PL_halt(PL_toplevel() ? 0 : 1);

where boot.prc is in this subfolder :

src/swipl-devel/lib/swipl$ ls
ABI  bin   boot.prc  customize  doc      lib      LICENSE    swipl.home
app  boot  cmake     demo       include  library  README.md

and swipl executable is in this subfolder:

/src/swipl-devel/bin$ ls -lah
total 8.0K
drwxr-xr-x 2 raphy raphy 4.0K May 13 18:45 .
drwxr-xr-x 5 raphy raphy 4.0K May 13 18:45 ..
lrwxrwxrwx 1 raphy raphy   35 May 13 18:45 swipl -> ../lib/swipl/bin/x86_64-linux/swipl
lrwxrwxrwx 1 raphy raphy   38 May 13 18:45 swipl-ld -> ../lib/swipl/bin/x86_64-linux/swipl-ld
lrwxrwxrwx 1 raphy raphy   39 May 13 18:45 swipl-win -> ../lib/swipl/bin/x86_64-linux/swipl-win

Compiling

cmake --build builddir -j9

and executing I get the following error message:

[FATAL ERROR: at Wed May 13 21:41:13 2026
	Could not open resource database "./swiple-devel/lib/swipl/boot.prc": No such file or directory]
Aborted (core dumped)


OS: Ubuntu 24.04

gcc (Ubuntu 14.2.0-4ubuntu2~24.04.1) 14.2.0

What am I doing wrong and/or missing? How to make it work?

Doing in this way:

                char *argv[] = {"hello", "-q"};
                PL_initialise(2, argv);
                PL_halt(PL_toplevel() ? 0 : 1);

I do not get the error message anymore, but, when executing I get: 

```1 ?-
```
which is the prompt for swipl-devl

But what I would like to do is to be able to use swipl within my C++ program
Is this feasible and possible? How to do it?

What is this extra e doing there?

Hi @Jan

It seems the that there should be other errors apart from that typo swiple:

With /src/main :

                char *av[10];
                int ac = 0;
                av[ac++] = "./swipl-devel/bin/swipl";
                av[ac++] = "-x";
                av[ac++] = "./swipl-devel/lib/swipl/boot.prc";
                av[ac] = NULL;


                PL_initialise(ac, av);
                PL_halt(PL_toplevel() ? 0 : 1);

I still get the error message:

[FATAL ERROR: at Thu May 14 09:58:10 2026
	Could not open resource database "./swipl-devel/lib/swipl/boot.prc": No such file or directory]
Aborted (core dumped)

/src/swipl-devel/lib/swipl$ ls
ABI  bin   boot.prc  customize  doc      lib      LICENSE    swipl.home
app  boot  cmake     demo       include  library  README.md

Trying the same, adjusted for my local paths works just fine. Depending on the platform, I’d try tracing system calls. On linux:

strace -o calls.log <exe>

and then search in calls.log to see what is happening.

Make sure you link the a compatible libswipl.so (or whatever it is named on your platform). One of the few things that changed over the years is that it expects an ABI file next to boot.prc that is inspected to validate that the Prolog VM is compatible with the one that created the boot file. In the old days a mismatch often worked to some degree. Now it is rejected.

I tried to build and compile swipl fetching the swipl-devel git repo within MyPrj’s CMakeLists.txt :

FetchContent_Declare(
    swipl
    GIT_REPOSITORY https://github.com/SWI-Prolog/swipl-devel.git
    GIT_TAG master
)
FetchContent_MakeAvailable(swipl)

But during compilation I got this error message:

gmake[2]: *** No rule to make target '_deps/swipl-build/man/utf8proc', needed by 'man/pldoc2tex'.  Stop.
gmake[1]: *** [CMakeFiles/Makefile2:3793: _deps/swipl-build/man/CMakeFiles/pldoc2tex_state.dir/all] Error 2

So…I’m going to stick to my previous method:

raphy@raphy:~/swipl-devel$ cmake -DCMAKE_INSTALL_PREFIX=/home/raphy/MyPrj/src/swipl-devel/ -DCMAKE_BUILD_TYPE=PGO -DSWIPL_PACKAGES_JAVA=OFF -DBUILD_TESTING=OFF -DINSTALL_TESTS=OFF -DINSTALL_DOCUMENTATION=OFF -GNinja -B builddir -S .

raphy@raphy:~/swipl-devel$ cmake --build builddir --target install -j9

And I will trace system calls, as you suggested. I will write down here the output

Output of strace:

raphy@raphy:~$ strace -o calls.log /home/raphy/MyPrj/builddir/MyPrj 
Current path: "/home/raphy"
[FATAL ERROR: at Thu May 14 12:12:58 2026
	Could not open resource database "./swipl-devel/lib/swipl/boot.prc": No such file or directory]
Aborted (core dumped)

In CMakeLists.txt` file:

target_link_directories(${PROJECT_NAME} PUBLIC
    ./src/swipl-devel/lib/swipl/lib
    ./src/swipl-devel/lib/swipl/lib/x86_64-linux
    ./src/swipl-devel/lib/swipl/bin/x86_64-linux
)

target_include_directories(${PROJECT_NAME} PUBLIC
    ./src/swipl-devel/lib/swipl/include
)

target_link_libraries (${PROJECT_NAME} PUBLIC
    swipl
)

raphy@raphy:~/MyPrj/src/swipl-devel/lib/swipl/lib/x86_64-linux$ ls -lah | grep libswipl.so
lrwxrwxrwx 1 raphy raphy   14 May 13 18:45 libswipl.so -> libswipl.so.10
lrwxrwxrwx 1 raphy raphy   18 May 13 18:45 libswipl.so.10 -> libswipl.so.10.1.7
-rw-r--r-- 1 raphy raphy  14M May 13 18:44 libswipl.so.10.1.7

raphy@raphy:~/MyPrj/src/swipl-devel/lib/swipl$ ls
ABI  app  bin  boot  boot.prc  cmake  customize  demo  doc  include  lib  library  LICENSE  README.md  swipl.home

raphy@raphy:~/MyPrj/src/swipl-devel/lib/swipl$ cat ABI
swipl-abi-2-68-b5762387-a5b83da7


I built and compiled again swipl:

raphy@raphy:~$ git clone --recursive https://github.com/SWI-Prolog/swipl-devel.git

raphy@raphy:~/swipl-devel$ cmake -DCMAKE_INSTALL_PREFIX=/home/raphy/MyPrj/src/swipl-devel/ -DCMAKE_BUILD_TYPE=Release -DSWIPL_PACKAGES_JAVA=OFF -DINSTALL_DOCUMENTATION=OFF -GNinja -B builddir -S .

raphy@raphy:~/swipl-devel$ cmake --build builddir --target install -j10

-- Install configuration: "Release"
-- Set non-toolchain portion of runtime path of "/home/raphy/MyPrj/src/swipl-devel/lib/swipl/bin/x86_64-linux/swipl" to "/home/raphy/MyPrj/src/swipl-devel/lib/swipl/lib/x86_64-linux"
-- Set non-toolchain portion of runtime path of "/home/raphy/MyPrj/src/swipl-devel/lib/swipl/lib/x86_64-linux/libswipl.so.10.1.7" to "/home/raphy/MyPrj/src/swipl-devel/lib/swipl/lib/x86_64-linux"
-- Set non-toolchain portion of runtime path of "/home/raphy/MyPrj/src/swipl-devel/lib/swipl/bin/x86_64-linux/swipl-win" to "/home/raphy/MyPrj/src/swipl-devel/lib/swipl/lib/x86_64-linux"
-- Set non-toolchain portion of runtime path of "/home/raphy/MyPrj/src/swipl-devel/lib/swipl/bin/x86_64-linux/swipl-ld" to "/home/raphy/MyPrj/src/swipl-devel/lib/swipl/lib/x86_64-linux"


But I get the same error : 

raphy@raphy:~/MyPrj$ ./builddir/MyPrj
Current path: "/home/raphy/MyPrj"
[FATAL ERROR: at Thu May 14 12:55:25 2026
	Could not open resource database "./swipl-devel/lib/swipl/boot.prc": No such file or directory]
Aborted (core dumped)


raphy@raphy:~$ strace -o calls.log  /home/raphy/MyPrj/builddir/MyPrj
Current path: "/home/raphy"
[FATAL ERROR: at Thu May 14 13:01:32 2026
	Could not open resource database "./swipl-devel/lib/swipl/boot.prc": No such file or directory]
Aborted (core dumped)




Last lines of calls.log :

ioctl(0, TCGETS, {c_iflag=ICRNL|IXON|IUTF8, c_oflag=NL0|CR0|TAB0|BS0|VT0|FF0|OPOST|ONLCR, c_cflag=B38400|CS8|CREAD, c_lflag=ISIG|ICANON|ECHO|ECHOE|ECHOK|IEXTEN|ECHOCTL|ECHOKE, ...}) = 0
ioctl(1, TCGETS, {c_iflag=ICRNL|IXON|IUTF8, c_oflag=NL0|CR0|TAB0|BS0|VT0|FF0|OPOST|ONLCR, c_cflag=B38400|CS8|CREAD, c_lflag=ISIG|ICANON|ECHO|ECHOE|ECHOK|IEXTEN|ECHOCTL|ECHOKE, ...}) = 0
ioctl(2, TCGETS, {c_iflag=ICRNL|IXON|IUTF8, c_oflag=NL0|CR0|TAB0|BS0|VT0|FF0|OPOST|ONLCR, c_cflag=B38400|CS8|CREAD, c_lflag=ISIG|ICANON|ECHO|ECHOE|ECHOK|IEXTEN|ECHOCTL|ECHOKE, ...}) = 0
newfstatat(AT_FDCWD, "/home/raphy", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0
newfstatat(AT_FDCWD, "/home", {st_mode=S_IFDIR|0755, st_size=4096, ...}, 0) = 0
newfstatat(AT_FDCWD, "/", {st_mode=S_IFDIR|0755, st_size=4096, ...}, 0) = 0
newfstatat(AT_FDCWD, "/home/raphy", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0
newfstatat(AT_FDCWD, "./swipl-devel/bin/swipl", 0x7fff38e53cc0, 0) = -1 ENOENT (No such file or directory)
readlink("./swipl-devel/bin/swipl", 0x7fff38e55d60, 4095) = -1 ENOENT (No such file or directory)
getcwd("/home/raphy", 4096)             = 12
newfstatat(AT_FDCWD, "/home", {st_mode=S_IFDIR|0755, st_size=4096, ...}, 0) = 0
newfstatat(AT_FDCWD, "/home/raphy/swipl-devel/bin", 0x7fff38e4cbc0, 0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/raphy/swipl-devel/swipl.home", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/raphy/MyPrj/src/swipl-devel/lib/swipl/ABI", O_RDONLY) = 13
ioctl(13, TCGETS, 0x7fff38e50b70)       = -1 ENOTTY (Inappropriate ioctl for device)
fcntl(13, F_SETFD, FD_CLOEXEC)          = 0
read(13, "swipl-abi-2-68-b5762387-a5b83da7"..., 4096) = 33
close(13)                               = 0
openat(AT_FDCWD, "./swipl-devel/bin/swipl", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "./swipl-devel/lib/swipl/boot.prc", O_RDONLY) = -1 ENOENT (No such file or directory)
write(2, "[FATAL ERROR: at Thu May 14 13:0"..., 43) = 43
write(2, "Could not open resource database"..., 94) = 94
write(2, "]\n", 2)                      = 2
rt_sigprocmask(SIG_UNBLOCK, [ABRT], NULL, 8) = 0
gettid()                                = 153682
getpid()                                = 153682
tgkill(153682, 153682, SIGABRT)         = 0
--- SIGABRT {si_signo=SIGABRT, si_code=SI_TKILL, si_pid=153682, si_uid=1000} ---
+++ killed by SIGABRT (core dumped) +++

@jan What does “Inappropriate ioctl for device” mean ?

And why

newfstatat(AT_FDCWD, "./swipl-devel/bin/swipl", 0x7ffc02a43000, 0) = -1 ENOENT (No such file or directory)

?