I tried to cross-compile Golang scripts to run on Amazon Linux
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 **.
Command used
$ GOOS=darwin GOARCH=amd64 go build -o main_for_mac ./main.go
The build was successful and the operation was normal.
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
...
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)
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).
[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).
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
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