Skip to main content

3 posts tagged with "Golang"

View All Tags

· 2 min read

Tips#

初试#

# 生成 libdemo.so libdemo.hgo build -buildmode=c-shared -o libdemo.so ./demo.go# 编译 maingo build main.go./main
# macos - 查看内容nm libdemo.so | grep hello -i# linuxnm libdemo.so# windowsdumpbin /EXPORTS libdemo.dll
# windows 交叉编译 - 需要安装 mingwbrew install mingw-w64
# 32 位 交叉编译GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc go build -buildmode=c-shared -o libdemo.dll ./demo.goGOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc go build main.go
# macos wine 不支持 32 位 - mingw 版本低不支持 64 位# mac 会出现异常 cc1: sorry, unimplemented: 64-bit mode not compiled in# 通过 docker 编译docker run --rm -v $HOME/go:/root/go -v $PWD:/host -w /host wener/go:win sh
GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build -buildmode=c-shared -o libdemo.dll ./demo.goGOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build main.go
# 回到 mac 使用 wine 执行# wine 无法在 macos 上编译安装 - 系统版本问题 - 需要使用 cask 安装brew cask install wine-stablewine64 main.exe
# wine 4.0 初始化 dll 失败# 0010:fixme:winsock:set_dont_fragment IP_DONTFRAGMENT for IPv4 not supported in this platform# 0009:err:module:attach_dlls "libdemo.dll" failed to initialize, aborting# 0009:err:module:attach_dlls Initializing dlls for L"Z:\\Users\\wener\\demo\\main.exe" failed, status c0000005

demo.go

package main
import "C"
//export Versionfunc Version() int32 {    return 123}
//export Hellofunc Hello(name *C.char) *C.char {    return C.CString("Hello "+C.GoString(name)+"!")}
func main() {}

main.go

package main
import "fmt"
/*#cgo CFLAGS: -I.#cgo LDFLAGS: -L. -ldemo#include "libdemo.h"*/import "C"
func main() {    fmt.Printf("Int %v String %v\n",C.Version(), C.GoString(C.Hello(C.CString("Wener"))))}