Previous: , Up: The Application Builder   [Contents][Index]


6.7.3.5 Examples

  1. The character-based SICStus development system executable (sicstus) can be created using:
    % spld --main=prolog -o sicstus
    

    This will create a development system that is dynamically linked and has no prelinked foreign resources.

  2. % spld --static -D --resources=random -o main
    
    This will create a statically linked executable called main that has the resource random prelinked (statically).
  3. An all-in-one executable with a home-built foreign resource.

    This example is similar to the example in All-in-one Executables, with the addition of a foreign resource of our own.

    % bar.pl
    
    :- use_module(library(random)).
    :- use_module(library(clpfd)).
    
    
    % This will be called when the application starts:
    user:runtime_entry(start) :-
       %% You may consider putting some other code here...
       write('hello world'),nl,
       write('Getting a random value:'),nl,
       random(1, 10, R),                % from random
       writeq(R),nl,
       ( all_different([3,9]) ->        % from clpfd
           write('3 != 9'),nl
       ; otherwise ->
           write('3 = 9!?'),nl
       ),
       '$pint'(4711).                   % from our own foreign resource 'bar'
    
    foreign(print_int, '$pint'(+integer)).
    foreign_resource(bar, [print_int]).
    :- load_foreign_resource(bar).
    
    /* bar.c */
    
    #include <sicstus/sicstus.h>
    #include <stdio.h>
    /* bar_glue.h is generated by splfr from the foreign/[2,3] facts. 
       Always include the glue header in your foreign resource code.
    */
    #include "bar_glue.h"
    
    extern void print_int(SP_integer a);
    
    void print_int(SP_integer a)
    {
      /* Note the use of SPRIdINTEGER to get a format specifier corresponding
         to the SP_integer type. For most platforms this corresponds 
         to "ld" and long, respectively. */
      printf("a=%" SPRIdINTEGER "\n", (SP_integer)a);
    }
    

    To create the saved state bar.sav we will compile the file bar.pl and save it with save_program('bar.sav'). When compiling the file the directive :- load_foreign_resource(bar). is called so a dynamic foreign resource must be present.

    Thus, first we build a dynamic foreign resource.

    % splfr bar.c bar.pl
    

    Then, we create the saved state.

    % sicstus --goal "compile(bar), save_program('bar.sav'), halt."
    

    We also need a static foreign resource to embed in our all-in-one executable.

    % splfr --static bar.c bar.pl
    

    Finally, we build the all-in-one executable with spld. We do not need to list the foreign resources needed. spld will extract their names from the .sav file. Adding the --verbose option will make spld output lots of progress information, among which are the names of the foreign resources that are needed. Look for “Found resource name” in the output.

    % spld --verbose --static --main=restore --respath=. --resources=bar.sav=/mystuff/bar.sav --output=bar
    

    In this case four foreign resource names are extracted from the .sav file: bar, clpfd, and random. The source file bar.pl loads the foreign resource named bar. It also uses the library(random) module, which loads the foreign resource named random, and the library(clpfd) module, which loads the foreign resource named clpfd.

    By not listing foreign resources when running spld, we avoid the risk of omitting a required resource.



Send feedback on this subject.