Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
haxwagon opened this issue Apr 27, 2015 · 11 comments
Closed

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

haxwagon opened this issue Apr 27, 2015 · 11 comments

Comments

@haxwagon
Copy link

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.

@minux
Copy link

minux commented Apr 27, 2015

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.

@haxwagon
Copy link
Author

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
Copy link

minux commented Apr 27, 2015 via email

@mattn
Copy link
Owner

mattn commented Apr 27, 2015

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

@minux
Copy link

minux commented Apr 27, 2015 via email

@mattn
Copy link
Owner

mattn commented Apr 27, 2015

@minux okay, thanks.

mattn added a commit that referenced this issue Apr 27, 2015
@haxwagon
Copy link
Author

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!

@minux
Copy link

minux commented Apr 28, 2015 via email

@mattn
Copy link
Owner

mattn commented Apr 29, 2015

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

@haxwagon
Copy link
Author

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)

rod-hynes added a commit to rod-hynes/psiphon-tunnel-core that referenced this issue May 27, 2015
* Based on pure-Go BoltDB
* Initial motivation: failure to build sqlite3 for android with
  recent Go mobile versions (mattn/go-sqlite3#201)
* Set to be used on all but Windows (where sqlite3 is in production)
  for maximum test exposure
@weitzj
Copy link

weitzj commented Oct 24, 2015

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

gjrtimmer added a commit to gjrtimmer/go-sqlite3 that referenced this issue May 26, 2018
Compile Section Closes mattn#175
Compile Section Closes mattn#201
Compile Section Closes mattn#206
Compile Section Closes mattn#404
Compile Section Closes mattn#217
Compile Section Closes mattn#224
Compile Section Closes mattn#234
Compile Section Closes mattn#242
Feature table Closes mattn#255
Description Section Closes mattn#232
Golang:1.6 not supported Closes mattn#272
Golang:1.5 not supported + compilation section Closes mattn#283
usleep Implemented Closes mattn#285
FAQ Section Closes mattn#289
Compile Section closes mattn#295
FAQ Section Closes mattn#305
PR339 Closes mattn#318 mattn#321
Compilation Section Closes mattn#341
PR407 Closes mattn#364
Feature `sqlite_vtable` Closes mattn#393
Compile Section Closes mattn#416
sqlite_trace feature Closes mattn#433
Compilation Section Closes mattn#435
Compilation Section Closes mattn#443
Golang:1.6 Not Supported Closes mattn#445
Compilation Section Closes mattn#451
Compilation Section Closes mattn#467
Compilation Section Closes mattn#491
Compilation Section Closes mattn#495
Compilation Section Closes mattn#505
Compilation Section Closes mattn#557
Compilation Section Closes mattn#560
@mattn mattn closed this as completed in c9394b1 May 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants