Go 编译静态库 给ios,windows,macos使用
Posted 小米渣的逆袭
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Go 编译静态库 给ios,windows,macos使用相关的知识,希望对你有一定的参考价值。
Go代码
package main
// #include <stdlib.h>
// #include <sys/types.h>
// static void callLogger(void *func, const char *msg)
//
// ((void(*)(const char *))func)(msg);
//
import "C"
import (
"fmt"
"unsafe"
)
var loggerFunc unsafe.Pointer
//export test
func test(a int)
n := a + 5
fmt.Printf("结果=%d\\n",n)
if uintptr(loggerFunc) != 0
message := C.CString(fmt.Sprintf("输出结果等于:%d",n))
C.callLogger(loggerFunc, message)
//export setLogger
func setLogger(loggerFn uintptr)
loggerFunc = unsafe.Pointer(loggerFn)
func main()
test(5)
cd到Go目录下
执行命令
export CFLAGS="-arch arm64 -miphoneos-version-min=9.0 -isysroot "$(xcrun -sdk iphoneos --show-sdk-path)
CGO_LDFLAGS="-arch arm64 -miphoneos-version-min=9.0 -isysroot "$(xcrun -sdk iphoneos --show-sdk-path)
CGO_ENABLED=1 GOARCH=arm64 GOOS=darwin CC="clang $CFLAGS" go build -tags ios -ldflags=-w -trimpath -v -o "testiphone.a" -buildmode c-archive
模拟器版本
export CFLAGS="-arch x86_64 -miphoneos-version-min=9.0 -isysroot "$(xcrun -sdk iphonesimulator --show-sdk-path)
CGO_LDFLAGS="-arch x86_64 -miphoneos-version-min=9.0 -isysroot "$(xcrun -sdk iphonesimulator --show-sdk-path)
CGO_ENABLED=1 GOARCH=amd64 GOOS=darwin CC="clang $CFLAGS" go build -tags ios -ldflags=-w -trimpath -v -o "testsimulator.a" -buildmode c-archive
这样就生成了文件test.a test.h文件
找个ios工程测试
上一个编译脚本,自动编译模拟器版本和真机版本
#!/bin/sh
export LANG=en_US.UTF-8
baseDir=$(cd "$(dirname "$0")"; pwd)
rm -f testsimulator.a
rm -f testiphone.a
rm -f test.a
rm -f test.h
rm -f testiphone.h
rm -f testsimulator.h
export CFLAGS="-arch arm64 -miphoneos-version-min=9.0 -isysroot "$(xcrun -sdk iphoneos --show-sdk-path)
CGO_LDFLAGS="-arch arm64 -miphoneos-version-min=9.0 -isysroot "$(xcrun -sdk iphoneos --show-sdk-path)
CGO_ENABLED=1 GOARCH=arm64 GOOS=darwin CC="clang $CFLAGS" go build -tags ios -ldflags=-w -trimpath -v -o "testiphone.a" -buildmode c-archive
cp $baseDir/testiphone.h $baseDir/test.h
export CFLAGS="-arch x86_64 -miphoneos-version-min=9.0 -isysroot "$(xcrun -sdk iphonesimulator --show-sdk-path)
CGO_LDFLAGS="-arch x86_64 -miphoneos-version-min=9.0 -isysroot "$(xcrun -sdk iphonesimulator --show-sdk-path)
CGO_ENABLED=1 GOARCH=amd64 GOOS=darwin CC="clang $CFLAGS" go build -tags ios -ldflags=-w -trimpath -v -o "testsimulator.a" -buildmode c-archive
lipo -create $baseDir/testiphone.a $baseDir/testsimulator.a -output $baseDir/test.a
lipo -info $baseDir/test.a
Windows版本
i386
i386首先可以简化一个概念,i386=Intel 80386。其实dui386通常被用来作为对Intel(英zhi特尔)32位微dao处理器的统称。
AMD64
AMD64,又称“x86-64”或“x64”,是一种64位元的电脑处理器架构。它是建基于现有32位元的x86架构,由AMD公司所开发,应用AMD64指令集的自家产品有Athlon 64、Athlon 64 FX、Athlon 64 X2、Turion 64、Opteron及最新的Sempron处理器。
首先安装 mingw-w64-install.exe mingw下载地址
SET CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ GOOS=windows GOARCH=amd64
go build -o test.dll -buildmode=c-shared test.go
写一个c程序试试
有个问题,FreeLibrary时程序退出时会崩溃。。不知道为什么
MacOS
mac系统下和ios类似
export CFLAGS="-mmacosx-version-min=10.9 -isysroot "$(xcrun -sdk macosx --show-sdk-path)
CGO_LDFLAGS="-mmacosx-version-min=10.9 -isysroot "$(xcrun -sdk macosx --show-sdk-path)
CGO_ENABLED=1 GOARCH=amd64 GOOS=darwin CC="clang $CFLAGS" go build -tags macosx -ldflags=-w -trimpath -v -o "test.a" -buildmode c-archive
写个mac工程试试
GOOS指目标操作系统:
darwin mac
freebsd
linux
windows
android
dragonfly
netbsd
openbsd
plan9
solaris
GOARCH指的是目标处理器的架构:
arm
arm64
386 32位
amd64 64位
ppc64
ppc64le
mips64
mips64le
s390x
以上是关于Go 编译静态库 给ios,windows,macos使用的主要内容,如果未能解决你的问题,请参考以下文章
go 工具链目前[不支持编译 windows 下的动态链接库]解决方案
Go交叉编译 Build(Mac/linux/Windows)
Go交叉编译 Build(Mac/linux/Windows)
Mac OS X10.10_xcode6.1_ios8.1环境下,编译lame静态库libmp3lame.a,支持arm64 armv7s x86_64 i386 armv7指令集