Go 是否依赖于 C 运行时?
Does Go depend on C runtime?
我找不到信息 golang 是否依赖于 c 运行时?如果它依赖于它,它是否在 go-binary 中静态编译以使 Go-app 无依赖地在任何地方工作?
这是关于 what C-runtime is
的话题
如果你说的是 Go 编译器提供的可执行文件,那么答案是 "yes or no—it depends":
在大多数情况下,生成的可执行程序 不 依赖于 C run-time 库。
但是,在某些平台上,在某些情况下,C 运行时库会动态 链接。
特别是,当使用 DNS 解析时,Linux 上的 Go 版本 < 1.5 就是这种情况:运行时依赖于平台的 libc
实现来处理此类解析。
In 1.5 this has been reworked.
另一种可能的情况是 (IIRC) Solaris,它没有提供直接访问内核系统调用的稳定方法,requires routing these calls through the platform's libc
.
还有一种情况:使用cgo
,这是一个将Go代码与国外C代码接口的层。使用 cgo
使您的 Go 程序依赖于 C 运行时。请注意,您可能不会直接使用 cgo
,但您可能正在使用的一个或多个 third-party 包可以使用 cgo
,并且——传递地——你的程序最终取决于 C 运行时库.
我认为接受的答案是正确的,但是导入 'net' 包的二进制文件通常依赖于 c 运行时,即使在 go 1.10 和 Unix 中也是如此。
这里是简单回显服务器的例子:
package main
import (
"io"
"log"
"net"
)
func main() {
ln, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatal(err)
}
for {
conn, err := ln.Accept()
if err != nil {
log.Print(err)
continue
}
_, err = io.Copy(conn, conn) // blocked until the client closes the conn.
if err != nil {
log.Print(err)
continue
}
if err = conn.Close(); err != nil {
log.Print(err)
}
}
}
构建它并检查它的依赖关系:
$ go build ./libc_check.go
$ ldd ./libc_check
linux-vdso.so.1 => (0x00007ffe34fc1000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc005d4e000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc005984000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc005f6b000)
因为'the decision of how to run the resolver applies at run time, not build time' as the release note indicates.
要避免这种依赖性,请像这样使用 'netgo' 构建标签:
$ go build -tags netgo ./libc_check.go
$ ldd ./libc_check
not a dynamic executable
我找不到信息 golang 是否依赖于 c 运行时?如果它依赖于它,它是否在 go-binary 中静态编译以使 Go-app 无依赖地在任何地方工作?
这是关于 what C-runtime is
的话题如果你说的是 Go 编译器提供的可执行文件,那么答案是 "yes or no—it depends":
在大多数情况下,生成的可执行程序 不 依赖于 C run-time 库。
但是,在某些平台上,在某些情况下,C 运行时库会动态 链接。
特别是,当使用 DNS 解析时,Linux 上的 Go 版本 < 1.5 就是这种情况:运行时依赖于平台的 libc
实现来处理此类解析。
In 1.5 this has been reworked.
另一种可能的情况是 (IIRC) Solaris,它没有提供直接访问内核系统调用的稳定方法,requires routing these calls through the platform's libc
.
还有一种情况:使用cgo
,这是一个将Go代码与国外C代码接口的层。使用 cgo
使您的 Go 程序依赖于 C 运行时。请注意,您可能不会直接使用 cgo
,但您可能正在使用的一个或多个 third-party 包可以使用 cgo
,并且——传递地——你的程序最终取决于 C 运行时库.
我认为接受的答案是正确的,但是导入 'net' 包的二进制文件通常依赖于 c 运行时,即使在 go 1.10 和 Unix 中也是如此。
这里是简单回显服务器的例子:
package main
import (
"io"
"log"
"net"
)
func main() {
ln, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatal(err)
}
for {
conn, err := ln.Accept()
if err != nil {
log.Print(err)
continue
}
_, err = io.Copy(conn, conn) // blocked until the client closes the conn.
if err != nil {
log.Print(err)
continue
}
if err = conn.Close(); err != nil {
log.Print(err)
}
}
}
构建它并检查它的依赖关系:
$ go build ./libc_check.go
$ ldd ./libc_check
linux-vdso.so.1 => (0x00007ffe34fc1000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc005d4e000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc005984000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc005f6b000)
因为'the decision of how to run the resolver applies at run time, not build time' as the release note indicates.
要避免这种依赖性,请像这样使用 'netgo' 构建标签:
$ go build -tags netgo ./libc_check.go
$ ldd ./libc_check
not a dynamic executable