Generating a stand-alone executable

Tips and tricks when running Logtalk using GNU Prolog as the back-end compiler

Moderator: Paulo Moura

Post Reply
Jaetan
Posts: 13
Joined: Thu Apr 09, 2009 11:35 am
Location: Tōkyō, Japan

Generating a stand-alone executable

Post by Jaetan » Tue May 12, 2009 11:54 am

Hello,

I switched my Prolog compiler from SWI Prolog 5.7.8 to GNU Prolog 1.3.1, as I would like to compile my Logtalk code to a (small) stand-alone executable. The Prolog source generated by logtalk_compile still depends on some internal Logtalk definitions, and I do not know what to do to resolve them. I am using Logtalk 2.37.0. What am I missing ?

Here is a transcript of what I did:

Code: Select all

nicolas@tamago:~/dev/example$ cat >> example.lgt <<EOF
> :- object( example ).
> 
>     :- initialization( go ).
> 
>     go :-
>         write( 'Example\n' ).
> 
> :- end_object.
> EOF
nicolas@tamago:~/dev/example$ gplgt
...
| ?- logtalk_compile( 'example' ).
% >>> compiling source file example...
%     compiling object example... compiled
% >>> example source file compiled
% (0 warnings)

(4 ms) yes
| ?- halt.
nicolas@tamago:~/dev/example$ gplc --output example ./example.pl 
/tmp/gplc0aBpAf.o: In function `directive_1':
(.text+0x475): undefined reference to `predicate($lgt_assert_runtime_clauses/1)'
collect2: ld returned 1 exit status
compilation failed
nicolas@tamago:~/dev/example$ find $LOGTALKHOME $LOGTALKUSER \( -type f -o -type l \) -exec grep -l lgt_assert_runtime_clauses {} \;
nicolas@tamago: ~/dev/example

Paulo Moura
Logtalk developer
Posts: 479
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Generating a stand-alone executable

Post by Paulo Moura » Tue May 12, 2009 1:56 pm

You need to include both the GNU Prolog config file (configs/gnu.pl) and the Logtalk compiler/runtime (compiler/logtalk.pl):

Code: Select all

$ gplc --no-top-level --no-susp-warn --no-mult-warn --output example $LOGTALKHOME/configs/gnu.pl $LOGTALKHOME/compiler/logtalk.pl ./example.pl
However, this command will result in a number of errors due to predicates that are not used but they are also not defined in GNU Prolog. Most of the errors concern multi-threading built-in predicates. These errors can be solved by removing all predicate clauses from the Logtalk compiler/runtime that call the multi-threading built-in predicates (search for the predicate names). You will also need to add dummy definitions for the predicates (:)/2 and '$lgt_logtalk_prolog_encoding'/3 to the config file. After these changes you should be able to generate the stand-alone executable:

Code: Select all

$ gplc --no-top-level --no-susp-warn --no-mult-warn --output example $LOGTALKHOME/configs/gnu.pl $LOGTALKHOME/compiler/logtalk.pl ./example.pl
$ ll example
-rwxr-xr-x  1 pmoura  staff  2070960 12 Mai 14:52 example
$ ./example
Example
$ 
Don't forget to set the compiler flag "startup_message" to "none" in the config file. I have also changed your example to call halt/0 as the last goal in the go/0 predicate.

Hope this helps. Cheers,

Paulo
Paulo Moura
Logtalk developer

Jaetan
Posts: 13
Joined: Thu Apr 09, 2009 11:35 am
Location: Tōkyō, Japan

Re: Generating a stand-alone executable

Post by Jaetan » Wed May 13, 2009 10:38 am

Thank you for your help. Following your advice, I was able to generate a stand-alone executable :D I had to add also a few other dummy predicate definitions, among which -- gasp -- ensure_loaded/1, which, contrary to the comment in gnu.pl, is only defined as a directive for GNU Prolog. I also tested your recipe successfully on contributions/xml_parser, although I had to massage xml.lgt a bit, since GNU Prolog chokes on the following:

Code: Select all

GNU Prolog 1.3.1
By Daniel Diaz
Copyright (C) 1999-2009 Daniel Diaz
| ?- X = 0''.        % This looks as a GNU Prolog bug to me.
uncaught exception: error(syntax_error('user_input:1 (char:8) quote character expected here'),read_term/3)
| ?- X = 0'\'.

X = 39

yes
| ?- X = "
".
".        % I am not sure about that one, though.
uncaught exception: error(syntax_error('user_input:3 (char:6) double quote character expected here'),read_term/3)
| ?- X = "\n".

X = [10]

yes
For a bigger picture of what I am trying to achieve: I am thinking about embedding some Logtalk or Prolog into a network appliance, for non time-critical tasks. Specifically, handling the many configuration settings and checking their consistency, or monitoring the state of the system and find out early if something suspicious happens. I have some experience embedding Scheme, but none yet for Prolog. If someone on this forum has experience on this topic, I would like to hear from him or her.

Regards

Paulo Moura
Logtalk developer
Posts: 479
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Generating a stand-alone executable

Post by Paulo Moura » Wed May 13, 2009 10:57 am

Jaetan wrote: Thank you for your help. Following your advice, I was able to generate a stand-alone executable :D I had to add also a few other dummy predicate definitions, among which -- gasp -- ensure_loaded/1, which, contrary to the comment in gnu.pl, is only defined as a directive for GNU Prolog.
I have just checked the ISO Prolog standard. To my surprise, ensure_loaded/1 is only specified as a directive. In contrast, set_prolog_flag/2 is both a directive and a built-in predicate.
Jaetan wrote: I also tested your recipe successfully on contributions/xml_parser, although I had to massage xml.lgt a bit, since GNU Prolog chokes on the following:
...
The goal X = 0'' is syntactically correct. That's definitely a bug in GNU Prolog. Not sure about the second one. Have you already sent a bug report to Daniel Diaz?
Jaetan wrote: For a bigger picture of what I am trying to achieve: I am thinking about embedding some Logtalk or Prolog into a network appliance, for non time-critical tasks. Specifically, handling the many configuration settings and checking their consistency, or monitoring the state of the system and find out early if something suspicious happens.
Nice :-) Is there a public URL for your project that I could add to the Logtalk links page?
Paulo Moura
Logtalk developer

Jaetan
Posts: 13
Joined: Thu Apr 09, 2009 11:35 am
Location: Tōkyō, Japan

Re: Generating a stand-alone executable

Post by Jaetan » Thu May 14, 2009 11:06 am

Paulo Moura wrote: The goal X = 0'' is syntactically correct. That's definitely a bug in GNU Prolog. Not sure about the second one. Have you already sent a bug report to Daniel Diaz?
I just did today.
Paulo Moura wrote: Nice :-) Is there a public URL for your project that I could add to the Logtalk links page?
Not yet. But I will tell you when I have something worth showing.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest