You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Termux编译C项目报错libresolv.a缺失求解决方案

在Android Termux中编译sods项目解决libresolv.a缺失问题

尝试在Android Termux中编译C语言项目sods,该项目在Ubuntu、Debian等发行版可正常编译,但在Termux执行make时出现找不到libresolv.a的错误。原因是Android环境不存在该静态库,且项目的configure脚本未适配Android/Termux环境。

操作步骤

  • 安装依赖并克隆项目:
    pkg install git
    git clone https://github.com/msantos/sods.git
    
  • 进入客户端目录:
    cd sods/sdt
    
  • 执行配置脚本:
    ./configure
    
  • 执行编译:
    make
    
    此时返回libresolv.a不存在的错误。

相关文件内容

configure脚本

#!/bin/sh

OS=`uname -s`

case $OS in
    'Linux')
        if [ -e /usr/lib/libresolv.a ]; then
            STATIC_LIB=/usr/lib/libresolv.a
        else
            STATIC_LIB=/usr/lib/*/libresolv.a
        fi
        DEF="-DHAVE_ERRX"
    ;;    'Darwin'|'NetBSD')
        DEF="-DHAVE_ARC4RANDOM -DHAVE_ERRX"
        LIB="-lresolv"
    ;;    'OpenBSD')
        if [ ! -e /usr/local/lib/libbind/libbind.a ]; then
            echo "Please install libbind: sudo pkg_add libbind"
            exit 1
        fi
        DEF="-DHAVE_ARC4RANDOM -DHAVE_ERRX"
        LIB="-L/usr/local/lib/libbind -Wl,-rpath=/usr/local/lib/libbind -lbind -I/usr/local/include/bind"
    ;;    'FreeBSD')
        if [ ! -e /usr/local/lib/libbind.a ]; then
            echo "Please install libbind: sudo pkg add libbind"
            exit 1
        fi
        DEF="-DHAVE_ARC4RANDOM -DHAVE_ERRX"
        LIB="-L/usr/local/lib -lbind -I/usr/local/include/bind"
    ;;    'SunOS')
        if [ ! -e /opt/local/lib/libbind.a ]; then
            echo "Please install libbind: sudo pkg_add libbind"
            exit 1
        fi
        DEF="-DHAVE_ERRX"
        LIB="-lsocket -lnsl -L/opt/local/lib -Wl,-rpath=/opt/local/lib -lbind -I/opt/local/include/bind"
    ;;    *)
        echo "Guessing, adjust Makefile by hand ..."
        LIB="-lresolv"
    ;;    
esac

if [ -e /dev/urandom ]; then
    DEF="${DEF} -DHAVE_URANDOM"
fi

cat<<EOF>Makefile
CC ?= cc
RM ?= rm
APP=sdt

STATIC_LIB=${STATIC_LIB}
DEF=${DEF}
LIB=${LIB}

all:
        \$(CC) \$(DEF) -g -Wall \$(LIB) \\
    -Wall -Wextra -pedantic \\
    -Wshadow -Wpointer-arith -Wcast-qual \\
    -Wstrict-prototypes -Wmissing-prototypes \\
    -I../common \\
    -o \$(APP) \$(APP).c \$(APP)_dns.c \$(APP)_err.c \$(APP)_rand.c base32.c ../common/strtonum.c \$(STATIC_LIB) \\
    -Wl,-z,relro,-z,now -Wl,-z,noexecstack

clean:
        -@\$(RM) *.o \$(APP)
EOF

生成的Makefile

CC ?= cc
RM ?= rm
APP=sdt

STATIC_LIB=/usr/lib/*/libresolv.a
DEF=-DHAVE_ERRX -DHAVE_URANDOM
LIB=

all:
        $(CC) $(DEF) -g -Wall $(LIB) \
    -Wall -Wextra -pedantic \
    -Wshadow -Wpointer-arith -Wcast-qual \
    -Wstrict-prototypes -Wmissing-prototypes \
    -I../common \
    -o sdt sdt.c sdt_dns.c sdt_err.c sdt_rand.c base32.c ../common/strtonum.c $(STATIC_LIB) \
    -Wl,-z,relro,-z,now -Wl,-z,noexecstack

clean:
        -@$(RM) *.o $(APP)

报错输出

cc -DHAVE_ERRX -DHAVE_URANDOM -g -Wall  \
    -Wall -Wextra -pedantic \
    -Wshadow -Wpointer-arith -Wcast-qual \
    -Wstrict-prototypes -Wmissing-prototypes \
    -I../common \
    -o sdt sdt.c sdt_dns.c sdt_err.c sdt_rand.c base32.c ../common/strtonum.c /usr/lib/libresolv.a \
    -Wl,-z,relro,-z,now -Wl,-z,noexecstack
clang: error: no such file or directory: '/usr/lib/*/libresolv.a'
make: *** [Makefile:10: all] Error 1

解决办法

方法1:修改configure脚本适配Termux

编辑sdt/configure脚本,在Linux分支中加入Termux环境检测,使用动态链接替代静态库:

#!/bin/sh

OS=`uname -s`

case $OS in
    'Linux')
        # 检测是否为Termux环境
        if [ -n "$TERMUX_VERSION" ] || [ -d "/data/data/com.termux" ]; then
            STATIC_LIB=""
            LIB="-lresolv"
            DEF="-DHAVE_ERRX"
        else
            if [ -e /usr/lib/libresolv.a ]; then
                STATIC_LIB=/usr/lib/libresolv.a
            else
                STATIC_LIB=/usr/lib/*/libresolv.a
            fi
            DEF="-DHAVE_ERRX"
        fi
    ;;
    'Darwin'|'NetBSD')
        DEF="-DHAVE_ARC4RANDOM -DHAVE_ERRX"
        LIB="-lresolv"
    ;;
    'OpenBSD')
        if [ ! -e /usr/local/lib/libbind/libbind.a ]; then
            echo "Please install libbind: sudo pkg_add libbind"
            exit 1
        fi
        DEF="-DHAVE_ARC4RANDOM -DHAVE_ERRX"
        LIB="-L/usr/local/lib/libbind -Wl,-rpath=/usr/local/lib/libbind -lbind -I/usr/local/include/bind"
    ;;
    'FreeBSD')
        if [ ! -e /usr/local/lib/libbind.a ]; then
            echo "Please install libbind: sudo pkg add libbind"
            exit 1
        fi
        DEF="-DHAVE_ARC4RANDOM -DHAVE_ERRX"
        LIB="-L/usr/local/lib -lbind -I/usr/local/include/bind"
    ;;
    'SunOS')
        if [ ! -e /opt/local/lib/libbind.a ]; then
            echo "Please install libbind: sudo pkg_add libbind"
            exit 1
        fi
        DEF="-DHAVE_ERRX"
        LIB="-lsocket -lnsl -L/opt/local/lib -Wl,-rpath=/opt/local/lib -lbind -I/opt/local/include/bind"
    ;;
    *)
        echo "Guessing, adjust Makefile by hand ..."
        LIB="-lresolv"
    ;;    
esac

if [ -e /dev/urandom ]; then
    DEF="${DEF} -DHAVE_URANDOM"
fi

cat<<EOF>Makefile
CC ?= cc
RM ?= rm
APP=sdt

STATIC_LIB=${STATIC_LIB}
DEF=${DEF}
LIB=${LIB}

all:
        \$(CC) \$(DEF) -g -Wall \$(LIB) \\
    -Wall -Wextra -pedantic \\
    -Wshadow -Wpointer-arith -Wcast-qual \\
    -Wstrict-prototypes -Wmissing-prototypes \\
    -I../common \\
    -o \$(APP) \$(APP).c \$(APP)_dns.c \$(APP)_err.c \$(APP)_rand.c base32.c ../common/strtonum.c \$(STATIC_LIB) \\
    -Wl,-z,relro,-z,now -Wl,-z,noexecstack

clean:
        -@\$(RM) *.o \$(APP)
EOF

修改后重新执行./configure和make即可完成编译。

方法2:手动修改生成的Makefile

如果不想修改configure脚本,可直接编辑生成的Makefile:

  1. 将STATIC_LIB=/usr/lib/*/libresolv.a改为STATIC_LIB=
  2. 将LIB=改为LIB=-lresolv

修改后的Makefile如下:

CC ?= cc
RM ?= rm
APP=sdt

STATIC_LIB=
DEF=-DHAVE_ERRX -DHAVE_URANDOM
LIB=-lresolv

all:
        $(CC) $(DEF) -g -Wall $(LIB) \
    -Wall -Wextra -pedantic \
    -Wshadow -Wpointer-arith -Wcast-qual \
    -Wstrict-prototypes -Wmissing-prototypes \
    -I../common \
    -o sdt sdt.c sdt_dns.c sdt_err.c sdt_rand.c base32.c ../common/strtonum.c $(STATIC_LIB) \
    -Wl,-z,relro,-z,now -Wl,-z,noexecstack

clean:
        -@$(RM) *.o $(APP)

保存后执行make即可完成编译。

原理说明

Termux环境中不提供静态库libresolv.a,但包含动态库libresolv.so,通过动态链接-lresolv即可满足项目的DNS解析依赖需求,无需静态链接。

内容的提问来源于stack exchange,提问作者AhmedSCRIPT Hacker بالعربية

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 06:05:59