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
This part is on progress and unstable!
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:
CXXmeans the compiler being used. It’ll beclang++for a-Shell.CXXFLAGSandDEFINESmeans the options of the compiler.-O2means O2 optimization, working withclang++.-Wno-logical-op-parentheses -Wno-switch -Wno-dangling-elsewill be not needed.-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -DRAR_SMPis for 32-bit systems to deal with large files. For a 64-bit system, it‘s useless but harmless./usrwon’t be accessible on a-Shell. Actually~/Libraryacts as/usrso it‘ll be used instead.-fPICis means Position Independent Code for dynamic link libraries, working withclang++.-pthreadmeans 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:
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