An example Xcode project showing how to write an X11 app on OS X
Install Quartz using Homebrew Cask or directly from Sourceforge
$ brew cask install xquartz
After installing XQuartz, you will need to logout. Log back in and ensure the DISPLAY
environment variable is set up:
$ echo $DISPLAY
/private/tmp/com.apple.launchd.uk0lyyi1Ny/org.macosforge.xquartz:0
If this is empty, your XQuartz installation may have failed.
Link the header files into /usr/local/include/X11
and libraries into /usr/local/lib/X11
:
$ ln -s /usr/X11/include/X11 /usr/local/include
$ ln -s /usr/X11/lib /usr/local/lib/X11
In the Xcode project, under build settings, import the following paths to the search path:
$(SRCROOT)/CX11
for the CX11 module/usr/local/include/X11
for the X11 headers
Also under build settings, import the following library search path /usr/local/lib/X11
:
Under build phases, link the binary with libX11.6.dylib
. Under the "Choose frameworks and libraries to add" drop sheet, select "Add Other" and locate /usr/local/lib/X11/libX11.6.dylib
.
Navigate to main.swift
Run:
$ cd /path/to/main.swift
$ xcrun \
-sdk macosx \ # Include OS X SDK
swiftc main.swift \ # Compile main.swift
-lX11 -L/usr/local/lib/X11 \ # Include X11 dylib
-I/usr/local/include/X11 \ # Include X11 headers
-I$(pwd)/../CX11 # Include CX11 module
Thanks to @terhechte for his Linux example and CX11 package (slightly modified to support El Capitan's rootless system)