~jsmp

C Development on iOS with iSH

05 May 2020

iSH offers a Linux environment on iOS using a usermode x86 32bits emulator. It is not Linux, but does enough syscalls translation and x86 emulation to get a Alpine Linux environment running on iOS. Given all the restrictions this app has to face on iOS where no such things like an hypervisor, JIT, fork, exec are possible, the author did an almost impossible job just to get this working.

Here are some personal notes on getting a C/C++ programming environment on iOS (using iSH)

Why iSH

Because it emulates Alpine Linux and can tap into the massive Alpine package repository, not all applications work without flaw but the list keeps getting bigger not so long ago, nasm was the only thing I could use to build a native x86 binary.

The Setup

This is a basic setup for Alpine Linux, the only constraint is sticking to what works on iSH.

EDIT 23 Oct: iSH is on the App Store but lacks the apk binary, you can folow these instructions to get it.

Documentation

Alpine is a small distro, and won’t include the documentation or manual pages. Other than system manual pages, the following packages will also be missing their manual pages, if you wish include the <package-name>-doc package as well.

apk add man \
    man-pages \
    mdocml-apropos \
    less \
    less-doc

Editing

Vim plays well on the virtual keyboard of the iPhone and even better on the iPad. For viewing other than cat , highlight as the ability to output term escape sequences code (or HTML). But you can use any app as iSH is also a Files provider.

apk add vim \
    highlight \
    highlight-doc

Building

I will include nasm (my favorite cross x86 assembler), gcc and clang, this also includes make and pkgconfig for helping with compile flags. If you are not going to do any cross compilation you can ignore clang.

apk add build-base \
    clang \
    clang-doc \
    nasm \
    nasm-doc

Transfering files around

Curl and curl-dev for fetching files transferring files either from inside C or in the shell. openssh as both server and client come in hand.

apk add curl \
    curl-dev \
    curl-doc \
    openssh \
    openssh-doc \
    git \
    git-doc

For git, I will use Working Copy . You can then use mount -t ios src <destination> to bring the File.app picker and select an working copy cloned repository (or other).

Test of fire

If all worked, we can create a hello.c C file with the following contents:

#include <stdio.h>
int main() {
  puts("it works!");
  return 0;
}

And compile it using make (it should know how to compile it).

$ make hello
cc     hello.c   -o hello
$ ./hello
it works!

Playgrounds and eBooks

Now you can pair your favorite ebook with a proper playground to follow along with the exercises.

Here are some recommendations regarding C and C++:

Worthy Mentions

Shells

There are other similar shells in a box like a-Shell that take a different approach (this one leverages wasm to get clang compiling C/C++ programs and running and Python with pip as a package manager) . For the terminal alone and connecting to the cloud there is Blink Shell a mosh & SSH client with some local CLI tools or Prompt a SSH only client.

IDEs

There are already and for some time now, some really good IDEs on the App Store.

Final Thoughts

CMake is not there yet, and while compiling small C programs is fast, even smaller C++ programs might make you think that the app stoped responding. This is not that painful as It is rare nowadays for me to compile an entire program, most of my development cycle centers around writting tests and only compile what is needed for that specific test suite including their test doubles.

Sometimes is faster to either use one of Prompt or Bink Shell for a remote development shell or just let your CI/CD let you know if all worked well (might not be feasable for a non personal project).

Alpine is built around musl libc and busybox. Some software is not ready to be built around musl libc and might take some effort to bring to iSH (for example Swift).

Given all the constraints iSH has to deal with, it is almost magical that it works! From reading a book and tagging along to that last idea that you want to try out while away from the desktop this is a nice add-on for the toolbox, better if it is one that is always with you.

C