Skip to content

Instantly share code, notes, and snippets.

@bryfry
Created January 13, 2022 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryfry/ffd006facf157902d5e613449cb74411 to your computer and use it in GitHub Desktop.
Save bryfry/ffd006facf157902d5e613449cb74411 to your computer and use it in GitHub Desktop.
Zig Golang C

Following steps from https://blog.ralch.com/articles/golang-sharing-libraries/

.
├── _wale.c
└── nautilus.go

go build -buildmode=c-archive -o nautilus.a nautilus.go

.
├── _wale.c
├── nautilus.a
├── nautilus.go
└── nautilus.h

other compilters

These two wont compile without -lpthread but when they do, they work!

gcc -o _wale _wale.c nautilus.a -lpthread
./_wale # works!

aarch64-linux-gnu-gcc -static -o _wale _wale.c nautilus.a -lpthread
./_wale # works!

zig fails

zig cc -static -o _wale _wale.c nautilus.a
./_wale # hangs and segfaults

zig cc -static -o _wale _wale.c nautilus.a -lpthread
./_wale # hangs and segfaults

zig cc -o _wale _wale.c nautilus.a
./_wale # hangs and segfaults

zig cc -o _wale _wale.c nautilus.a -lpthread
./_wale # hangs and segfaults

fancy extra args dont help

Above examples are more simple but what I'm really tring to do is:

GOOS=linux GOARCH=arm64 CGO_ENABLED=1  CC="${PWD}/zigcc" go build -a -ldflags "-linkmode external -extldflags '-stati
c' -s -w" -buildmode=c-archive -o nautilus.a nautilus.go
zig cc -static -o _wale _wale.c nautilus.a

And really I want to write a zig application that uses this C library. I dropped back to zig compiled C to see what was broken without zig syntax learning in the way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment