Mingw靜態編譯go-sqlite3包

    在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

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 工具創建表結構,運行後結果正確。
相關文章
相關標籤/搜索