在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。git
折騰的過程當中發現使用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 ,內容以下:github
Hi all. i got the solution. i hope it can help someone like me .golang
1. install tdm64-gccweb
2. download the source code of sqlite3sql
3. go get https://github.com/mattn/go-sqlite3shell
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"windows
2) change #include <sqlite3.h> to #include "sqlite3.h"app
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-probe static 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 main import ( "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
Hey. I know nothing about gcc but if I remove your line ofI could build your package
#cgo windows LDFLAGS: -lpthread -lgcc_s -lmingwex -lmsvcrt
make clean
make all
The test
make test
fails:
gotest_test/camdev/sqlite.a(sqlite3_obj.o)(.text): __divdi3: not defined
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): __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