编译机版本:Linux ubuntu18 5.4.0-150-generic
交叉编译链:arm-none-linux-gnueabihf-gcc(version 9.2.1 20191025)
glib版本:glib-2.78.0
目标芯片:STM32PM157
参考文章:arm-linux-cc 4.4.3 交叉编译glib 2.70.2_交叉编译 glib-CSDN博客
如果你是编译2.6之前的版本,需要使用configure进行编译配置,对于依赖的库需要自己进行源码下载和交叉编译,编译glib依赖libffi、zlib、以及libpcre,也就是在编译glib之前需要交叉编译libffi、zlib、以及libpcre。
但在2.6版本之后,编译工具切换到了meson,meson可以将依赖的lib作为子项目,编译目标项目时可以自动下载子项目的source code并编译,因此不必要像makefile那样需要先编译子项目。
下面讲一下我是用meson进行交叉编译的过程,和遇到的问题。
首先在Index of /sources/glib/ (gnome.org)这里下载glib源码,这一需要注意的是,并不是位置在最下面的就是最新的,目前最新的版本是2.78并不是2.9,2.9其实是2.09.
首先需要准备一个交叉编译配置文件cross_file.txt,这里直接照抄
[properties]
pkg_config_libdir = ['/home/alientek/Desktop/arm_lib/lib/pkgconfig']
[binaries]
c = 'arm-none-linux-gnueabihf-gcc'
cpp = 'arm-none-linux-gnueabihf-g++'
ar = 'arm-none-linux-gnueabihf-ar'
strip = 'arm-none-linux-gnueabihf-strip'
pkgconfig = 'pkg-config'
[host_machine]
system = 'linux'
cpu_family = 'arm'
cpu = 'ARM9'
endian = 'little'
[build_machine]
system = 'linux'
cpu_family = 'x86_64'
cpu = 'i686'
endian = 'little'
pkg_config_libdir是pkg的配置目录,这里写的是我的交叉编译pkg配置目录。
arm-none-linux-gnueabihf-*这些是交叉编译链的命令,如果使用的是其他交叉编译链,这里需要根据对应的编译链名字配置。
host_machine部分是你的芯片配置,需要根据具体芯片配置了。
配置之后就可以开始用meson配置了,但是不要使用ubuntu下载的meson。因为Ubuntu下载的meson版本低,会导致编译报错
alientek@ubuntu18:~/Desktop/threelibrary/test/glib-2.78.0$ meson -v
0.45.1
alientek@ubuntu18:~/Desktop/threelibrary/test/glib-2.78.0$ meson --cross-file cross_file.txt --prefix=/home/alientek/Desktop/arm_lib -Dinternal_pcre=true -Dselinux=false -Dinstalled_tests=false -Ddtrace=false -Dfam=false -Dsystemtap=false -Dselinux=disabled -Dlibelf=disabled -Dlibmount=disabled build
The Meson build system
Version: 0.45.1
Source dir: /home/alientek/Desktop/threelibrary/test/glib-2.78.0
Build dir: /home/alientek/Desktop/threelibrary/test/glib-2.78.0/build
Build type: cross build
meson.build:15:13: ERROR: lexer
c_standards = {}
^
A full log can be found at /home/alientek/Desktop/threelibrary/test/glib-2.78.0/build/meson-logs/meson-log.txt
需要使用python下载的meson才行,或者自己下载最新的meson源码编译使用。
这里就使用python下载的方式进行了。
首先需要更新python3到3.7或更高,Ubuntu自带的python3.6,这个下载的meson是不行的,而且非常有趣。
错误尝试如下,
用python3.6下载meson后,运行meson,meson要求python3.7及以上的python版本;
升级python3.7后,运行meson报告meson版本过低错误;
使用python3.7更新meson后才能正常使用。
所以这里直接跳过这个坑,我们先升级python3.7.
sudo apt install python3.7
安装完成后需要配置系统python默认调用命令,
alientek@ubuntu18:~/Desktop/threelibrary/test/glib-2.78.0$ python3
Python 3.6.5 (default, Dec 9 2021, 17:04:37)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
表明默认的python3命令调用的是python3.6,此时你的电脑中已经同时拥有3.6和3.7版本了,需要重新配置。
配置命令如下:
sudo update-alternatives --list python
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1
sudo update-alternatives --list python
输出
/usr/bin/python3.7
此时你再调用python3用的就是3.7了
安装meson
alientek@ubuntu18:~/Desktop/threelibrary/test/glib-2.78.0$ pip3 install meson
Collecting meson
Using cached https://files.pythonhosted.org/packages/b0/74/73a0d5264f04c9193d2d588400ae913a167911b4342320cbdde3040b753d/meson-1.2.3-py3-none-any.whl
Installing collected packages: meson
Successfully installed meson-1.2.3
此时你就拥有了meson,现在已经可以进行编译,但是如果直接敲meson会调用Ubuntu安装的meson,python下载的meson在~/.local/bin/meson目录下,你或者直接把这个替换系统的,或者编译时敲全路径。
/home/alientek/.local/bin/meson --cross-file cross_file.txt --prefix=/home/alientek/Desktop/arm_lib -Dselinux=false -Dinstalled_tests=false -Ddtrace=false -Dsystemtap=false -Dselinux=disabled -Dlibelf=disabled -Dlibmount=disabled build
--prefix=是编译后的安装路径。
执行后需要下载依赖包,依赖包会从github下载,可能下载不下来,咱们可以使用迅雷或者镜像中下载后复制到build文件夹中,然后重新执行。
Looking for a fallback subproject for the dependency libpcre2-8
Downloading pcre2 source from https://github.com/PhilipHazel/pcre2/releases/download/pcre2-10.42/pcre2-10.42.tar.bz2
WARNING: failed to download with error: The read operation timed out. Trying after a delay...
<urlopen error [Errno 111] Connection refused>
WARNING: failed to download with error: could not get https://github.com/PhilipHazel/pcre2/releases/download/pcre2-10.42/pcre2-10.42.tar.bz2 is the internet available?. Trying after a delay...
<urlopen error [Errno 111] Connection refused>
WARNING: failed to download with error: could not get https://github.com/PhilipHazel/pcre2/releases/download/pcre2-10.42/pcre2-10.42.tar.bz2 is the internet available?. Trying after a delay...
<urlopen error [Errno 111] Connection refused>
WARNING: failed to download with error: could not get https://github.com/PhilipHazel/pcre2/releases/download/pcre2-10.42/pcre2-10.42.tar.bz2 is the internet available?. Trying after a delay...
<urlopen error [Errno 111] Connection refused>
WARNING: failed to download with error: could not get https://github.com/PhilipHazel/pcre2/releases/download/pcre2-10.42/pcre2-10.42.tar.bz2 is the internet available?. Trying after a delay...
<urlopen error [Errno 111] Connection refused>
A fallback URL could be specified using source_fallback_url key in the wrap file
meson.build:2103:10: ERROR: could not get https://github.com/PhilipHazel/pcre2/releases/download/pcre2-10.42/pcre2-10.42.tar.bz2 is the internet available?
A full log can be found at /home/alientek/Desktop/threelibrary/test/glib-2.78.0/build/meson-logs/meson-log.txt
下载正确之后
glib 2.78.0
Build environment
host cpu : arm
host endian : little
host system : linux
C Compiler : gcc
C++ Compiler : gcc
shared build : True
static build : False
Directories
prefix : /home/alientek/Desktop/arm_lib
bindir : /home/alientek/Desktop/arm_lib/bin
libexecdir : /home/alientek/Desktop/arm_lib/libexec
pkgdatadir : /home/alientek/Desktop/arm_lib/share/glib-2.0
datadir : /home/alientek/Desktop/arm_lib/share
includedir : /home/alientek/Desktop/arm_lib/include/glib-2.0
giomodulesdir : /home/alientek/Desktop/arm_lib/lib/gio/modules
localstatedir : /home/alientek/Desktop/arm_lib/var
runstatedir : /run
Options
selinux : False
libmount : False
xattr : False
man : False
dtrace : False
systemtap : False
sysprof : False
gtk_doc : False
bsymbolic_functions: True
force_posix_threads: False
tests : True
installed_tests : False
nls : auto
oss_fuzz : disabled
glib_debug : auto
glib_assert : True
glib_checks : True
libelf : disabled
multiarch : False
Subprojects
gvdb : YES
User defined options
Cross files : cross_file.txt
prefix : /home/alientek/Desktop/arm_lib
dtrace : false
installed_tests : false
libelf : disabled
libmount : disabled
selinux : disabled
systemtap : false
Found ninja-1.8.2 at /usr/bin/ninja
WARNING: Running the setup command as `meson [options]` instead of `meson setup [options]` is ambiguous and deprecated.
其中有一些编译选项可以在使用meson命令时添加进去,比如生成doc,可以添加-Dgtk_doc=true
然后
cd build
ninja
ninja install
这里就开始编译了,并安装到设置的目录。