Tencent / MMKV

An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, and POSIX.
Other
17.46k stars 1.91k forks source link

/usr/bin/ld: cannot find -lz #941

Closed JasonYjz closed 2 years ago

JasonYjz commented 2 years ago

I have try to run the MMKV for Golang on arm64 arch platform, not x86. And the both libcore.a and libmmkv.a are successfully compiled to be generated. When I run the test code, it is failed.

Moreover, i have checked this '/usr/lib' folder, the libz.a and libpthread.a are indeed not existed.

My point is how to skip this error, whether there is another workround way to pass the test code.

root@localhost:/home/MMKV/POSIX/golang/test# ls -al ../tencent.com/mmkv/lib/
total 2480
drwx------. 2 root root    4096 Aug 23 03:04 .
drwx------. 3 root root    4096 Aug 23 03:04 ..
-rw-r--r--. 1 root root 2403872 Aug 23 03:03 libcore.a
-rw-r--r--. 1 root root  123736 Aug 23 03:04 libmmkv.a

root@localhost:/home/MMKV/POSIX/golang/test# go run main.go 
# tencent.com/mmkv
/usr/bin/ld: cannot find -lz
collect2: error: ld returned 1 exit status

root@localhost:/home/MMKV/POSIX/golang/test# go env
GO111MODULE=""
GOARCH="arm64"
GOBIN="/home/goProject/bin"
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/goProject/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/goProject"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_arm64"
GOVCS=""
GOVERSION="go1.18.5"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/MMKV/POSIX/golang/test/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1378537216=/tmp/go-build -gno-record-gcc-switches"
lingol commented 2 years ago

You should install zlib for that arch.

JasonYjz commented 2 years ago

OK, after execute apt-get install libz-dev, it is ok now.

But there is one query need your help.

Now I need to develop Golang project on Windows, the final actual running envionment is arm64 linux.

My sample project is like below:

image

When I want to execute go build command to generate binary,

Jason@LAPTOP-BKD MINGW64 /d/Work/YJZ/Code/aid-mmkv
$ go build
package aid-mmkv
        imports tencent.com/mmkv: build constraints exclude all Go files in D:\Work\YJZ\Code\aid-mmkv\my\mmkv

What I need additional configuration steps? Or what build command is effected.

lingol commented 2 years ago

Dev environment setup? That's way out of my reach. You do whatever you like.

lingol commented 2 years ago

I personally prefer dev on the target OS environment instead of daily work environment (in your case Windows). It's can avoid lots of problem. If you don't have a device running the target OS, use a cloud server or a VM.

JasonYjz commented 2 years ago

I personally prefer dev on the target OS environment instead of daily work environment (in your case Windows). It's can avoid lots of problem. If you don't have a device running the target OS, use a cloud server or a VM.

But still cannot run my test code on x86_64 linux after following your suggestion. I directly copy the generated directory(tencent.com) into my project, and execute go run main.gowith the changed go.mod.

[jasyu@manjaro aid-mmkv]$ tree
.
├── go.mod
├── main.go
└── tencent.com
    └── mmkv
        ├── callback.go
        ├── golang-bridge.cpp
        ├── golang-bridge.h
        ├── go.mod
        ├── lib
        │   ├── libcore.a
        │   └── libmmkv.a
        ├── mmkv.go
        └── mmkv_test.go

3 directories, 10 files

The errors is below:

[jasyu@manjaro aid-mmkv]$ go install                                                                                                                                                                             
[jasyu@manjaro aid-mmkv]$ go run main.go
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4d4e08]

runtime stack:
runtime.throw({0x516cd0?, 0x0?})
        /usr/lib/go/src/runtime/panic.go:1047 +0x5d fp=0x7ffd1a2fbe40 sp=0x7ffd1a2fbe10 pc=0x4743bd
runtime.sigpanic()
        /usr/lib/go/src/runtime/signal_unix.go:819 +0x369 fp=0x7ffd1a2fbe90 sp=0x7ffd1a2fbe40 pc=0x488529

goroutine 1 [syscall]:
runtime.cgocall(0x4c5100, 0xc00005cee8)
        /usr/lib/go/src/runtime/cgocall.go:158 +0x5c fp=0xc00005ceb0 sp=0xc00005ce78 pc=0x4454dc
tencent.com/mmkv._Cfunc_getDefaultMMKV(0x1, {0x0, 0x0})
        _cgo_gotypes.go:537 +0x4d fp=0xc00005cee8 sp=0xc00005ceb0 pc=0x4c404d
tencent.com/mmkv.DefaultMMKV()
        /home/jasyu/my/aid-mmkv/tencent.com/mmkv/mmkv.go:237 +0x45 fp=0xc00005cf18 sp=0xc00005cee8 pc=0x4c4205
main.main()
        /home/jasyu/my/aid-mmkv/main.go:9 +0x1d fp=0xc00005cf80 sp=0xc00005cf18 pc=0x4c47fd
runtime.main()
        /usr/lib/go/src/runtime/proc.go:250 +0x212 fp=0xc00005cfe0 sp=0xc00005cf80 pc=0x476c12
runtime.goexit()
        /usr/lib/go/src/runtime/asm_amd64.s:1594 +0x1 fp=0xc00005cfe8 sp=0xc00005cfe0 pc=0x49fb41

goroutine 2 [force gc (idle)]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
        /usr/lib/go/src/runtime/proc.go:363 +0xd6 fp=0xc00004efb0 sp=0xc00004ef90 pc=0x476fd6
runtime.goparkunlock(...)
        /usr/lib/go/src/runtime/proc.go:369
runtime.forcegchelper()
        /usr/lib/go/src/runtime/proc.go:302 +0xad fp=0xc00004efe0 sp=0xc00004efb0 pc=0x476e6d
runtime.goexit()
        /usr/lib/go/src/runtime/asm_amd64.s:1594 +0x1 fp=0xc00004efe8 sp=0xc00004efe0 pc=0x49fb41
created by runtime.init.6
        /usr/lib/go/src/runtime/proc.go:290 +0x25

goroutine 3 [GC sweep wait]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
        /usr/lib/go/src/runtime/proc.go:363 +0xd6 fp=0xc00004f790 sp=0xc00004f770 pc=0x476fd6
runtime.goparkunlock(...)
        /usr/lib/go/src/runtime/proc.go:369
runtime.bgsweep(0x0?)
        /usr/lib/go/src/runtime/mgcsweep.go:278 +0x8e fp=0xc00004f7c8 sp=0xc00004f790 pc=0x46410e
runtime.gcenable.func1()
        /usr/lib/go/src/runtime/mgc.go:178 +0x26 fp=0xc00004f7e0 sp=0xc00004f7c8 pc=0x458fc6
runtime.goexit()
        /usr/lib/go/src/runtime/asm_amd64.s:1594 +0x1 fp=0xc00004f7e8 sp=0xc00004f7e0 pc=0x49fb41
created by runtime.gcenable
        /usr/lib/go/src/runtime/mgc.go:178 +0x6b

goroutine 4 [GC scavenge wait]:
runtime.gopark(0xc000076000?, 0x52fb28?, 0x1?, 0x0?, 0x0?)
        /usr/lib/go/src/runtime/proc.go:363 +0xd6 fp=0xc00004ff70 sp=0xc00004ff50 pc=0x476fd6
runtime.goparkunlock(...)
        /usr/lib/go/src/runtime/proc.go:369
runtime.(*scavengerState).park(0x5bbc80)
        /usr/lib/go/src/runtime/mgcscavenge.go:389 +0x53 fp=0xc00004ffa0 sp=0xc00004ff70 pc=0x4621b3
runtime.bgscavenge(0x0?)
        /usr/lib/go/src/runtime/mgcscavenge.go:617 +0x45 fp=0xc00004ffc8 sp=0xc00004ffa0 pc=0x462785
runtime.gcenable.func2()
        /usr/lib/go/src/runtime/mgc.go:179 +0x26 fp=0xc00004ffe0 sp=0xc00004ffc8 pc=0x458f66
runtime.goexit()
        /usr/lib/go/src/runtime/asm_amd64.s:1594 +0x1 fp=0xc00004ffe8 sp=0xc00004ffe0 pc=0x49fb41
created by runtime.gcenable
        /usr/lib/go/src/runtime/mgc.go:179 +0xaa

goroutine 18 [finalizer wait]:
runtime.gopark(0x5bc080?, 0xc00008a4e0?, 0x0?, 0x0?, 0xc00004e770?)
        /usr/lib/go/src/runtime/proc.go:363 +0xd6 fp=0xc00004e628 sp=0xc00004e608 pc=0x476fd6
runtime.goparkunlock(...)
        /usr/lib/go/src/runtime/proc.go:369
runtime.runfinq()
        /usr/lib/go/src/runtime/mfinal.go:180 +0x10f fp=0xc00004e7e0 sp=0xc00004e628 pc=0x4580cf
runtime.goexit()
        /usr/lib/go/src/runtime/asm_amd64.s:1594 +0x1 fp=0xc00004e7e8 sp=0xc00004e7e0 pc=0x49fb41
created by runtime.createfing
        /usr/lib/go/src/runtime/mfinal.go:157 +0x45
exit status 2

The code in main.go is similar with the provided test code.

[jasyu@manjaro aid-mmkv]$ cat main.go
package main

import (
        "fmt"
        "tencent.com/mmkv"
)

func main() {
        kv := mmkv.DefaultMMKV()

        kv.SetBool(true, "bool")
        fmt.Println("bool =", kv.GetBool("bool"))
}

I think my project strcuture is almost same with the 'test' directory, I feel very confused that why not run with my test code. Is this still my envrionmental problem or some additional configration is need?

lingol commented 2 years ago

You have not initialized MMKV. https://github.com/Tencent/MMKV/wiki/golang_setup#configuration

func main() {
    // init MMKV with root dir
    mmkv.InitializeMMKV("/path/to/your/working/directory")
}
JasonYjz commented 2 years ago

OK. There is one suggestion from my side. I'm not sure whether it is valued.

Whether to wrap this initialization action (mmkv.InitializeMMKV) in this API (mmkv.DefaultMMKV()) as well, the default root dir, for example can be set '/tmp/'. If user want to use another dir, can invoke the mmkv.InitializeMMKV function.

Now that provide mmkv.DefaultMMKV() to create the default MMKV instance, better to do a series of default operations, including intialize root dir.

This is just my simple suggestion, it is up to you.

lingol commented 2 years ago

No. It's a bad idea. These are totally two different things. DefaultMMKV() is nothing but a short cut for the more general MMKVWithID() method. Some may never use the default MMKV instance and stay with their own mmapID.

lingol commented 2 years ago

And setting the working dir to /tmp is also a really really bad idea. You should never set it to temp in the production code.