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

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

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程序可以运行,但是当我尝试运行一个更复杂的程序时,我遇到了以下错误:

go run .
# runtime/cgo
/usr/bin/ld: cannot find -lavcodec
/usr/bin/ld: cannot find -lavformat
/usr/bin/ld: cannot find -lavutil
/usr/bin/ld: cannot find -lswscale
/usr/bin/ld: cannot find -lswresample
/usr/bin/ld: cannot find -lavdevice
/usr/bin/ld: cannot find -lavfilter
collect2: error: ld returned 1 exit status

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

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

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

package main

import "net/http"

func main() {
	cli := &http.Client{}
	rsp, err := cli.Get("https://google.com")
	if err != nil {
		panic(err)
	}

	defer rsp.Body.Close()

}

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

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

go env的输出:

$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/home/bob/go/bin"
GOCACHE="/home/bob/.cache/go-build"
GOENV="/home/bob/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/bob/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/bob/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org"
GOROOT="/snap/go/8408"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/snap/go/8408/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.8"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/bob/Desktop/projects/go.mod"
CGO_CFLAGS="-I/home/bob/ffmpeg/include"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-L/home/bob/ffmpeg/lib/ -lavcodec -lavformat -lavutil -lswscale -lswresample -lavdevice -lavfilter"
PKG_CONFIG="pkg-config"
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:

go run .
# runtime/cgo
/usr/bin/ld: cannot find -lavcodec
/usr/bin/ld: cannot find -lavformat
/usr/bin/ld: cannot find -lavutil
/usr/bin/ld: cannot find -lswscale
/usr/bin/ld: cannot find -lswresample
/usr/bin/ld: cannot find -lavdevice
/usr/bin/ld: cannot find -lavfilter
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.

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

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

package main

import "net/http"

func main() {
	cli := &http.Client{}
	rsp, err := cli.Get("https://google.com")
	if err != nil {
		panic(err)
	}

	defer rsp.Body.Close()

}

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:

$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/home/bob/go/bin"
GOCACHE="/home/bob/.cache/go-build"
GOENV="/home/bob/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/bob/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/bob/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org"
GOROOT="/snap/go/8408"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/snap/go/8408/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.8"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/bob/Desktop/projects/go.mod"
CGO_CFLAGS="-I/home/bob/ffmpeg/include"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-L/home/bob/ffmpeg/lib/ -lavcodec -lavformat -lavutil -lswscale -lswresample -lavdevice -lavfilter"
PKG_CONFIG="pkg-config"
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:

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

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:

确定