#!/bin/bash # 设置默认值 OS=${1:-linux} ARCH=${2:-amd64} EXT="" LDFLAGS="" # 根据目标操作系统和架构设置编译参数 case "$OS" in windows) GOOS=windows ;; mac) GOOS=darwin ;; darwin) GOOS=darwin ;; linux) GOOS=linux ;; *) echo "Unsupported OS: $OS" exit 1 ;; esac case "$ARCH" in x64) GOARCH=amd64 ;; amd64) GOARCH=amd64 ;; x86) GOARCH=386 ;; 386) GOARCH=386 ;; arm64) GOARCH=arm64 ;; arm) GOARCH=arm ;; *) echo "Unsupported ARCH: $ARCH" exit 1 ;; esac # 启用 CGO export CGO_ENABLED=1 # 设置交叉编译工具链(如果需要) if [ "$GOOS" = "windows" ]; then EXT=.exe CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ LDFLAGS="-H windowsgui" PWD=`pwd` CGO_CPPFLAGS="-I${PWD}/include" elif [ "$GOOS" = "darwin" ]; then CC=o64-clang CXX=o64-clang++ else CC=gcc CXX=g++ fi export CC # 编译 echo "Building for $GOOS/$GOARCH..." # mkdir -p build GOOS="${GOOS}" GOARCH="${GOARCH}" CC="${CC}" CXX="${CXX}" CGO_CPPFLAGS="${CGO_CPPFLAGS}" go build -o "dist/${GOOS}_${GOARCH}${EXT}" -ldflags="${LDFLAGS}" . if [ $? -eq 0 ]; then echo "Build successful: dist/${GOOS}_${GOARCH}${EXT}" else echo "Build failed" exit 1 fi