运行 编译 swi-prolog 代码时出错

Errors while running compiled swi-prolog code

如果我拿prolog文件main_write.pl:

main :-
    open('test.txt', write, S, [encoding(utf8)]),
    write(S, 'Hello world!'),
    close(S).

然后使用命令编译它:

swipl -q --toplevel=main --stand_alone=true -o c_main_write -c main_wite.pl

我得到 c_main_write 文件,当我 运行 它时,我得到以下错误:

./c_main_write 
ERROR: /usr/lib/swi-prolog/library/filesex.pl:57: Initialization goal raised exception:
ERROR: '$open_shared_object'/3: files: cannot open shared object file: No such file or directory

编译此代码的正确(无错误)方法是什么?

我正在使用以下机器和 swipl:

Linux 3.2.0-61-generic #93-Ubuntu SMP i686 i686 i386 GNU/Linux
SWI-Prolog version 6.6.5 for i386

此错误不依赖于文件写入谓词,即使子句的主体非常简单,如 member(1,[1,2]).

,也会发生此错误

以下对我有效:

$ cat hello.pl
main :-
    format("Hello!~n"),
    halt.
main :- halt(1).
$ swipl -q --goal=main --stand_alone=true -o hello -c hello.pl
$ ls -l hello*
-rwxr-xr-x 1 boris users 384302 Mar  8 13:16 hello
-rw-r--r-- 1 boris users     53 Mar  8 13:15 hello.pl
$ ./hello
Hello!

您似乎在使用 --toplevel=main 而不是 --goal=main。见底manual page on compilation.

@Boris 你的例子对我不起作用。 它不起作用的原因不在顶层和目标中。 --toplevel=main--goal=main不同的是前者不进入交互模式

我找到的解决方案如下:

cat main_write.pl 
main :-
    open('test.txt', write, S, [encoding(utf8)]),
    write(S, 'Hello world!'),
    close(S).

swipl -q --goal=main --toplevel=halt  --stand_alone=true --foreign=save  -o c_main_write -c main_write.pl


./c_main_write 

--foreign_language=save 将共享对象 (DLL) 包含到已保存状态中: http://www.swi-prolog.org/pldoc/doc_for?object=qsave_program/2

我还编辑了--goal=main(初始化目标是main/0)和--toplevel=halt(在证明目标后程序停止作为顶级目标被证明——避免进入交互式模式。)