打开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, overridingembedded-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.bz2cd expat-2.3.0/./configure --prefix=$PWD/tmp --host=arm-linux-gnueabihfmakemake 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.gzcd 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=nomakemake 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.gzcd libffi-3.3/./configure --prefix=$PWD/tmp --host=arm-linux-gnueabihfmakemake install
4.1.2 zlib
下载地址:http://zlib.net/
tar xzf zlib-1.2.11.tar.gzcd zlib-1.2.11/./configure --prefix=$PWD/tmpsed -i 's/=gcc/=arm-linux-gnueabihf-gcc/g' Makefilemakemake 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.xzcd 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.cachemakemake install
cross_compile.cache内容如下:
glib_cv_long_long_format=llglib_cv_stack_grows=noglib_cv_working_bcopy=yesglib_cv_sane_realloc=yesglib_cv_have_strlcpy=noglib_cv_have_qsort_r=noglib_cv_va_val_copy=yesglib_cv_rtldglobal_broken=yesglib_cv_uscore=yesac_cv_func_posix_getpwuid_r=yesac_cv_func_nonposix_getpwuid_r=yesac_cv_func_posix_getgrgid_r=yesglib_cv_use_pid_surrogate=yesac_cv_func_printf_unix98=noac_cv_func_vsnprintf_c99=no
4.2 编译dbus-glib
下载地址:https://dbus.freedesktop.org/releases/dbus-glib/
tar xzf dbus-glib-0.106.tar.gzcd dbus-glib-0.106/export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$PWD/../glib-2.34.1/tmp/lib/pkgconfigexport 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/MakefileSUBDIRS = . examples 改为 SUBDIRS = .vi ./MakefileSUBDIRS = dbus tools test doc 改为 SUBDIRS = dbusmakemake 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... noconfigure: 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 examplesmake[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: SuccessMakefile:788: recipe for target 'example-service-glue.h' failedmake[3]: *** [example-service-glue.h] Error 126
解决:
vi ./dbus/MakefileSUBDIRS = . examples 改为 SUBDIRS = .
参考文章:How to run dbus and obex-data-server on ARM-xScale - stevenliyong.CSDN
b. make编译出错情况2:
Making all in toolsmake[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: SuccessMakefile:598: recipe for target 'dbus-glib-bindings.h' failedmake[2]: *** [dbus-glib-bindings.h] Error 126
解决:
vi ./MakefileSUBDIRS = 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/pkgconfigexport PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/libffi-3.3/tmp/lib/pkgconfigexport PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/zlib-1.2.11/tmp/lib/pkgconfigexport PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/glib-2.34.1/tmp/lib/pkgconfigexport PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/dbus-1.12.20/tmp/lib/pkgconfigexport PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/dbus-glib-0.106/tmp/lib/pkgconfigarm-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`
