# Compiler directive to distinguish between ubuntu and windows

**URL:** <https://swi-prolog.discourse.group/t/compiler-directive-to-distinguish-between-ubuntu-and-windows/217>\
**Category:** Help!\
**Created:** [February 8, 2019, 11:29am UTC](https://swi-prolog.discourse.group/t/compiler-directive-to-distinguish-between-ubuntu-and-windows/217 "2019-02-08T11:29:57Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [February 8, 2019, 11:29am UTC](https://swi-prolog.discourse.group/t/compiler-directive-to-distinguish-between-ubuntu-and-windows/217/1 "2019-02-08T11:29:57Z")

</div>

Hello,

I am looking for compiler directive to distinguish between running on ubunutu or windows, but haven’t found it yet.

I have some log file path information that is different on either platform and want to condition compile each one of them.

Any pointers would be much appreciated.

thank you,

Dan

p.s. is there a unified approach for paths that works across all platforms ?

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [February 8, 2019, 12:14pm UTC](https://swi-prolog.discourse.group/t/compiler-directive-to-distinguish-between-ubuntu-and-windows/217/2 "2019-02-08T12:14:02Z")

</div>

I hope I understand your question.

There is the `current_prolog_flag/2` predicate, see [here](http://www.swi-prolog.org/pldoc/doc_for?object=current_prolog_flag/2). Towards the top you see an example of writing Windows-specific code. The relevant keys are I guess `windows`, `apple`, `unix`?

---

<div class="post-metadata">

**Author:** ![joaomarizgraca](https://avatars.discourse-cdn.com/v4/letter/j/f07891/32.png) [@joaomarizgraca](https://swi-prolog.discourse.group/u/joaomarizgraca)\
**Post date:** [February 8, 2019, 12:21pm UTC](https://swi-prolog.discourse.group/t/compiler-directive-to-distinguish-between-ubuntu-and-windows/217/3 "2019-02-08T12:21:05Z")

</div>

Perhaps you can use the following statement. Hope can help,  
Regarde

find\_OS(windows):-  
current\_prolog\_flag(windows, true).  
find\_OS(unix):-  
current\_prolog\_flag(unix, true).

consult\_according\_2\_platform:-  
find\_OS(OpSystem),  
load\_code\_according\_to\_file\_system(OpSystem).

---

<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 8, 2019, 12:42pm UTC](https://swi-prolog.discourse.group/t/compiler-directive-to-distinguish-between-ubuntu-and-windows/217/4 "2019-02-08T12:42:56Z")

</div>

> [@Boris](#):
>
> The relevant keys are I guess `windows` , `apple` , `unix` ?

And since recently `emscripten` (for web assembly) and `android` 🙂

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [February 8, 2019, 1:03pm UTC](https://swi-prolog.discourse.group/t/compiler-directive-to-distinguish-between-ubuntu-and-windows/217/5 "2019-02-08T13:03:24Z")

</div>

Thank you.

Yes, current\_prolog\_flag should work for me …

Dan

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [February 8, 2019, 1:09pm UTC](https://swi-prolog.discourse.group/t/compiler-directive-to-distinguish-between-ubuntu-and-windows/217/6 "2019-02-08T13:09:05Z")

</div>

for now i just did something like the following: which returns the right log\_file when requested. Seems to work.

log\_file(‘c:/file.text’) :-  
current\_prolog\_flag(windows, true).

log\_file(’/home/user/file.text’) :-  
current\_prolog\_flag(windows, true).

---

<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 8, 2019, 5:02pm UTC](https://swi-prolog.discourse.group/t/compiler-directive-to-distinguish-between-ubuntu-and-windows/217/7 "2019-02-08T17:02:32Z")

</div>

If you write it this way, you won’t leave a choice point:

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [February 8, 2019, 6:17pm UTC](https://swi-prolog.discourse.group/t/compiler-directive-to-distinguish-between-ubuntu-and-windows/217/8 "2019-02-08T18:17:55Z")

</div>

I can guess what you wrote but I don’t see it. I don’t know if you accidentally something or if the new forum swallowed your markdown.

---

<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 8, 2019, 6:51pm UTC](https://swi-prolog.discourse.group/t/compiler-directive-to-distinguish-between-ubuntu-and-windows/217/9 "2019-02-08T18:51:21Z")

</div>

This is what I wrote (and it seems to have been swallowed by using email):

```
log_file(Path) :- % This code hasn't been tested
( current_prolog_flag(unix, true)
-> absolute_file_name('~/file.text', Path)
; current_prolog_flag(windows, true)
-> Path = 'c:/file.text'
; current_prolog_flag(arch, Arch),
   throw(error(unknown_os(arch=Arch), _))
).

```

(You can also prevent choice points by using cuts, of course)

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [February 9, 2019, 7:53pm UTC](https://swi-prolog.discourse.group/t/compiler-directive-to-distinguish-between-ubuntu-and-windows/217/10 "2019-02-09T19:53:21Z")

</div>

Hi Peter,

It seems to work well for me.

For each OS there is only one choice available. the predicate must be called with an unbound variable though.

I added a third clause with a default file name without path info, assuming that it would create the log file in any default folder where prolog is running – so at least the system won’t fail – hopefully.

Dan

---

<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 9, 2019, 8:13pm UTC](https://swi-prolog.discourse.group/t/compiler-directive-to-distinguish-between-ubuntu-and-windows/217/11 "2019-02-09T20:13:40Z")

</div>

Your code works, but it leaves a choice point on Unix (it doesn’t on Windows):

```
log_file('/home/user/file.text') :-
    current_prolog_flag(unix, true).
log_file('c:/file.text') :-
    current_prolog_flag(windows, true).

```

You can see this when you run it (on Unix):

```
$ swipl --no-tty -q
1 ?- [log_file].
true.

2 ?- log_file(LogFile).
LogFile = '/home/user/file.text' ;
false.

```

(If there were no choice point, you wouldn’t see the `false`.)

You can avoid the choice point by either using the if-then-else style or by adding cuts as follows (the cut in the 2nd clause isn’t necessary, but makes it easier to add a 3rd clause, for example with a default or to throw an error):

```
log_file('/home/user/file.text') :-
    current_prolog_flag(unix, true),
    !.
log_file('c:/file.text') :-
    current_prolog_flag(windows, true),
    !.
```

---

<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 9, 2019, 8:19pm UTC](https://swi-prolog.discourse.group/t/compiler-directive-to-distinguish-between-ubuntu-and-windows/217/12 "2019-02-09T20:19:07Z")

</div>

(Also, I would suggest using `~` instead of `/home/user` on Unix, to make it easier for other people to use your code … [`absolute_file_name/2`](http://www.swi-prolog.org/pldoc/doc_for?object=absolute_file_name/2) and friends can also be useful.)

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [February 9, 2019, 8:35pm UTC](https://swi-prolog.discourse.group/t/compiler-directive-to-distinguish-between-ubuntu-and-windows/217/13 "2019-02-09T20:35:11Z")

</div>

thank you.

in the PDT prolog console it seems to complete with true. However, adding the cuts is a good idea – since, there should be no choice points.

thank you,

Dan

p.s. thank you for pointing me to the the absolute\_file\_name and related predicates – i will try to use them –

---

<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 10, 2019, 4:52pm UTC](https://swi-prolog.discourse.group/t/compiler-directive-to-distinguish-between-ubuntu-and-windows/217/14 "2019-02-10T16:52:50Z")

</div>

> [@grossdan](#):
>
> p.s. thank you for pointing me to the the absolute\_file\_name and related predicates – i will try to use them

Good advice by Peter. Also look for `expand_file_name/2` to expand `~` and `$VAR` and, on Windows, `win_folder/2` that allows you to find the _well known_ places on Windows.
