Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to compile binary running files in golang

Shulou Source: shulou.com Published: 2022-06-02 11:07:55 12月04日 Update

Today, I will talk to you about how to compile binary running files in golang. Many people may not know much about it. In order to let you know more, Xiaobian summarized the following contents for you. I hope you can gain something according to this article.

The system contains sqlite packages.

CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o $file_path . #Compile liunx system runnable files, error will be reported

Report the following error

github.com/mattn/go-sqlite3../../ mattn/go-sqlite3/sqlite3_opt_preupdate.go:12:16: undefined: SQLiteConn

This should be the time

CGO_ENABLED=0 changed to CGO_ENABLED=1

Next run, if reported

# 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-sqlite3sqlite3-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 does not have linux cross-compiler installed. Solution: install linux cross-compiler

download address

Compiling for Linux 32 and Linux 64 on MacOS X

Modify compile command to

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 path Please see the default path of the installation package when downloading

Generally compiled files can already run on liunx, but if there is a problem on the alpine system placed on docker, the problem will not be executed.

This problem is usually alpine does not have a corresponding dependency library can be solved by the following method

Check out the dependency libraries of the following executable files:

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)

direct system operation

mkdir /lib64ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2

Corresponding to operations in dockerfile

RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2

Then it works fine, since musl and glibc are compatible and the missing dependency is fixed by creating the symbolic link. Alpine This 5M mirror can also meet the go binary file operating environment!

The same problem exists for compilation environments that use alpine as go, and can be solved in the same way.

After reading the above, do you have any further understanding of how to compile binary runtime files in golang? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.

Tags: Compile file run problem system binary content method environment compiler path crossover common same principle command address more fruit knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MySQL Microsoft vpn Linux