daneelsan/WolframLanguageRuntimeZigDemo
WolframLanguageRuntime demo in Zig
A demo of the new Wolfram Language Runtime written in Zig.
I wrote a Wolfram community post expanding on the details Wolfram Language Runtime (SDK) demo in Zig.
We will create a standalone executable that runs the Transliterate function from Wolfram Language.
$ zig cc main.c -o transliterate-c \
-L"/Applications/Wolfram.app/Contents/SystemFiles/Components/StandaloneApplicationsSDK/MacOSX-x86-64/" \
-I"/Applications/Wolfram.app/Contents/SystemFiles/Components/StandaloneApplicationsSDK/MacOSX-x86-64/" \
-lstdc++ \
-lStandaloneApplicationsSDK \
-target x86_64-macos
Notice that even tough I'm compiling in ARM64 (see the section Prerequisites), I'm targeting the x86_64-macos
architecture.
This is because there is a bug in the current MacOSX-ARM64 StandaloneApplicationsSDK
library; though should be fixed soon.
The executable will still be able to run because of Rosetta.
Now that the executable is compiled, see the usage:
$ ./transliterate-c
Usage: ./transliterate-c "input"
Use the executable:
$ ./transliterate-c 'しんばし'
shinbashi
$ zig build-exe main.zig --name transliterate-zig \
-L"/Applications/Wolfram.app/Contents/SystemFiles/Components/StandaloneApplicationsSDK/MacOSX-x86-64/" \
-lStandaloneApplicationsSDK \
-lc++ \
-target x86_64-macos
Again, notice we are targeting x86_64-macos
(as explained in the section above).
Instead of specifying where the header library is (as is the case for the C demo), we are using our handwritten Zig package: wlr.zig.
Now that the executable is compiled, see the usage:
$ ./transliterate-zig
Usage: ./transliterate-zig "input"
Use the executable:
$ ./transliterate-zig 'しんばし'
shinbashi
Tested on:
$ uname -a
Darwin m6502.local 23.5.0 Darwin Kernel Version 23.5.0: Wed May 1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000 arm64
Tested on:
In[]:= $Version
Out[]= "14.1.0 for Mac OS X ARM (64-bit) (July 16, 2024)"
Another thing to make sure is to rename the SDK library found in "/Applications/Wolfram.app/Contents/SystemFiles/Components/StandaloneApplicationsSDK/MacOSX-x86-64/"
to have the lib
prefix (libraries are expected to have them in Unix-like systems).
Otherwise, zig cc
and zig build-exe
won't be able to find the library with -lStandaloneApplicationsSDK
.
$ mv /Applications/Wolfram.app/Contents/SystemFiles/Components/StandaloneApplicationsSDK/MacOSX-x86-64/StandaloneApplicationsSDK.a /Applications/Wolfram.app/Contents/SystemFiles/Components/StandaloneApplicationsSDK/MacOSX-x86-64/libStandaloneApplicationsSDK.a
Tested on:
$ zig version
0.14.0-dev.823+624fa8523
Zig is not on version 1.0. The language is in constant development and some things might break in the future.