• 交叉编译glib


    依赖图

    glib
    libffi
    zlib
    gettext
    libiconv

    先创建一个输出目录

    mkdir bluez_output
    cd bluez_output
    export bluez_output=/home/liefyuan/luckfox-pico-main/project/app/bluez/bluez_output		#设置环境变量
    
    • 1
    • 2
    • 3

    非常实用的操作。

    安装libffi

    libffi是可移植外部函数接口库,是构建GLib的先决条件。它是一个接口,允许用一种语言编写的代码调用用另一种语言编写的代码。

    • libffi下载路径:https://github.com/libffi/libffi/releases/

    • libffi下载版本:libffi-3.4.3.tar.gz

    手动下载后,执行下面命令:

    tar -zxvf libffi-3.4.3.tar.gz		# 解压
    cd libffi-3.4.3/					# 切换目录
    ./configure \
    	--prefix=$bluez_output CC="arm-rockchip830-linux-uclibcgnueabihf-gcc -I$bluez_output/include -L$bluez_output/lib" \
    	--host=arm-rockchip830-linux-uclibcgnueabihf
    	
    make -j8
    make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    安装完libffi后,再次回到GLib进行安装,可见报错:fatal error: zlib.h: No such file or directory

    说明GLib仍然依赖zlib库,需要安装zlib

    安装zlib

    zlib是一个压缩库,是构建GLib的先决条件。

    zlib下载地址:https://github.com/madler/zlib/releases/tag/v1.3

    zlib下载版本:zlib-1.3.tar.gz

    手动下载后,执行下面命令:

    tar -zxvf zlib-1.3.tar.gz
    cd zlib-1.3/
    ./configure \
    	--prefix=$bluez_output \
    	--includedir=$bluez_output/include \
    	--libdir=$bluez_output/lib
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    vim Makefile 将CC变量改为自己的交叉编译器arm-linux-gnueabihf-gcc

    CC=arm-rockchip830-linux-uclibcgnueabihf-gcc
    LDSHARED=arm-rockchip830-linux-uclibcgnueabihf-gcc -shared -Wl,-soname,libz.so.1,--version-script,zlib.map
    CPP=arm-rockchip830-linux-uclibcgnueabihf-g++ -E
    AR=arm-rockchip830-linux-uclibcgnueabihf-ar
    RANLIB=arm-rockchip830-linux-uclibcgnueabihf-ranlib
    
    • 1
    • 2
    • 3
    • 4
    • 5
    make -j8
    make install
    
    • 1
    • 2

    安装libiconv

    libiconv 在Linux的交叉编译与使用

    官网:https://www.gnu.org/software/libiconv/#TOCdownloading
    下载地址:https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.17.tar.gz

    手动下载以后:

    tar -xzvf libiconv-1.17.tar.gz 			# 解压
    cd libiconv-1.17/							# 进入目录
    
    • 1
    • 2
    ./configure \
    	--prefix=$bluez_output CC="arm-rockchip830-linux-uclibcgnueabihf-gcc -I$bluez_output/include -L$bluez_output/lib" \
    	--host=arm-rockchip830-linux-uclibcgnueabihf \
    	--enable-static=yes \
    	--enable-shared=no
    
    make -j8
    make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    然后所有的都安装在bluez_output目录下了。

    安装 gettext

    交叉编译-20:gettext移植到君正平台

    • 下载地址:https://ftp.gnu.org/pub/gnu/gettext/
    • https://ftp.gnu.org/pub/gnu/gettext/gettext-0.20.tar.gz

    手动下载以后:

    tar -xzvf gettext-0.20.tar.gz 			# 解压
    cd gettext-0.20/					    # 进入目录
    
    • 1
    • 2
    ./configure \
    	--prefix=$bluez_output \
    	--host=arm-rockchip830-linux-uclibcgnueabihf  \
    	CC="arm-rockchip830-linux-uclibcgnueabihf-gcc -I$bluez_output/include -L$bluez_output/lib" CFLAGS="-fPIC" \
    	--enable-static=yes \
    	--enable-shared=no
    
    make -j8
    make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    编译花了5分钟左右。

    交叉编译GLib

    参考:Glib 2.45.3 交叉编译完整版

    GLib 是一个通用的、可移植的实用程序库,它提供了许多有用的数据类型、宏、类型转换、字符串实用程序、文件实用程序、主循环抽象等。

    新版本的GLib需要Meson工具编译,因此我们这里使用2.46.2版本的,可以通过cmake编译

    wget https://download.gnome.org/sources/glib/2.46/glib-2.46.2.tar.xz	#下载GLib
    tar -xvf glib-2.46.2.tar.xz 			# 解压
    cd glib-2.46.2/							# 进入目录
    
    • 1
    • 2
    • 3

    然后手动创建arm-linux.cache文件,手动写入下面内容:

    touch arm-linux.cache		
    
    • 1

    创建文件, 添加下面内容

    glib_cv_long_long_format=ll
    glib_cv_stack_grows=no
    glib_cv_working_bcopy=no
    glib_cv_sane_realloc=yes
    glib_cv_have_strlcpy=no
    glib_cv_va_val_copy=yes
    glib_cv_rtldglobal_broken=no
    glib_cv_uscore=no
    ac_cv_func_posix_getpwuid_r=yes
    ac_cv_func_nonposix_getpwuid_r=no
    ac_cv_func_posix_getgrgid_r=no
    glib_cv_use_pid_surrogate=no
    ac_cv_func_printf_unix98=no
    ac_cv_func_vsnprintf_c99=no
    ac_cv_path_GLIB_COMPILE_SCHEMAS=yes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    然后接着执行下面命令:

    ./configure \
    	--prefix=$bluez_output \
    	CC="arm-rockchip830-linux-uclibcgnueabihf-gcc -I$bluez_output/include -L$bluez_output/lib" \
    	--host=arm-rockchip830-linux-uclibcgnueabihf \
    	--cache-file=arm-linux.cache  \
    	--enable-static=yes \
    	--enable-shared=no
    	
    make -j8
    make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    configure阶段报错1:

    checking for python extension module directory... ${exec_prefix}/lib/python2.7/site-packages
    checking for iconv_open... no
    checking for libiconv_open in -liconv... no
    checking for iconv_open in -liconv... no
    configure: error: *** No iconv() implementation found in C library or libiconv
    
    • 1
    • 2
    • 3
    • 4
    • 5

    交叉编译libiconv!!

    好的,不报这个错了!但是有报错2了!!

    configure阶段报错2:

    checking for LC_MESSAGES... yes
    checking libintl.h usability... no
    checking libintl.h presence... no
    checking for libintl.h... no
    configure: error:
    *** You must have either have gettext support in your C library, or use the
    *** GNU gettext library. (http://www.gnu.org/software/gettext/gettext.html
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    交叉编译 gettext!!

    configure阶段报错3:

    checking for ZLIB... yes
    checking for LIBFFI... no
    configure: error: Package requirements (libffi >= 3.0.0) were not met:
    
    No package 'libffi' found
    
    Consider adjusting the PKG_CONFIG_PATH environment variable if you
    installed software in a non-standard prefix.
    
    Alternatively, you may set the environment variables LIBFFI_CFLAGS
    and LIBFFI_LIBS to avoid the need to call pkg-config.
    See the pkg-config man page for more details.
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    确认已经安装,但是还是没有识别出来。

    “FFI” 的全名是Foreign Function Interface,通常指的是允许以一种语言编写的代码调用另一种语言的代码。而libffi库只提供了最底层的、与架构相关的、完整的"FFI",在它之上必须有一层来负责管理两种语言之间参数的格式转换。

    Ubuntu安装一个软件就可以了:

    sudo apt-get install libffi-dev
    
    • 1

    make阶段报错3:

    解决办法:gdate error: format not a string literal -Werror=format-nonliteral 错误详解

      CC       libglib_2_0_la-gdir.lo
      CC       libglib_2_0_la-genviron.lo
    gdate.c: In function 'g_date_strftime':
    gdate.c:2497:7: error: format not a string literal, format string not checked [-Werror=format-nonliteral]
           tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
           ^~~~~~
      CC       libglib_2_0_la-gerror.lo
      CC       libglib_2_0_la-gfileutils.lo
      CC       libglib_2_0_la-ggettext.lo
      CC       libglib_2_0_la-ghash.lo
      CC       libglib_2_0_la-ghmac.lo
      CC       libglib_2_0_la-ghook.lo
    cc1: some warnings being treated as errors
    make[4]: *** [Makefile:1845: libglib_2_0_la-gdate.lo] Error 1
    make[4]: *** Waiting for unfinished jobs....
    make[4]: Leaving directory '/home/liefyuan/luckfox-pico-main/project/app/bluez/glib-2.46.2/glib'
    make[3]: *** [Makefile:2511: all-recursive] Error 1
    make[3]: Leaving directory '/home/liefyuan/luckfox-pico-main/project/app/bluez/glib-2.46.2/glib'
    make[2]: *** [Makefile:1224: all] Error 2
    make[2]: Leaving directory '/home/liefyuan/luckfox-pico-main/project/app/bluez/glib-2.46.2/glib'
    make[1]: *** [Makefile:1212: all-recursive] Error 1
    make[1]: Leaving directory '/home/liefyuan/luckfox-pico-main/project/app/bluez/glib-2.46.2'
    make: *** [Makefile:854: all] Error 2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    ChatGPT解释到:这个错误提示意味着在代码中的g_date_strftime函数的调用中,locale_format参数作为格式字符串传递给了strftime函数,但是编译器警告这种用法可能存在潜在的安全问题。由于这个参数不是编译时固定的字符串,编译器无法在编译时对其进行格式检查。编译器给出了-Werror=format-nonliteral选项,将这个警告视为错误。
    这种警告的目的是防止可能的格式字符串漏洞,其中攻击者可以通过传递恶意格式字符串来执行任意代码。为了解决这个问题,你可以使用字符串常量作为格式字符串,或者对传递给strftime函数的格式字符串进行其他安全验证。这样编译器就能够对格式字符串进行静态检查,减少潜在的安全风险。

    解决办法:https://www.cnblogs.com/yuandaozhe/p/14365389.html
    编辑报错的文件

     vim ./glib/gdate.c
    
    • 1

    添加如下代码

    #pragma GCC diagnostic ignored "-Wformat-nonliteral"
    
    • 1

    就在包含头文件的地方加就行了

    make阶段报错4:

      CCLD     gresource
    /home/liefyuan/luckfox-pico-main/project/app/bluez/bluez_output/lib/libz.so: file not recognized: file format not recognized
    collect2: error: ld returned 1 exit status
    make[4]: *** [Makefile:2121: gio-querymodules] Error 1
    make[4]: *** Waiting for unfinished jobs....
    /home/liefyuan/luckfox-pico-main/project/app/bluez/bluez_output/lib/libz.so: file not recognized: file format not recognized
    collect2: error: ld returned 1 exit status
    /home/liefyuan/luckfox-pico-main/project/app/bluez/bluez_output/lib/libz.so: file not recognized: filemake[4]: *** [Makefile:2125: glib-compile-resources] Error 1
     format not recognized
    collect2: error: ld returned 1 exit status
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    xxx.dll: file not recognized: file format not recognized
    collect2.exe: error: ld returned 1 exit status
    出现类似错误一般是动态链接库版本(32位/64位)不对的问题造成的。

    读了一下文件,吓我一跳!

    $ file libz.so.1.3
    libz.so.1.3: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=5382c6bb50b8ff4685d2f9d49ef3678f77e530f8, not stripped
    
    • 1
    • 2

    说明zlib没有编译好!重新编译安装OK!

    make阶段报错5:

     CC       libgio_2_0_la-gdesktopappinfo.lo
    gzlibcompressor.c:26:10: fatal error: zlib.h: No such file or directory
     #include 
              ^~~~~~~~
    compilation terminated.
    Makefile:3169: recipe for target 'libgio_2_0_la-gzlibcompressor.lo' failed
    make[4]: *** [libgio_2_0_la-gzlibcompressor.lo] Error 1
    make[4]: *** Waiting for unfinished jobs....
    gzlibdecompressor.c:26:10: fatal error: zlib.h: No such file or directory
     #include 
              ^~~~~~~~
    compilation terminated.
    Makefile:3176: recipe for target 'libgio_2_0_la-gzlibdecompressor.lo' failed
    make[4]: *** [libgio_2_0_la-gzlibdecompressor.lo] Error 1
    make[4]: Leaving directory '/home/liefyuan/rv1103/luckfox-pico-main-origin/project/app/bluez/glib-2.46.2/gio'
    Makefile:4094: recipe for target 'all-recursive' failed
    make[3]: *** [all-recursive] Error 1
    make[3]: Leaving directory '/home/liefyuan/rv1103/luckfox-pico-main-origin/project/app/bluez/glib-2.46.2/gio'
    Makefile:1861: recipe for target 'all' failed
    make[2]: *** [all] Error 2
    make[2]: Leaving directory '/home/liefyuan/rv1103/luckfox-pico-main-origin/project/app/bluez/glib-2.46.2/gio'
    Makefile:1212: recipe for target 'all-recursive' failed
    make[1]: *** [all-recursive] Error 1
    make[1]: Leaving directory '/home/liefyuan/rv1103/luckfox-pico-main-origin/project/app/bluez/glib-2.46.2'
    Makefile:854: recipe for target 'all' failed
    make: *** [all] Error 2
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    前面的zlib没有make install

  • 相关阅读:
    Android 类加载ClassLoader
    JSD-2204-酷莎商城(后端)-Day17,18
    办鹿uniapp小程序(一)
    web前端——简单的网页布局案列
    【Vue3响应式原理#01】Reactivity
    nginx proxy_set_header设置、自定义header
    鼠标坐标获取及编程应用
    JSP1410 科研项目团队建设经费管理系统mysql
    每日一题:吃透大文件上传问题(附可运行的前后端源码)
    防抖和节流
  • 原文地址:https://blog.csdn.net/qq_28877125/article/details/133951507