Golang编译错误:未定义的符号引用 'pthread_key_delete@@GLIBC_2.2.5'

Golang编译错误:未定义的符号引用 ‘pthread_key_delete@@GLIBC_2.2.5’ 朋友们好…

我正在使用 Slackware 15.0、GoLang 1.16.5(go version go1.16.5 gccgo (GCC) 11.2.0 linux/amd64)以及 fyne(来自 fyne.io 的 GUI API)。

我测试了非常简单的代码如下…

package main

import (
"fmt"
"fyne.io/fyne/app"
)

func main() {
fmt.Println("XStopMotion dengan Fyne-GUI!")
myapp := app.New()
jendela := myapp.NewWindow("XStopMotion with Fyne-GUI by Ilham-Firdaus.")
jendela.ShowAndRun()
}

这个简单的代码已保存为 “xstopmotion_fyne.go”

我已经执行了…

ceo[GUI-dengan-Fyne]$ go mod init xstopmotion_fyne
ceo[GUI-dengan-Fyne]$ go mod tidy
ceo[GUI-dengan-Fyne]$ go run xstopmotion_fyne.go
/usr/bin/ld: $WORK/b001/_pkg2_.a(_x004.o): undefined reference to symbol 'pthread_key_delete@@GLIBC_2.2.5'
/usr/bin/ld: /lib64/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: Fehler: ld gab 1 als Ende-Status zurück
ceo[GUI-dengan-Fyne]$

但我的代码无法正常工作。它卡在这个错误信息上:“/usr/bin/ld: $WORK/b001/pkg2.a(_x004.o): undefined reference to symbol ‘pthread_key_delete@@GLIBC_2.2.5’”

有哪位好心人能告诉我应该怎么做才能运行我的简单代码吗?

我也尝试过这个方法

ceo[GUI-dengan-Fyne]$ go run -gccgoflags "-L=/lib64 -l pthread" xstopmotion_fyne.go
flag provided but not defined: -l
usage: go run [build flags] [-exec xprog] package [arguments...]
Run 'go help run' for details.
ceo[GUI-dengan-Fyne]$

更多关于Golang编译错误:未定义的符号引用 'pthread_key_delete@@GLIBC_2.2.5'的实战教程也可以访问 https://www.itying.com/category-94-b0.html

2 回复

我通过以下方法解决了这个问题

ceo[GUI-dengan-Fyne]$ go run -gccgoflags ‘-L=/lib64 -l pthread’ xstopmotion_fyne.go
XStopMotion dengan Fyne-GUI!
^Csignal: interrupt
ceo[GUI-dengan-Fyne]$

更多关于Golang编译错误:未定义的符号引用 'pthread_key_delete@@GLIBC_2.2.5'的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


这个编译错误是因为使用gccgo编译器时缺少pthread库的链接。gccgo需要显式链接系统线程库。以下是解决方案:

解决方案1:使用正确的编译标志

go run -compiler gccgo -gccgoflags '-lpthread' xstopmotion_fyne.go

或者编译为可执行文件:

go build -compiler gccgo -gccgoflags '-lpthread' xstopmotion_fyne.go

解决方案2:使用标准Go编译器(推荐)

切换到官方的Go编译器(gc),它默认链接pthread:

# 首先检查是否安装了标准Go编译器
go version

# 如果显示的是gccgo,安装标准Go编译器
# 从 https://golang.org/dl/ 下载并安装

# 然后使用标准编译器编译
go run xstopmotion_fyne.go

解决方案3:设置环境变量

# 设置链接器标志
export CGO_LDFLAGS="-lpthread"

# 然后运行
go run xstopmotion_fyne.go

解决方案4:创建构建脚本

创建一个build.sh脚本:

#!/bin/bash
export CGO_LDFLAGS="-lpthread"
go build -compiler gccgo xstopmotion_fyne.go
./xstopmotion_fyne

或者直接在Makefile中:

build:
    CGO_LDFLAGS="-lpthread" go build -compiler gccgo xstopmotion_fyne.go

run:
    CGO_LDFLAGS="-lpthread" go run -compiler gccgo xstopmotion_fyne.go

验证解决方案

测试标准编译器是否工作:

// test_compiler.go
package main

import (
    "fmt"
    "runtime"
)

func main() {
    fmt.Printf("Go version: %s\n", runtime.Version())
    fmt.Printf("Compiler: %s\n", runtime.Compiler)
}

运行:

go run test_compiler.go

如果输出显示gc而不是gccgo,问题应该解决。对于GUI开发,建议使用标准Go编译器(gc),因为它对cgo和系统库的集成更好。

回到顶部