无法运行程序:collect2: 错误:ld 返回 1 退出状态

huangapple go评论181阅读模式
英文:

Unable to run program: collect2: error: ld returned 1 exit status

问题

我已经在https://stackoverflow.com/questions/16710047/usr-bin-ld-cannot-find-lnameofthelibrary上查看了答案,但它们都不适用于这种情况。

我刚刚买了一台新笔记本电脑并设置了Go。一个简单的Hello World程序可以运行,但是当我尝试运行一个更复杂的程序时,我遇到了以下错误:

  1. go run .
  2. # runtime/cgo
  3. /usr/bin/ld: cannot find -lavcodec
  4. /usr/bin/ld: cannot find -lavformat
  5. /usr/bin/ld: cannot find -lavutil
  6. /usr/bin/ld: cannot find -lswscale
  7. /usr/bin/ld: cannot find -lswresample
  8. /usr/bin/ld: cannot find -lavdevice
  9. /usr/bin/ld: cannot find -lavfilter
  10. collect2: error: ld returned 1 exit status

我不确定错误发生在我的程序的哪个位置,因为上面是我得到的唯一输出。

  1. $ gcc --version
  2. gcc (Ubuntu 10.3.0-1ubuntu1) 10.3.0

编辑:
这里有一个能够重现错误的小示例:

  1. package main
  2. import "net/http"
  3. func main() {
  4. cli := &http.Client{}
  5. rsp, err := cli.Get("https://google.com")
  6. if err != nil {
  7. panic(err)
  8. }
  9. defer rsp.Body.Close()
  10. }

这是在全新安装的Ubuntu 21.04上全新安装的Go:

Go版本:
go version go1.16.8 linux/amd64

go env的输出:

  1. $ go env
  2. GO111MODULE="on"
  3. GOARCH="amd64"
  4. GOBIN="/home/bob/go/bin"
  5. GOCACHE="/home/bob/.cache/go-build"
  6. GOENV="/home/bob/.config/go/env"
  7. GOEXE=""
  8. GOFLAGS=""
  9. GOHOSTARCH="amd64"
  10. GOHOSTOS="linux"
  11. GOINSECURE=""
  12. GOMODCACHE="/home/bob/go/pkg/mod"
  13. GONOPROXY=""
  14. GONOSUMDB=""
  15. GOOS="linux"
  16. GOPATH="/home/bob/go"
  17. GOPRIVATE=""
  18. GOPROXY="https://proxy.golang.org"
  19. GOROOT="/snap/go/8408"
  20. GOSUMDB="off"
  21. GOTMPDIR=""
  22. GOTOOLDIR="/snap/go/8408/pkg/tool/linux_amd64"
  23. GOVCS=""
  24. GOVERSION="go1.16.8"
  25. GCCGO="gccgo"
  26. AR="ar"
  27. CC="gcc"
  28. CXX="g++"
  29. CGO_ENABLED="1"
  30. GOMOD="/home/bob/Desktop/projects/go.mod"
  31. CGO_CFLAGS="-I/home/bob/ffmpeg/include"
  32. CGO_CPPFLAGS=""
  33. CGO_CXXFLAGS="-g -O2"
  34. CGO_FFLAGS="-g -O2"
  35. CGO_LDFLAGS="-L/home/bob/ffmpeg/lib/ -lavcodec -lavformat -lavutil -lswscale -lswresample -lavdevice -lavfilter"
  36. PKG_CONFIG="pkg-config"
  37. GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2239100166=/tmp/go-build -gno-record-gcc-switches"
英文:

I've already reviewed the answers at https://stackoverflow.com/questions/16710047/usr-bin-ld-cannot-find-lnameofthelibrary and none of them work for this context.

I just got a new laptop and setting up Go. A simple hello world program works, but when I try a more complicated program, I get:

  1. go run .
  2. # runtime/cgo
  3. /usr/bin/ld: cannot find -lavcodec
  4. /usr/bin/ld: cannot find -lavformat
  5. /usr/bin/ld: cannot find -lavutil
  6. /usr/bin/ld: cannot find -lswscale
  7. /usr/bin/ld: cannot find -lswresample
  8. /usr/bin/ld: cannot find -lavdevice
  9. /usr/bin/ld: cannot find -lavfilter
  10. collect2: error: ld returned 1 exit status

I'm not sure where in my program the error occurs because the above is the only output I get.

  1. $ gcc --version
  2. gcc (Ubuntu 10.3.0-1ubuntu1) 10.3.0

EDIT:
Here's a small sample that reproduces the error:

  1. package main
  2. import "net/http"
  3. func main() {
  4. cli := &http.Client{}
  5. rsp, err := cli.Get("https://google.com")
  6. if err != nil {
  7. panic(err)
  8. }
  9. defer rsp.Body.Close()
  10. }

This is a brand new Go install on a brand new Ubuntu 21.04 install:

Go version:
go version go1.16.8 linux/amd64

Output of go env:

  1. $ go env
  2. GO111MODULE="on"
  3. GOARCH="amd64"
  4. GOBIN="/home/bob/go/bin"
  5. GOCACHE="/home/bob/.cache/go-build"
  6. GOENV="/home/bob/.config/go/env"
  7. GOEXE=""
  8. GOFLAGS=""
  9. GOHOSTARCH="amd64"
  10. GOHOSTOS="linux"
  11. GOINSECURE=""
  12. GOMODCACHE="/home/bob/go/pkg/mod"
  13. GONOPROXY=""
  14. GONOSUMDB=""
  15. GOOS="linux"
  16. GOPATH="/home/bob/go"
  17. GOPRIVATE=""
  18. GOPROXY="https://proxy.golang.org"
  19. GOROOT="/snap/go/8408"
  20. GOSUMDB="off"
  21. GOTMPDIR=""
  22. GOTOOLDIR="/snap/go/8408/pkg/tool/linux_amd64"
  23. GOVCS=""
  24. GOVERSION="go1.16.8"
  25. GCCGO="gccgo"
  26. AR="ar"
  27. CC="gcc"
  28. CXX="g++"
  29. CGO_ENABLED="1"
  30. GOMOD="/home/bob/Desktop/projects/go.mod"
  31. CGO_CFLAGS="-I/home/bob/ffmpeg/include"
  32. CGO_CPPFLAGS=""
  33. CGO_CXXFLAGS="-g -O2"
  34. CGO_FFLAGS="-g -O2"
  35. CGO_LDFLAGS="-L/home/bob/ffmpeg/lib/ -lavcodec -lavformat -lavutil -lswscale -lswresample -lavdevice -lavfilter"
  36. PKG_CONFIG="pkg-config"
  37. GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2239100166=/tmp/go-build -gno-record-gcc-switches"

答案1

得分: 5

标志“-l”用于指示链接器在构建应用程序时应使用的库。如果这是一台新的笔记本电脑,可能尚未安装这些库。您可以使用以下命令安装所需的库:

sudo apt-get update -y
sudo apt-get install -y libavfilter-dev
sudo apt-get install -y libavcodec-dev
sudo apt-get install -y libavutil-dev
sudo apt-get install -y libswscale-dev
sudo apt-get install -y libswresample-dev
sudo apt-get install -y libavdevice-dev
sudo apt-get install -y libavfilter-dev

英文:

Flag -l is used to indicate libraries that the linker is supposed to use to build your application. If it's a new laptop, it's possible that the libraries are not installed. You should be able to install the libraries needed using the following commands:

  1. sudo apt-get update -y
  2. sudo apt-get install -y libavfilter-dev
  3. sudo apt-get install -y libavcodec-dev
  4. sudo apt-get install -y libavutil-dev
  5. sudo apt-get install -y libswscale-dev
  6. sudo apt-get install -y libswresample-dev
  7. sudo apt-get install -y libavdevice-dev
  8. sudo apt-get install -y libavfilter-dev

huangapple
  • 本文由 发表于 2021年9月27日 02:24:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/69337771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定