CentOS 8下CGO_ENABLED=1编译Go代码失败及yum源问题求助
CentOS 8下CGO_ENABLED=1编译Go代码失败及YUM源404问题解决
编译错误信息
执行CGO_ENABLED=1 go build时出现链接错误:
[nobin@CentOS9 tc-dump]$ CGO_ENABLED=1 go build # github.com/Asphaltt/tc-dump /usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1 /usr/bin/ld: cannot find -lresolv /usr/bin/ld: cannot find -lpthread /usr/bin/ld: cannot find -lc collect2: error: ld returned 1 exit status
系统信息
[nobin@CentOS9 tc-dump]$ uname -a Linux CentOS9 4.18.0-193.28.1.el8_2.x86_64 #1 SMP Thu Oct 22 00:20:22 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[nobin@CentOS9 tc-dump]$ cat /etc/os-release NAME="CentOS Linux" VERSION="8 (Core)" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="8" PLATFORM_ID="platform:el8" PRETTY_NAME="CentOS Linux 8 (Core)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:centos:centos:8" HOME_URL="https://www.centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-8" CENTOS_MANTISBT_PROJECT_VERSION="8" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="8"
YUM源及依赖安装失败信息
尝试安装glibc-static时:
[nobin@CentOS9 tc-dump]$ sudo yum install glibc-static CentOS-8 - AppStream 6.3 kB/s | 7.9 kB 00:01 Errors during downloading metadata for repository 'AppStream': - Status code: 404 for http://mirror.centos.org/centos/8/AppStream/x86_64/os/repodata/repomd.xml (IP: 50.2.190.74) Error: Failed to download metadata for repo 'AppStream': Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried CentOS-8 - Base 5.5 kB/s | 7.9 kB 00:01 Errors during downloading metadata for repository 'BaseOS': - Status code: 404 for http://mirror.centos.org/centos/8/BaseOS/x86_64/os/repodata/repomd.xml (IP: 69.167.138.255) Error: Failed to download metadata for repo 'BaseOS': Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried Ignoring repositories: AppStream, BaseOS Last metadata expiration check: 0:28:42 ago on বৃহস্পতিবার 04 এপ্রিল 2024 04:52:13 পূর্বাহ্ণ UTC. No match for argument: glibc-static Error: Unable to find a match: glibc-static
尝试安装libpthread-stubs时:
[nobin@CentOS9 tc-dump]$ sudo yum install libpthread-stubs CentOS-8 - AppStream 5.1 kB/s | 7.9 kB 00:01 Errors during downloading metadata for repository 'AppStream': - Status code: 404 for http://mirror.centos.org/centos/8/AppStream/x86_64/os/repodata/repomd.xml (IP: 104.206.99.122) Error: Failed to download metadata for repo 'AppStream': Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried CentOS-8 - Base 7.0 kB/s | 7.9 kB 00:01 Errors during downloading metadata for repository 'BaseOS': - Status code: 404 for http://mirror.centos.org/centos/8/BaseOS/x86_64/os/repodata/repomd.xml (IP: 192.240.166.165) Error: Failed to download metadata for repo 'BaseOS': Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried Ignoring repositories: AppStream, BaseOS Last metadata expiration check: 0:29:39 ago on বৃহস্পতিবার 04 এপ্রিল 2024 04:52:13 পূর্বাহ্ণ UTC. No match for argument: libpthread-stubs Error: Unable to find a match: libpthread-stubs
解决方案
1. 修复CentOS 8 YUM源
CentOS 8官方源已停止维护,需切换到归档源:
- 备份原有repo文件:
sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak sudo mv /etc/yum.repos.d/CentOS-AppStream.repo /etc/yum.repos.d/CentOS-AppStream.repo.bak
- 创建新的BaseOS和AppStream repo文件:
sudo tee /etc/yum.repos.d/CentOS-Base.repo <<EOF [base] name=CentOS-\$releasever - Base baseurl=http://vault.centos.org/centos/\$releasever/BaseOS/\$basearch/os/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial EOF
sudo tee /etc/yum.repos.d/CentOS-AppStream.repo <<EOF [appstream] name=CentOS-\$releasever - AppStream baseurl=http://vault.centos.org/centos/\$releasever/AppStream/\$basearch/os/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial EOF
- 清理并更新YUM缓存:
sudo yum clean all sudo yum makecache
2. 安装编译所需依赖
修复源后,安装缺失的静态库和编译工具:
sudo yum install -y glibc-static libstdc++-static gcc gcc-c++
注:libpthread属于glibc-static包的一部分,无需单独安装libpthread-stubs。
3. 重新编译Go代码
再次执行编译命令:
CGO_ENABLED=1 go build
内容的提问来源于stack exchange,提问作者NobinPegasus
相关产品推荐
相关产品推荐

