系統包含 sqlite 包的用普通編譯會出現異常php
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o $file_path . #編譯liunx系統可運行文件,會報錯
報如下錯誤linux
github.com/mattn/go-sqlite3 ../../mattn/go-sqlite3/sqlite3_opt_preupdate.go:12:16: undefined: SQLiteConn
這時應該git
CGO_ENABLED=0 改爲 CGO_ENABLED=1
下一步運行,若是報github
# os/user /usr/local/go/src/os/user/getgrouplist_unix.go:16:35: warning: passing 'gid_t *' (aka 'unsigned int *') to parameter of type 'int *' converts between pointers to integer types with different sign [-Wpointer-sign] /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/unistd.h:653:43: note: passing argument to parameter here # github.com/mattn/go-sqlite3 sqlite3-binding.c:32993:42: error: use of undeclared identifier 'pread64' sqlite3-binding.c:33011:42: error: use of undeclared identifier 'pwrite64' sqlite3-binding.c:33143:22: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []' sqlite3-binding.c:33152:22: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []' sqlite3-binding.c:33179:20: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []' sqlite3-binding.c:33196:16: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []' sqlite3-binding.c:14168:38: note: expanded from macro 'ArraySize' sqlite3-binding.c:33200:14: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []' sqlite3-binding.c:14168:38: note: expanded from macro 'ArraySize' sqlite3-binding.c:35853:11: warning: type specifier missing, defaults to 'int' [-Wimplicit-int] sqlite3-binding.c:32997:49: note: expanded from macro 'osPread64' sqlite3-binding.c:35965:17: warning: type specifier missing, defaults to 'int' [-Wimplicit-int] sqlite3-binding.c:33015:57: note: expanded from macro 'osPwrite64'
該問題 Mac未安裝linux的交叉編譯器。解決方法,安裝linux的交叉編譯器sql
Compiling for Linux 32 and Linux 64 on MacOS Xdocker
修改編譯命令爲app
CGO_ENABLED=0 GOOS=linux CC=/usr/local/gcc-4.8.1-for-linux64/bin/x86_64-pc-linux-gcc go build -a -installsuffix cgo -o $file_path . #cc路徑請下載時看清楚安裝包的默認路徑填寫
通常編譯出來的文件已經能夠在liunx上運行了,可是若是放在docker上的 alpine系統上又會出現問題會沒法執行問題ide
這個問題通常是alpine沒有對應的依賴庫能夠用下面的方法解決ui
查看下執行文件的依賴庫:spa
ldd $WORKDIR/main /lib64/ld-linux-x86-64.so.2 (0x7fdd15cd0000) libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7fdd15cd0000) libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7fdd15cd0000)
直接系統操做
mkdir /lib64 ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2
對應dockerfile裏的操做
RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2
而後就能夠正常工做了,原理是musl和glibc是兼容的,經過建立該符號連接修復缺乏的依賴項。alpine這個5M的鏡像也能知足go二進制文件的運行環境!
對於用alpine做爲go的編譯環境一樣存在上述問題,一樣用相同方法能夠解決。