githubEdit

Compile a simple command with a-Shell

This article focus on compiling a small file or project written in C/C++ with a-Shell’s own tool chain. Due to Apple’s limitations, they can only be compiled to WebAssembly instead of native codes, so don’t dream for emacs or fish!

Meet clang, clang++ and wasm

Let’s start from compiling a single program file. Here are two examples:

// test.c
#include<stdio.h>

int main(){
    printf("Hello, world!\n");
    return 0;
}
// test.cpp
#include<iostream>
using namespace std;

int main(){
    cout << "Hello, world!" << endl;
    return 0;
}

To compile, we use clang and clang++ respectively. We use -o to set the name of the output file (they can end with .wasm or not).

Then run the compiled files with wasm. You can either call wasm to run it or execute it directly like binary code. Also you may want to try wasm3.

You may receive a message wasm: Error: sometimes. When you do, try to close all the open windows then retry.

Meet make

circle-exclamation

For big projects, it’ll be a difficult job to input commands to compile all files line by line. Usually, make is used to do it automatically. make seeks for the makefile of the project and do as it directs when it works. You may have known a famous way to compile and install a project from source codes:

For the example above, ./configure generates the makefile according to your platform, make compiles the project according to the makefile, and make install installs it to the computer. However, sometimes ./configure really doesn’t know the difference between a-Shell and WebAssembly, so it may do the wrong work and mess it up.

Now let’s see a simple project: unrar. It’s simple enough that there is no script like configure to generate makefiles. First of all get the whole source code, and here are parts of the short makefile:

Now we’ll revise the makefile to let it suit our tool chain. Before that, we need to know:

  • CXX means the compiler being used. It’ll be clang++ for a-Shell.

  • CXXFLAGS and DEFINES means the options of the compiler.

  • -O2 means O2 optimization, working with clang++.

  • -Wno-logical-op-parentheses -Wno-switch -Wno-dangling-else will be not needed.

  • -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -DRAR_SMP is for 32-bit systems to deal with large files. For a 64-bit system, it‘s useless but harmless.

  • /usr won’t be accessible on a-Shell. Actually ~/Library acts as /usr so it‘ll be used instead.

  • -fPIC is means Position Independent Code for dynamic link libraries, working with clang++.

  • -pthread means multiple threads, and is not provided by WebAssembly yet, so it doesn’t work with a-Shell.

Then the makefile can be revised to:

Now let’s try to compile it:

circle-info

Actually this project does not work due to lack of APIs provided by WASI. We are looking for a project that the source code does work with a-Shell’s own tool chain. If you know one, please let us know.

Last updated