This content originally appeared on DEV Community and was authored by kojix2
Homebrew often provides static libraries in addition to shared ones. Therefore, you can use Homebrew to obtain static libraries and link them manually.
To do this, you should first create symbolic links or copy the static libraries to a separate directory, and then refer to them using --link-flags
. This is necessary because if both static and shared libraries exist in the same directory, the linker will prioritize the shared libraries. Moving the static libraries to a different location is currently the only reliable workaround. If there is a better solution, I’d be happy to hear it.
- name: Build executable (macOS)
if: matrix.os == 'macos'
run: |
brew update
brew install libgc pcre2
ln -s $(brew ls libgc | grep libgc.a) .
ln -s $(brew ls pcre2 | grep libpcre2-8.a) .
shards build --link-flags="-L $(pwd) $(pwd)/libgc.a $(pwd)/libpcre2-8.a" --release
otool -L bin/lolcat
Note: The binary produced using this method will be specific to Apple Silicon (Arm) Macs when using latest-macos
runners. Creating a universal binary that supports both Intel and Arm architectures is significantly more complex. Given that it has been five years since the last Intel Mac was sold, it is probably acceptable to distribute Arm-only binaries for now.
Alternative Distribution Methods
Even if you provide a pre-built binary as described above, users on macOS may need to manually allow its execution via the System Settings > Privacy & Security menu, which can be a bit of a hassle.
A more convenient and recommended approach is to use a Homebrew tap. This method allows users to compile the source code themselves using Crystal, automatically installed via Homebrew. Since Homebrew also installs shared libraries, this method ensures that dependencies are resolved smoothly.
Nevertheless, distributing a statically linked binary—as is common in Rust projects—has a certain elegance. The method shown above demonstrates that it’s not impossible to achieve this with Crystal as well.
This content originally appeared on DEV Community and was authored by kojix2