When writing cross-platform applications or libraries, it is often necessary to allow for differences between operating systems. The variableSys.KERNEL
can be used to handle such cases. There are several functions in theSys
module intended to make this easier, such asisunix
,islinux
,isapple
,isbsd
,isfreebsd
, andiswindows
. These may be used as follows:
if Sys.iswindows() windows_specific_thing(a)end
Note thatislinux
,isapple
, andisfreebsd
are mutually exclusive subsets ofisunix
. Additionally, there is a macro@static
which makes it possible to use these functions to conditionally hide invalid code, as demonstrated in the following examples.
Simple blocks:
ccall((@static Sys.iswindows() ? :_fopen : :fopen), ...)
Complex blocks:
@static if Sys.islinux() linux_specific_thing(a)elseif Sys.isapple() apple_specific_thing(a)else generic_thing(a)end
When nesting conditionals, the@static
must be repeated for each level (parentheses optional, but recommended for readability):
@static Sys.iswindows() ? :a : (@static Sys.isapple() ? :b : :c)
Settings
This document was generated withDocumenter.jl version 1.8.0 onWednesday 9 July 2025. Using Julia version 1.11.6.