mattn/go-sqlite3

doesn't compile using gomobile (for android/ios)

haxwagon opened this issue · 11 comments

First there is an issue with pthread that I can get past, but then it seems to struggle linking with symbol conflicts. I know sqlite and go both work on android, so i'm not sure why there is symbol duplication here.

example

// github.com/mattn/go-sqlite3/example2/test.go
package example2

import (
    "database/sql"

    _ "github.com/mattn/go-sqlite3"
)

var (
    db *sql.DB
)

func OpenDatabase(filePath string) error {
    d, err := sql.Open("sqlite3", filePath)
    if err != nil {
        return err
    }

    db = d
    return nil
}

Trying to bind without any patches

}{mhawkins:~/Library/go/src/github.com/mattn/go-sqlite3/example2}{gomobile bind                      }{master!}{
# github.com/mattn/go-sqlite3
/Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lpthread
collect2: error: ld returned 1 exit status
gomobile: exit status 2

Making this change (turning off pthread for android, as the link is not required)

diff --git a/sqlite3_other.go b/sqlite3_other.go
index 8d98b4a..5c96d59 100644
--- a/sqlite3_other.go
+++ b/sqlite3_other.go
@@ -9,6 +9,6 @@ package sqlite3
 /*
 #cgo CFLAGS: -I.
 #cgo linux LDFLAGS: -ldl
-#cgo LDFLAGS: -lpthread
+#cgo linux,-android LDFLAGS: -lpthread
 */
 import "C"

Gets me past the android link issue. Now i'm having symbol conflicts, though

}{mhawkins:~/Library/go/src/github.com/mattn/go-sqlite3/example2}{gomobile bind                      }{master!}{
# command-line-arguments
/Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/5l: running /Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/arm-linux-androideabi-gcc failed: exit status 1
/Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: error: /var/folders/9s/_chc2vkd5l3d7251t9sflgk5759k1h/T/go-link-929937803/000004.o: multiple definition of '__aeabi_uidivmod'
/Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: /var/folders/9s/_chc2vkd5l3d7251t9sflgk5759k1h/T/go-link-929937803/000001.o: previous definition here
/Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: error: /var/folders/9s/_chc2vkd5l3d7251t9sflgk5759k1h/T/go-link-929937803/000004.o: multiple definition of '__aeabi_uidiv'
/Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: /var/folders/9s/_chc2vkd5l3d7251t9sflgk5759k1h/T/go-link-929937803/000001.o: previous definition here
/Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: error: /var/folders/9s/_chc2vkd5l3d7251t9sflgk5759k1h/T/go-link-929937803/000004.o: multiple definition of '__udivsi3'
/Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: /var/folders/9s/_chc2vkd5l3d7251t9sflgk5759k1h/T/go-link-929937803/000001.o: previous definition here
/Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: error: /var/folders/9s/_chc2vkd5l3d7251t9sflgk5759k1h/T/go-link-929937803/000004.o: multiple definition of '__aeabi_unwind_cpp_pr0'
/Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: /var/folders/9s/_chc2vkd5l3d7251t9sflgk5759k1h/T/go-link-929937803/000001.o: previous definition here
/Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: error: /var/folders/9s/_chc2vkd5l3d7251t9sflgk5759k1h/T/go-link-929937803/000004.o: multiple definition of '__gnu_Unwind_Restore_VFP_D'

...AND MANY MORE....

Any ideas? It seems it's a cgo link issue, but I'm not sure what it is.

Please remove LDFLAGS -lpthread from sqlite3_other.go,
it's never needed. Thanks.

See golang/go#10584 for the why android doesn't need
-lpthread.

I removed the link it but even when the contrived example i get the same errors

}{mhawkins:~/Library/go/src/github.com/mattn/go-sqlite3/example2}{gomobile bind                      }{master!}{
# command-line-arguments
/Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/5l: running /Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/arm-linux-androideabi-gcc failed: exit status 1
/Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: error: /var/folders/9s/_chc2vkd5l3d7251t9sflgk5759k1h/T/go-link-250010074/000004.o: multiple definition of '__aeabi_uidivmod'
/Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: /var/folders/9s/_chc2vkd5l3d7251t9sflgk5759k1h/T/go-link-250010074/000001.o: previous definition here
/Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: error: /var/folders/9s/_chc2vkd5l3d7251t9sflgk5759k1h/T/go-link-250010074/000004.o: multiple definition of '__aeabi_uidiv'
/Users/mhawkins/Library/go/pkg/gomobile/android-ndk-r10d/arm/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: /var/folders/9s/_chc2vkd5l3d7251t9sflgk5759k1h/T/go-link-250010074/000001.o: previous definition here

@minux do you mean: remove -lpthread FOR ANDROID with remaining it on linux, right?

@minux okay, thanks.

Perhaps I'm a little slow, but I don't know how to fix the compilation issue with a dummy c file per golang/go#9510

Could any of you fine folks explain what i need to do? I'm sort of blocked.

Thanks!

golang/go#9510 is related issue of go-sqlite3?

It is related to the gomobile stuff. Which sucks, because it's a
crazy common problem of mobile app code to need sqlite. I am not sure
if it's possible to write an adapter to the system sqlite, but without
this I cannot move forward with gomobile for app business logic. It
would require a pure go implementation of sqlite which doesn't exist
or an adaptable binding provided by gomobile which isn't planned.

Suck :(

On 4/29/15, mattn notifications@github.com wrote:

golang/go#9510 is related issue of go-sqlite3?


Reply to this email directly or view it on GitHub:
#201 (comment)

Android Native Application (works)

If you use an Android native project, e.g. package main, go-sqlite3 seems to work. Take the example project golang.org/x/mobile/example/basic and add the sql example code from #201 (comment)

Then run on an ARM device or ARM emulator:

gomobile build .
gomobile install .

Android SDK Library (aar) (works, when allowing duplicate symbols)

Edit for Go 1.6:

Should work since golang/go@754f707 is merged.

Go 1.5 Workaround:

Trying to build an aar using gomobile bind -target android seems to fail as mentioned in #201 (comment)

If you pass some compiler-args to allow duplicate symbols, it at least compiles without errors. And also works on Android 4 (Tested on API 19). It will not work with go1.5.1 on API 23 right now, since one has to wait until a new go version with the fix in golang/go#12890 comes out.

To create a library project with go-sqlite3 library do:

gomobile bind -target android -ldflags "-extldflags=-Wl,--allow-multiple-definition"

Passing the compiler args might not be needed in the future. See comments in issue 9510

See:
golang/go#9510
golang/go#9510 (comment)
golang/go#12890
https://github.com/karalabe/xgo/blob/master/docker/base/build.sh#L125