Mingw靜態編譯go-sqlite3包

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

    在github.com/mattn/go-sqlite3上,作者說在windows下使用go-sqlite3要使用動態連結的方法[Go does not support static linking for external C library; sqlite3 should be built as a shared library. If it runs on Windows, it needs dll.],結合網上的資源,我整理出了如何使用靜態編譯的方法使用go-sqlite3。

    折騰的過程中發現使用go get命令的時候會把源碼下載到go的安裝目錄\src\pkg\下,然後使用go install 的時候會編譯包再安裝的pkg目錄下。src\pkg\github.com\mattn\go-sqlite3 這是go-sqlite3下載到本地後的目錄結構。網上的資源是在Google groups找到的how to build "go-sqlite3" under windows with x64 ,內容如下:

Hi all. i got the solution. i hope it can help someone like me .

1. install tdm64-gcc

2. download the source code of sqlite3

3. go get https://github.com/mattn/go-sqlite3

4. unzip it and copy all files except shell.c to  github.com/mattn/go-sqlite3 directory

5. change sqlite3.go

    1) delete "#cgo pkg-config: sqlite3"

    2) change #include <sqlite3.h> to  #include "sqlite3.h"

    3) add

       #cgo windows LDFLAGS: -lmingwex -lmingw32

       #cgo windows CFLAGS: -fno-stack-check -fno-stack-protector -mno-stack-arg-probe

6. go install github.com/mattn/go-sqlite3  and test it 

the final sqlite3.go's beginning like this:

package sqlite/*#include "sqlite3.h"#include <stdlib.h>#include <string.h>#cgo windows LDFLAGS: -lmingwex -lmingw32#cgo windows CFLAGS: -fno-stack-check -fno-stack-protector -mno-stack-arg-probestatic int_sqlite3_bind_text(sqlite3_stmt *stmt, int n, char *p, int np) {  return sqlite3_bind_text(stmt, n, p, np, SQLITE_TRANSIENT);}static int_sqlite3_bind_blob(sqlite3_stmt *stmt, int n, void *p, int np) {  return sqlite3_bind_blob(stmt, n, p, np, SQLITE_TRANSIENT);}*/import "C"

    按如上的步驟,沒有下載安裝tdm-gcc,其實tdm-gcc也就是mingw,執行go install github.com/mattn/go-sqlite3 之後能順利的產生包go-sqilte3.a。然後就使用《Go web編程中》的例子使用SQLite資料庫,進行測試,原始代碼如下:

package mainimport (    "database/sql"    "fmt"    _ "github.com/mattn/go-sqlite3")func main() {    db, err := sql.Open("sqlite3", "./foo.db")    checkErr(err)    //插入資料    stmt, err := db.Prepare("INSERT INTO userinfo(username, departname, created) values(?,?,?)")    checkErr(err)    res, err := stmt.Exec("astaxie", "研發部門", "2012-12-09")    checkErr(err)    id, err := res.LastInsertId()    checkErr(err)    fmt.Println(id)    //更新資料    stmt, err = db.Prepare("update userinfo set username=? where uid=?")    checkErr(err)    res, err = stmt.Exec("astaxieupdate", id)    checkErr(err)    affect, err := res.RowsAffected()    checkErr(err)    fmt.Println(affect)    //查詢資料    rows, err := db.Query("SELECT * FROM userinfo")    checkErr(err)    for rows.Next() {        var uid int        var username string        var department string        var created string        err = rows.Scan(&uid, &username, &department, &created)        checkErr(err)        fmt.Println(uid)        fmt.Println(username)        fmt.Println(department)        fmt.Println(created)    }    //刪除資料    stmt, err = db.Prepare("delete from userinfo where uid=?")    checkErr(err)    res, err = stmt.Exec(id)    checkErr(err)    affect, err = res.RowsAffected()    checkErr(err)    fmt.Println(affect)}func checkErr(err error) {    if err != nil {        panic(err)    }}

在Liteide中編譯連結後報如下的錯誤:

D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text):__moddi3: not defined?

D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text): __divdi3: not defined?

D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text): __moddi3: not defined?

D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text):__divdi3: not defined?

D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text): __divdi3: not defined?D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text): __divdi3: not defined?D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text): __divdi3: not defined?D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text): __moddi3: not defined?D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text):__divdi3: not defined?D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text):__moddi3: not defined?D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text):__moddi3: not defined?D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text):__divdi3: not defined?D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text):__divdi3: not defined?D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text):__divdi3: not defined?D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text):__divdi3: not defined?D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text):__moddi3: not defined?D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text):__moddi3: not defined?D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text):__umoddi3: not defined?D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text):__udivdi3: not defined?D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text):__umoddi3: not defined?D:\go\go1.0.2.windows-386\go/pkg/windows_386/github.com/mattn/go-sqlite3.a(sqlite3.o)(.text):__udivdi3: not defined?

too many errors?exit code 2, process exited normally.

提示找不到這幾個函數,再次搜尋看這幾個函數在那個庫中的時候,找到如下的資源,連結地址是:

https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/VNP6Mwz_B6o

On Thu, Jul 21, 2011 at 5:09 PM, brainman  <alex.b...@gmail.com> wrote:
Hey. I know nothing about gcc but if I remove your line of

#cgo windows LDFLAGS: -lpthread -lgcc_s -lmingwex -lmsvcrt

I could build your package

make clean
make all

The test

make test

fails:

gotest
rm -f _test/camdev/sqlite.a
8g  -o _gotest_.8  _obj/vfs.cgo1.go _obj/sqlite.cgo1.go _obj/_cgo_gotypes.go sqlite_test.go
rm -f _test/camdev/sqlite.a
gopack grc _test/camdev/sqlite.a _gotest_.8  _cgo_defun.8 _cgo_import.8 sqlite3_obj.o vfs.o  vfs.cgo2.o sqlite.cgo2.o _cgo_export.o
_test/camdev/sqlite.a(sqlite3_obj.o)(.text): __divdi3: not defined
_test/camdev/sqlite.a(sqlite3_obj.o)(.text): __moddi3: not defined
_test/camdev/sqlite.a(sqlite3_obj.o)(.text): __moddi3: not defined
_test/camdev/sqlite.a(sqlite3_obj.o)(.text): __divdi3: not defined

Those are in -lgcc_s. 這樣在sqlite3.go 修改成這樣#cgo windows LDFLAGS: -lmingwex -lmingw32 -lgcc_s 再次執行go install github.com/mattn/go-sqlite3 報如下的錯誤, # github.com/mattn/go-sqlite3 D:\mingw\bin/ld.exe: cannot find -lgcc_s collect2.exe: error: ld returned 1 exit status 提示沒有找到這個libgcc_s.a,經尋找發現這是Mingw的一個基本庫檔案在如下的地址可以下載到: http://sourceforge.net/projects/mingw/files/MinGW/Base/gcc/Version4/gcc-4.7.0-1/gcc-core-4.7.0-1-mingw32-bin.tar.lzma/download,是安裝Mingw時沒有安裝完全導致的。後來也試了一下tdm-gcc,這個打包的mingw中比較完全,比Mingw 官網的那個安裝工具安裝的全面些。最後編譯go-sqlite3通過,再次編譯範例程式碼,正常通過,然後用sqlite的shell 工具建立表結構,運行後結果正確。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.