MinGW statically compiled Go-sqlite3 package

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

On Github.com/mattn/go-sqlite3, the author says using Go-sqlite3 in Windows to use dynamic linking method [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.], combined with the resources on the web, I've sorted out how to use the static compilation method using Go-sqlite3.

in the process of tossing and finding the use of the go get command will download the source code to go to the installation directory \ src\pkg\, and then using go install will compile the package and install the PKG directory. Src\pkg\github.com\mattn\go-sqlite3 This is the directory structure go-sqlite3 downloaded to the local. The resources on the Web are the how-to- build "Go-sqlite3" under windows with x64 found in Google Groups , which reads as follows:

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, SQ lite_transient);} */import "C"

Follow the steps above, do not download the installation TDM-GCC, in fact TDM-GCC is MinGW, execute go install github.com/mattn/go-sqlite3 after the smooth generation of package go-sqilte3.a. Then use the "Go Web Programming" Example using the SQLite database for testing, the original code is as follows:

Package Mainimport ("Database/sql" "Fmt" _ "Github.com/mattn/go-sqlite3") func main () {db, err: = SQL. Open ("Sqlite3", "./foo.db") Checkerr (ERR)//Insert Data stmt, err: = db.    Prepare ("INSERT into UserInfo (username, departname, created) VALUES (?,?,?)") Checkerr (Err) res, err: = stmt. Exec ("Astaxie", "Development department", "2012-12-09") Checkerr (ERR) ID, err: = Res. Lastinsertid () Checkerr (err) fmt. PRINTLN (ID)//Update data 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)//query data 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)}//delete data 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)}}

The following error is reported after compiling the link in 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:notdefined? D:\GO\GO1.0.2.WINDOWS-386\GO/PKG/WINDOWS_386/GITHUB.COM/MATTN/GO-SQLITE3.A (SQLITE3.O) (. Text):__divdi3:notdefined? D:\GO\GO1.0.2.WINDOWS-386\GO/PKG/WINDOWS_386/GITHUB.COM/MATTN/GO-SQLITE3.A (SQLITE3.O) (. Text):__divdi3:notdefined? D:\GO\GO1.0.2.WINDOWS-386\GO/PKG/WINDOWS_386/GITHUB.COM/MATTN/GO-SQLITE3.A (SQLITE3.O) (. Text):__moddi3:notdefined? D:\GO\GO1.0.2.WINDOWS-386\GO/PKG/WINDOWS_386/GITHUB.COM/MATTN/GO-SQLITE3.A (SQLITE3.O) (. Text):__divdi3:notdefined? D:\GO\GO1.0.2.WINDOWS-386\GO/PKG/WINDOWS_386/GITHUB.COM/MATTN/GO-SQLITE3.A (SQLITE3.O) (. Text):__moddi3:notdefined? D:\GO\GO1.0.2.WINDOWS-386\GO/PKG/WINDOWS_386/GITHUB.COM/MATTN/GO-SQLITE3.A (SQLITE3.O) (. Text):__moddi3:notdefined? D:\GO\GO1.0.2.WINDOWS-386\GO/PKG/WINDOWS_386/GITHUB.COM/MATTN/GO-SQLITE3.A (SQLITE3.O) (. Text):__divdi3:notdefined? D:\GO\GO1.0.2.WINDOWS-386\GO/PKG/WINDOWS_386/GITHUB.COM/MATTN/GO-SQLITE3.A (SQLITE3.O) (. Text):__divdi3:notdefined? D:\GO\GO1.0.2.WINDOWS-386\GO/PKG/WINDOWS_386/GITHUB.COM/MATTN/GO-SQLITE3.A (SQLITE3.O) (. Text):__divdi3:notdefined? D:\GO\GO1.0.2.WINDOWS-386\GO/PKG/WINDOWS_386/GITHUB.COM/MATTN/GO-SQLITE3.A (SQLITE3.O) (. Text):__divdi3:notdefined? D:\GO\GO1.0.2.WINDOWS-386\GO/PKG/WINDOWS_386/GITHUB.COM/MATTN/GO-SQLITE3.A (SQLITE3.O) (. Text):__moddi3:notdefined? D:\GO\GO1.0.2.WINDOWS-386\GO/PKG/WINDOWS_386/GITHUB.COM/MATTN/GO-SQLITE3.A (SQLITE3.O) (. Text):__moddi3:notdefined? D:\GO\GO1.0.2.WINDOWS-386\GO/PKG/WINDOWS_386/GITHUB.COM/MATTN/GO-SQLITE3.A (SQLITE3.O) (. Text):__umoddi3:notdefined? D:\GO\GO1.0.2.WINDOWS-386\GO/PKG/WINDOWS_386/GITHUB.COM/MATTN/GO-SQLITE3.A (SQLITE3.O) (. Text):__udivdi3:notdefined? D:\GO\GO1.0.2.WINDOWS-386\GO/PKG/WINDOWS_386/GITHUB.COM/MATTN/GO-SQLITE3.A (SQLITE3.O) (. Text):__umoddi3:notdefined? 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.

Prompt to find these functions, search again to see these functions in the library, find the following resources, the link address is:

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

On Thu, Jul at 5:09 PM, Brainman <alex.b. @gmail .com>wrote:
Hey. I know nothing on gcc 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 is in -lgcc_s. This changes the sqlite3.go to this #cgo Windows Ldflags:-lmingwex-lmingw32-lgcc_s execute go install again github.com /mattn/go-sqlite3 reported as the error, # Github.com/mattn/go-sqlite3 D:\mingw\bin/ld.exe:cannot find-lgcc_s collect2.exe:error:ld Returned 1 exit status prompt did not find this libgcc_s.a, after finding that this is MinGW a basic library file can be downloaded to the following address: 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 is not fully installed when installing MinGW. Later also tried a bit TDM-GCC, this package of mingw in more complete, than the MinGW official website of the installation tool installed in a comprehensive. Finally compile the Go-sqlite3 pass, compile the sample code again, normal pass, and then use the SQLite shell tool to build the table structure, the result is correct after running.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.