[LINUX] Solved the problem that sqlite3 got undefined error when cross-compiling Golang

What I tried to do

I tried to cross-compile Golang scripts to run on Amazon Linux

What happened

When I build it for Mac, I can build it normally and it works, but when I build it for linux, I get an error. I wondered if the cross-compilation settings for linux were wrong, but it was different, and it seems that it is happening only with ** sqlite3 **.

Works well for MacOS

Command used

$ GOOS=darwin GOARCH=amd64 go build -o main_for_mac ./main.go

The build was successful and the operation was normal.

linux undefined in sqlite3

Command used

$ GOOS=linux GOARCH=amd64 go build -o main_for_linux ./main.go

Error

# github.com/dinedal/textql/storage
../{Omission}/.go/pkg/mod/github.com/dinedal/textql{Omission}/sqlite.go:30:28: undefined: sqlite3.SQLiteConn
../{Omission}/.go/pkg/mod/github.com/dinedal/textql{Omission}/sqlite.go:49:4: undefined: sqlite3.SQLiteDriver
...

What I found after investigating:

I was addicted to a lot of Mac users using go-sqlite3. Applicable issues https://github.com/mattn/go-sqlite3/issues/384

** The cause was that the execution environment was OS-dependent in C language cross-compilation **. It seems that it is used in the implementation of sqlite.

Cross-compiling C code (and by extension, cgo code) is tricky because the choice of C compiler depends on both the OS (Windows/Linux/Mac) and architecture (x86, x64, arm) of both the host and target. On top of that, there are multiple flavors of compilers for a given combination. I don't think listing all of them is feasible. But I do think it would be helpful to more thoroughly explain what a cross-compiler is and why it is needed, and list out some of the more common options. It would also be helpful to mention using docker as a means of "cross-compiling" (at least when targeting Linux)

The solution

There is a problem with the C language compilation environment on Mac [gcc-4.8.1-for-linux32-linux64](http://crossgcc.rts-software.org/download/gcc-4.8.1-for-linux32- It is solved by installing linux64 / gcc-4.8.1-for-linux64.dmg).

procedure

Install gcc for Mac

[gcc-4.8.1-for-linux32-linux64](http://crossgcc.rts-software.org/download/gcc-4.8.1-for-linux32-linux64/gcc-4.8.1-for-linux64. Download and install dmg).

Run with options

The command to execute is as follows

build as usual env GOOS=linux GOARCH=amd64 CGO_ENABLED=1 CC=/usr/local/gcc-4.8.1-for-linux64/bin/x86_64-pc-linux-gcc go build

reference

Many people ran into problems with the above issue, so a kind person put the steps together in another issue.

install [http://crossgcc.rts-software.org/download/gcc-4.8.1-for-linux32-linux64/gcc-4.8.1-for-linux64.dmg]gcc-4.8.1-for-linux32-linux64 build as usual env GOOS=linux GOARCH=amd64 CGO_ENABLED=1 CC=/usr/local/gcc-4.8.1-for-linux64/bin/x86_64-pc-linux-gcc go build


Applicable issue
https://github.com/mattn/go-sqlite3/issues/797 


Recommended Posts

Solved the problem that sqlite3 got undefined error when cross-compiling Golang
The problem that scikit-learn gives the error No module named'_bz2'
Solved the problem that the image was not displayed in ROMol when loaded with PandasTools.LoadSDF.
Note that one problem was solved when jupyter-notebook was introduced.
The story that a hash error came out when using Pipenv
[Linux / GCP] Corrected the error that occurred when using the Git command.
When coverage fails with _sqlite3 error
Undefined symbol error when importing torchvision
[Windows] The problem that an error occurs when opening a file other than CP932 (Shift-JIS) encoded in Python has been solved for the time being.