用法(CGO_ENABLED=1 默认值)
CGO_ENABLED=1 go build -o main1 main.go
CGO_ENABLED=0 go build -o main0 main.go
1 package main
2
3 import (
4 "fmt"
5 "net/http"
6 )
7
8 func main() {
9 http.HandleFunc("/index", IndexHandler)
10 http.ListenAndServe(":6060", nil)
11 }
12
13 func IndexHandler(w http.ResponseWriter, r *http.Request) {
14 fmt.Fprintln(w, "hi,golang")
15 }
作用
当CGO_ENABLED=1, 进行编译时, 会将文件中引用libc的库(比如常用的net包),以动态链接的方式生成目标文件。
当CGO_ENABLED=0, 进行编译时, 则会把在目标文件中未定义的符号(外部函数)一起链接到可执行文件中。
通过ldd obj-file 查看引用的动态链接库


通过nm obj-file 查看符号表

ldd: print shared library denpencies
nm: list symbols from object files

网友评论