【发现一个问题】macos m2 下无法使用 x86_64-linux-musl-gcc 链接含有 avx512 指令的 c 代码

发布时间 2023-10-17 18:40:06作者: ahfuzhang

作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢!


一开始是使用 golang 中的 cgo 来编译:

env CC= c CGO_ENABLED=1 GOOS=linux GOARCH=amd64 \
CGO_CFLAGS="-mavx -mavx2 -mavx512f -mavx512vl -mavx512bw -O2" \
go build -tags 'musl' \
-ldflags="-w -s" \
-o bin/mysvr_linux_amd main.go

出现这样的错误信息:

# command-line-arguments
/opt/homebrew/Cellar/go/1.20.4/libexec/pkg/tool/darwin_arm64/link: running x86_64-linux-musl-gcc failed: exit status 1
/opt/homebrew/Cellar/musl-cross/0.9.9_1/libexec/bin/../lib/gcc/x86_64-linux-musl/9.2.0/../../../../x86_64-linux-musl/bin/ld: /var/folders/gh/kjdg0t613bgdkppccm_f_hb80000gn/T/go-link-3887829862/000013.o: in function `caesarEncodeSIMD':
caesar_linux_amd64.cgo2.c:(.text+0xad): undefined reference to `_mm_storeu_epi8'
collect2: error: ld returned 1 exit status

但是,我在 m2 上下载一个 linux/amd64 的 gcc 容器来编译,又是完全正常的。

最终确定是 x86_64-linux-musl-gcc 的问题:

x86_64-linux-musl-gcc -o a1 a1.o -static

/opt/homebrew/Cellar/musl-cross/0.9.9_1/libexec/bin/../lib/gcc/x86_64-linux-musl/9.2.0/../../../../x86_64-linux-musl/bin/ld: a1.o: in function `caesarEncodeSIMD':
/Users/fuchunzhang/code/infowiz/system/golang/game-launcher/pkg/cryptoutil/caesar.c:26: undefined reference to `_mm_storeu_epi8'
collect2: error: ld returned 1 exit status

由此看来无法使用x86_64-linux-musl-gcc 来实现 macos m2 下交叉编译 linux/amd64 中含有 avx 指令的代码。
后面再试试别的交叉编译工具。