0
votes

If I take the prolog file main_write.pl:

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

and then compile it using a command:

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

I get c_main_write file and when I run it, I get the following errors:

./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

What is the proper (error-less) way to compile this code?

I am using the following machine and swipl:

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

This error is not dependent on file writing predicates, this error happens even if a body of the clause is very simple like member(1,[1,2]).

2

2 Answers

1
votes

The following works for me:

$ 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!

You seem to be using --toplevel=main instead of --goal=main. See the bottom of the manual page on compilation.

0
votes

@Boris Your example did not work for me. The reason why it does not work is not in toplevel and goal. --toplevel=main differers from --goal=main that the former doesn't enter in interactive mode.

The solution I found is the following:

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 include shared objects (DLLs) into the saved state: http://www.swi-prolog.org/pldoc/doc_for?object=qsave_program/2

I also edited --goal=main (an initialization goal is main/0) and --toplevel=halt (after proving the goal the program halts as a toplevel goal is proved -- avoids entering to interactive mode.)