# How to create prolog scripts that use initialization/2

**URL:** <https://swi-prolog.discourse.group/t/how-to-create-prolog-scripts-that-use-initialization-2/6183>\
**Category:** Help!\
**Tags:** how-to\
**Created:** [February 5, 2023, 12:43pm UTC](https://swi-prolog.discourse.group/t/how-to-create-prolog-scripts-that-use-initialization-2/6183 "2023-02-05T12:43:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![didier31](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/didier31/32/2517_2.png) [@didier31](https://swi-prolog.discourse.group/u/didier31)\
**Post date:** [February 5, 2023, 12:43pm UTC](https://swi-prolog.discourse.group/t/how-to-create-prolog-scripts-that-use-initialization-2/6183/1 "2023-02-05T12:43:45Z")

</div>

Hi 🙂

I’m using: SWI-Prolog 8.4.1.

I’ve created a saved state with a logtalk library of my own and would like to be able to write prolog scripts that can use this library. Something like this :

```prolog
#!/usr/bin/mylib_savedstate

:- initialization(main, main).

main(_) :- ... .

```

How may I do that, please ?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 5, 2023, 1:59pm UTC](https://swi-prolog.discourse.group/t/how-to-create-prolog-scripts-that-use-initialization-2/6183/2 "2023-02-05T13:59:41Z")

</div>

Not sure what you want. If you have a working Prolog program using initialization/2 to start it, simply do

```
swipl -o myprog -c myfile.pl

```

For example, the `scasp` executable is created using

```
swipl --no-pce --undefined=error -O -o scasp -c prolog/scasp/main.pl

```

Using `--no-pce` avoid including the graphics stuff into the executable. `-O` removes debug/3 and assertion/1 and optimizes (notably) arithmetic. `--undefined=error` causes it to fail if the code contains calls to undefined predicates.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [February 5, 2023, 6:55pm UTC](https://swi-prolog.discourse.group/t/how-to-create-prolog-scripts-that-use-initialization-2/6183/3 "2023-02-05T18:55:31Z")

</div>

For building as part of a Makefile, I have found these options to be useful:

```prolog
--stand_alone=true 
--undefined=error 
--on_error=status 
--on_warning=status
--foreign=save
--verbose=false % --verbose=true is useful for finding some errors
--no-pce % if not using graphics

```

The full set of options are on the [command line options](https://www.swi-prolog.org/pldoc/man?section=cmdline) page.

---

<div class="post-metadata">

**Author:** ![didier31](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/didier31/32/2517_2.png) [@didier31](https://swi-prolog.discourse.group/u/didier31)\
**Post date:** [February 6, 2023, 1:52pm UTC](https://swi-prolog.discourse.group/t/how-to-create-prolog-scripts-that-use-initialization-2/6183/4 "2023-02-06T13:52:38Z")

</div>

I don’t want to compile the script. On the contrary, I want my saved state (mylib\_savedstate) acts as a new interpreter, interpreting the script. Because I’ve noticed a saved state runs as an interactive interpreter.

Note: mylib\_savedstate is obtained like this

```prolog
?- consult('logtalk.qlf').
true
?- consult('mylib.qlf').
true.
?- qsave_program('mylib_savedstate').
true.

```

Thanks a lot

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 6, 2023, 3:36pm UTC](https://swi-prolog.discourse.group/t/how-to-create-prolog-scripts-that-use-initialization-2/6183/5 "2023-02-06T15:36:51Z")

</div>

If you save the program using the `stand_alone(true)` option it is a normal native executable, so you should be able to use it as `#!` interpreter. The normal saved state on POSIX systems is an `sh` script.

---

<div class="post-metadata">

**Author:** ![didier31](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/didier31/32/2517_2.png) [@didier31](https://swi-prolog.discourse.group/u/didier31)\
**Post date:** [February 6, 2023, 4:04pm UTC](https://swi-prolog.discourse.group/t/how-to-create-prolog-scripts-that-use-initialization-2/6183/6 "2023-02-06T16:04:35Z")

</div>

I’ve tried with stand\_alone(true) option. The created file is an ELF executable but it doesn’t change the game :  
invoked with !#, the saved state is actually initialized but the script of the user is not taken into account. In place, an interactive interpreter runs. 😦

I’ve tried this too :

```prolog
swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.4.1)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- qsave_program(dummy, [stand_alone(true)]).
% Disabled autoloading (loaded 45 files)
% Disabled autoloading (loaded 0 files)
true.

?- ^D
% halt

```

```prolog
#!/home/didier/collect/dummy

:- initialization(main, main).

main(_) :- writeln("Main execution").

```

Same thing.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 6, 2023, 4:27pm UTC](https://swi-prolog.discourse.group/t/how-to-create-prolog-scripts-that-use-initialization-2/6183/7 "2023-02-06T16:27:41Z")

</div>

I see. Yes, normal saved states do not process the Prolog commandline arguments, passing all arguments to the application. This means you have two options. Reimplement the commandline processing in the main goal of the saved state or use `class(development)`. From the commandline I used for testing:

```
swipl -o interp --no-pce --stand-alone --class=development -c interp.pl

```

That seems to work fine.

---

<div class="post-metadata">

**Author:** ![didier31](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/didier31/32/2517_2.png) [@didier31](https://swi-prolog.discourse.group/u/didier31)\
**Post date:** [February 6, 2023, 4:32pm UTC](https://swi-prolog.discourse.group/t/how-to-create-prolog-scripts-that-use-initialization-2/6183/8 "2023-02-06T16:32:25Z")

</div>

Thank you a lot.  
I’m going to try this. 👍

Good evening, Jan
