交叉编译Python2.7.13/3.5.7时libpython.so未定义引用错误求助
交叉编译Python 2.7.13/3.5.7时出现
major/minor/makedev未定义引用错误 环境与配置参数
正在交叉编译Python 2.7.13和Python 3.5.7,使用的configure命令如下:
CONFIG_SITE=config.site ./configure \ --host=arm-linux-gnueabi \ --build=x86_64-pc-linux-gnu \ CC=/toolchain/gcc-arm-8.2-2018.08-x86_64-arm-linux-gnueabi/bin/arm-linux-gnueabi-gcc \ RANLIB=/toolchain/gcc-arm-8.2-2018.08-x86_64-arm-linux-gnueabi/bin/arm-linux-gnueabi-ranlib \ READELF=/toolchain/gcc-arm-8.2-2018.08-x86_64-arm-linux-gnueabi/bin/arm-linux-gnueabi-readelf \ --enable-shared --enable-ipv6 --without-cxx-main --without-doc-strings --with-system-ffi --with-pydebug
编译错误详情
配置成功生成Makefile后,执行make时出现链接错误:
/toolchain/arm/gcc-arm-8.2-2018.08-x86_64-arm-linux-gnueabi/bin/arm-linux-gnueabi-gcc -Xlinker -export-dynamic -o python \ Modules/python.o \ -L. -lpython2.7 -lpthread -ldl -lpthread -lutil -lm ./libpython2.7.so: undefined reference to `minor' ./libpython2.7.so: undefined reference to `major' ./libpython2.7.so: undefined reference to `makedev' collect2: error: ld returned 1 exit status make: *** [python] Error 1
工具链信息
使用的交叉编译工具链为gcc-arm-8.2-2018.08-x86_64-arm-linux-gnueabi。
符号检查结果
通过readelf检查Modules/posixmodule.o,发现major、minor和makedev属于未定义(UNDEF)符号:
readelf -s posixmodule.o | grep -i "major" 409: 00004cb8 20 FUNC LOCAL DEFAULT 1 os_major_impl 410: 00004ccc 124 FUNC LOCAL DEFAULT 1 os_major 1288: 0000065c 1 OBJECT LOCAL DEFAULT 5 os_major__doc__ 1629: 00000000 0 NOTYPE GLOBAL DEFAULT UND major readelf -s posixmodule.o | grep -i "minor" 404: 00004c28 20 FUNC LOCAL DEFAULT 1 os_minor_impl 405: 00004c3c 124 FUNC LOCAL DEFAULT 1 os_minor 1287: 00000658 1 OBJECT LOCAL DEFAULT 5 os_minor__doc__ 1628: 00000000 0 NOTYPE GLOBAL DEFAULT UND minor
解决方法
方法1:添加-D_GNU_SOURCE编译宏
major、minor、makedev属于GNU扩展函数,需要定义_GNU_SOURCE才能让编译器暴露这些函数的声明。在configure时添加CFLAGS参数:
CONFIG_SITE=config.site ./configure \ --host=arm-linux-gnueabi \ --build=x86_64-pc-linux-gnu \ CC=/toolchain/gcc-arm-8.2-2018.08-x86_64-arm-linux-gnueabi/bin/arm-linux-gnueabi-gcc \ RANLIB=/toolchain/gcc-arm-8.2-2018.08-x86_64-arm-linux-gnueabi/bin/arm-linux-gnueabi-ranlib \ READELF=/toolchain/gcc-arm-8.2-2018.08-x86_64-arm-linux-gnueabi/bin/arm-linux-gnueabi-readelf \ CFLAGS="-D_GNU_SOURCE" \ --enable-shared --enable-ipv6 --without-cxx-main --without-doc-strings --with-system-ffi --with-pydebug
添加后重新执行make。
方法2:修改Python源码头文件
如果方法1无效,直接修改Modules/posixmodule.c,在文件开头添加:
#define _GNU_SOURCE #include <sys/sysmacros.h>
保存后重新执行configure和make。
方法3:检查目标系统库支持
确认目标ARM系统的C库(如glibc)版本支持这些函数,若目标系统库版本过旧,可能需要升级工具链或调整编译选项。
内容的提问来源于stack exchange,提问作者strikersps
相关产品推荐
相关产品推荐

