美文网首页
Golang 的静态编译

Golang 的静态编译

作者: 欧阳_z | 来源:发表于2020-09-25 19:01 被阅读0次

Go语言和C语言的一个很大的区别是,Go语言只静态编译,做个测试:

$ cat testc.c
#include <stdio.h>
int main( void )
{
    printf("hello world \n");
    return 0;
}
$ cat testgo.go 
package main
import "fmt"
func main (){
    fmt.Println("hello world")
}
$ gcc testc.c -o testc
$ go build testgo.go
$ ls -lh testc testgo
-rwxrwxr-x 1 oo2 oo2 8.2K Sep 25 10:39 testc
-rwxrwxr-x 1 oo2 oo2 2.0M Sep 25 10:39 testgo

一方面是Go语言编译后的可执行文件大小比C语言的大很多,
另一方面是C语言的可执行文件需要依赖 glibc 动态库,
ldd命令可以看出来:

$ ldd testc
    linux-vdso.so.1 (0x00007ffd57bcd000)
    /lib/x86_64-linux-gnu/libc-2.27.so (0x00007fdd35e03000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fdd363f6000)
$ ldd testgo
    not a dynamic executable

或者直接删除 glibc 动态库,C可执行程序报错,而Go的还能运行:

$ ./testc
hello world 
$ ./testgo
hello world
$ ls -lh /lib/x86_64-linux-gnu/libc.so.6
lrwxrwxrwx 1 root root 12 Jun  4 17:25 /lib/x86_64-linux-gnu/libc.so.6 -> libc-2.27.so
$ sudo rm /lib/x86_64-linux-gnu/libc.so.6
$ ./testc 
./testc: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory
$ ./testgo
hello world

这时候只有内部命令可以运行,外部命令,包括 ln 甚至最常用的 ls 命令也不能运行了:

$ type ls
ls is aliased to `ls --color=auto'
$ ln -s /lib/x86_64-linux-gnu/libc-2.27.so /lib/x86_64-linux-gnu/libc.so.6
ln: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory
$ ls
ls: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory

设置好 LD_PRELOAD 环境变量之后,ln命令可以运行,但是 sudo 仍然不能运行

$ export LD_PRELOAD="/lib/x86_64-linux-gnu/libc-2.27.so"
$ ln -s /lib/x86_64-linux-gnu/libc-2.27.so /lib/x86_64-linux-gnu/libc.so.6
ln: failed to create symbolic link '/lib/x86_64-linux-gnu/libc.so.6': Permission denied
$ sudo ln -s /lib/x86_64-linux-gnu/libc-2.27.so /lib/x86_64-linux-gnu/libc.so.6
sudo: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory

只能靠 root 用户来重新创建软连接了:

# export LD_PRELOAD="/lib/x86_64-linux-gnu/libc-2.27.so"
# ln -s /lib/x86_64-linux-gnu/libc-2.27.so /lib/x86_64-linux-gnu/libc.so.6

所以用sudorm文件要小心,还是用 root 比较好。如果没有预先留一个打开的 root 终端,登录都登不进去。

相关文章

  • Alpine 的 CGO 问题

    Golang 很棒,静态编译十分方便。但是,它也不是 100% 静态编译的,因为它需要依赖glibc ( 标准C运...

  • 如何在 Golang 中使用 MQTT

    Golang[https://golang.org/] 是 Google 开发的一种静态强类型、编译型、并发型,并...

  • Golang 的静态编译

    Go语言和C语言的一个很大的区别是,Go语言只静态编译,做个测试: 一方面是Go语言编译后的可执行文件大小比C语言...

  • MacOS Monterey安装 Golang

    Golang 简介 Go(又称Golang),是 Google 开发的一种静态强类型、编译型、并发型,并具有垃圾回...

  • Golang特性及基础

    Golang背景 Go(又称Golang) Google开发的一种静态强类型、编译型、并发型,并具有垃圾回收功能的...

  • Golang(一)简介配置和HelloWorld

    介绍 什么是Golang Go也被称为Golang,它是由谷歌创建的一种开源、编译和静态类型的编程语言。 Gola...

  • 基于scratch的iris容器示例

    本示例基于golang iris的Hello World,静态编译,基于空白镜像scratch,解决了时区问题 创...

  • Golang环境安装和依赖管理

    Golang一种静态强类型、编译型、并发型,并具有垃圾回收功能的编程语言。Golang提供了方便的安装包,支持Wi...

  • 强类型弱类型

    静态类型:编译时就确定类型,java/C/C++/golang 动态类型:运行时确定 python/PHP 强类型...

  • 强类型弱类型

    静态类型:编译时就确定类型,java/C/C++/golang动态类型:运行时确定 python/PHP 强类型:...

网友评论

      本文标题:Golang 的静态编译

      本文链接:https://www.haomeiwen.com/subject/pjofuktx.html