打开dbus官网,可以看到“What is D-Bus?”章节有以下说明:
The low-level libdbus reference library has no required dependencies; the reference bus daemon’s only required dependency is an XML parser (expat). Higher-level bindings specific to particular frameworks (Qt, GLib, Java, C#, Python, etc.) add more dependencies, but can make more assumptions and are thus much simpler to use.
意思大概就是编译dbus只需要依赖第三方XML库(expat),高级的封装才会依赖更多的库。而文章接下来介绍使用arm-linux-gnueaihf-来交叉编译dbus和它的高级封装dbus-glib。关于dbus的交叉编译,可以从dbus源码包的README文件了解:
Bootstrapping D-Bus on new platforms
A full build of D-Bus, with all regression tests enabled and run, has some
dependencies which themselves depend on D-Bus, either for compilation or
for some of their regression tests: GLib, dbus-glib and dbus-python are
currently affected.To avoid circular dependencies, when bootstrapping D-Bus for the first time
on a new OS or CPU architecture, you can either cross-compile some of
those components, or choose the build order and options carefully:
- build and install D-Bus without tests
- do not use the —enable-modular-tests=yes configure option
- do not use the —enable-tests=yes configure option
- build and install GLib, again without tests
- use those versions of libdbus and GLib to build and install dbus-glib
- … and use those to install dbus-python
- rebuild libdbus; this time you can run all of the tests
- rebuild GLib; this time you can run all of the tests
意思大概就是在新的平台中引入D-bus时(通俗的说就是交叉编译),为了避免更多的依赖,在交叉编译dbus的时候就不能引入更多tests目录下的测试程序(当时编译没注意到这点走了不少弯路啊!!!)。通过./configure --help
查看配置帮助信息可以看到以下选项:
--enable-embedded-tests enable unit test code in the library and binaries
--enable-modular-tests enable modular regression tests (requires GLib)
--enable-tests enable/disable all tests, overriding
embedded-tests/modular-tests
所以在./configure
配置为交叉编译的时候就可以加上--enable-tests=no
选项来覆盖embedded-tests/modular-tests
这两个选项的配置,从而达到不引入tests测试程序的目的。
另外,编译第三方库时,它又会依赖其他库的时候,可以在./configure
命令时使用CFLAGS、LDFLAGS、XXX_CFLAGS和XXX_LIBS来指定编译好的依赖库。但如果依赖库与头文件路径较多时,手动设置这些变量就会比较的麻烦,这时就可以使用pkg-config程序来帮助我们完成这些填写,我们只需要把第三方库的xxx.pc所在路径添加到PKG_CONFIG_PATH这个环境变量中即可。接下来的编译步骤里这个两种方法是都会用上。更多关于pkg-config的用法可以参考文章《Linux:pkg-config的一些用法》。
sudo apt-get install autoconf autoconf-archive libtool pkg-config libglib2.0-dev
源码包下载:地址
3.1 编译expat
dbus需要依赖一个XML解析器–expat,所以需要先编译expat得到依赖库。
下载地址:https://github.com/libexpat/libexpat/releases
tar xjf expat-2.3.0.tar.bz2
cd expat-2.3.0/
./configure --prefix=$PWD/tmp --host=arm-linux-gnueabihf
make
make install
3.2 编译dbus
下载地址:
- https://www.freedesktop.org/wiki/Software/dbus/ (稳定版:1.12.x)
- https://dbus.freedesktop.org/releases/dbus/
tar xzf dbus-1.12.20.tar.gz
cd dbus-1.12.20/
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$PWD/../expat-2.3.0/tmp/lib/pkgconfig
./autogen.sh
./configure --prefix=$PWD/tmp --host=arm-linux-gnueabihf --enable-tests=no
make
make install
到了这一步,关于dbus的移植就基本完成了,下面的都是关于dbus-glib和它的一些依赖的移植,可以不用往下看,不影响后面文章的学习。但是另外有一点,这里的编译步骤可能会导致程序在目标板运行的时候出现主机路径的问题,所以这里的步骤只能是先了解大体步骤,dbus详细的移植与使用可以跳到【这篇文章】。
4.1 先编译glib
要想编译glib必须先编译它依赖的两个库(libffi和zlib):
4.1.1 libffi(版本需>=3.0)
下载地址:https://sourceware.org/libffi/
tar xzf libffi-3.3.tar.gz
cd libffi-3.3/
./configure --prefix=$PWD/tmp --host=arm-linux-gnueabihf
make
make install
4.1.2 zlib
下载地址:http://zlib.net/
tar xzf zlib-1.2.11.tar.gz
cd zlib-1.2.11/
./configure --prefix=$PWD/tmp
sed -i 's/=gcc/=arm-linux-gnueabihf-gcc/g' Makefile
make
make install
4.1.3 glib
下载地址:ftp://ftp.acc.umu.se/pub/gnome/sources/glib/
参考文档:
- INSTALL
- docs/reference/glib/html/glib-cross-compiling.html
tar xJf glib-2.34.1.tar.xz
cd glib-2.34.1/
vi cross_compile.cache
./configure --prefix=$PWD/tmp --host=arm-linux-gnueabihf \
LIBFFI_CFLAGS=-I$PWD/../libffi-3.3/tmp/include \
LIBFFI_LIBS="-lffi -L$PWD/../libffi-3.3/tmp/lib" \
ZLIB_CFLAGS=-I$PWD/../zlib-1.2.11/tmp/include \
ZLIB_LIBS="-lz -L$PWD/../zlib-1.2.11/tmp/lib" \
--cache-file=cross_compile.cache
make
make install
cross_compile.cache内容如下:
glib_cv_long_long_format=ll
glib_cv_stack_grows=no
glib_cv_working_bcopy=yes
glib_cv_sane_realloc=yes
glib_cv_have_strlcpy=no
glib_cv_have_qsort_r=no
glib_cv_va_val_copy=yes
glib_cv_rtldglobal_broken=yes
glib_cv_uscore=yes
ac_cv_func_posix_getpwuid_r=yes
ac_cv_func_nonposix_getpwuid_r=yes
ac_cv_func_posix_getgrgid_r=yes
glib_cv_use_pid_surrogate=yes
ac_cv_func_printf_unix98=no
ac_cv_func_vsnprintf_c99=no
4.2 编译dbus-glib
下载地址:https://dbus.freedesktop.org/releases/dbus-glib/
tar xzf dbus-glib-0.106.tar.gz
cd dbus-glib-0.106/
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$PWD/../glib-2.34.1/tmp/lib/pkgconfig
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$PWD/../dbus-1.12.20/tmp/lib/pkgconfig
./configure --prefix=$PWD/tmp --host=arm-linux-gnueabihf \
CFLAGS="-I$PWD/../expat-2.3.0/tmp/include" \
LDFLAGS="-lexpat -L$PWD/../expat-2.3.0/tmp/lib"
vi ./dbus/Makefile
SUBDIRS = . examples 改为 SUBDIRS = .
vi ./Makefile
SUBDIRS = dbus tools test doc 改为 SUBDIRS = dbus
make
make install
5.1 编译dbus出错
a. make编译出错:
/usr/include/glib-2.0/glib/gmacros.h:232:53: error: size of array ‘_GStaticAssertCompileTimeAssertion_0’ is negative
#define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __COUNTER__)[(expr) ? 1 : -1] G_GNUC_UNUSED
解决:在./configure ...
时配置交叉编译dbus时加上--enable-tests=no
选项,原因文章开头已经说了。
参考文章:dbus源码/README
5.2 编译glib出错
a. configure配置时出错:
checking for glib-genmarshal... no
configure: error: Could not find a glib-genmarshal in your PATH
解决:
sudo apt-get install libglib2.0-dev
参考文章:glib配置错误 - Gfim.CSDN
5.3 编译dbus-glib出错
a. make编译出错情况1:
Making all in examples
make[3]: Entering directory '/home/book/work/opensrc/dbus/dbus-glib-0.106/dbus/examples'
/bin/bash ../../libtool --mode=execute ../../dbus/dbus-binding-tool --prefix=some_object --mode=glib-server --output=example-service-glue.h ./example-service.xml
/home/book/work/opensrc/dbus/dbus-glib-0.106/dbus/examples/../../dbus/dbus-binding-tool: line 117: /home/book/work/opensrc/dbus/dbus-glib-0.106/dbus/.libs/lt-dbus-binding-tool: cannot execute binary file: Exec format error
/home/book/work/opensrc/dbus/dbus-glib-0.106/dbus/examples/../../dbus/dbus-binding-tool: line 117: /home/book/work/opensrc/dbus/dbus-glib-0.106/dbus/.libs/lt-dbus-binding-tool: Success
Makefile:788: recipe for target 'example-service-glue.h' failed
make[3]: *** [example-service-glue.h] Error 126
解决:
vi ./dbus/Makefile
SUBDIRS = . examples 改为 SUBDIRS = .
参考文章:How to run dbus and obex-data-server on ARM-xScale - stevenliyong.CSDN
b. make编译出错情况2:
Making all in tools
make[2]: Entering directory '/home/book/work/opensrc/dbus/dbus-glib-0.106/tools'
../dbus/dbus-binding-tool --mode=glib-client --prefix=dbus_bus --output=dbus-glib-bindings.h ../dbus-bus-introspect.xml
../dbus/dbus-binding-tool: line 117: /home/book/work/opensrc/dbus/dbus-glib-0.106/dbus/.libs/lt-dbus-binding-tool: cannot execute binary file: Exec format error
../dbus/dbus-binding-tool: line 117: /home/book/work/opensrc/dbus/dbus-glib-0.106/dbus/.libs/lt-dbus-binding-tool: Success
Makefile:598: recipe for target 'dbus-glib-bindings.h' failed
make[2]: *** [dbus-glib-bindings.h] Error 126
解决:
vi ./Makefile
SUBDIRS = dbus tools test doc 改为 SUBDIRS = dbus
参考文章:How to run dbus and obex-data-server on ARM-xScale - stevenliyong.CSDN
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/expat-2.3.0/tmp/lib/pkgconfig
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/libffi-3.3/tmp/lib/pkgconfig
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/zlib-1.2.11/tmp/lib/pkgconfig
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/glib-2.34.1/tmp/lib/pkgconfig
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/dbus-1.12.20/tmp/lib/pkgconfig
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/dbus-glib-0.106/tmp/lib/pkgconfig
arm-linux-gnueabihf-gcc test.c -o test `pkg-config --cflags --libs glib-2.0 dbus-1 dbus-glib-1 expat libffi gmodule-2.0 zlib gio-2.0 gthread-2.0`