打开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查看配置帮助信息可以看到以下选项:

  1. --enable-embedded-tests enable unit test code in the library and binaries
  2. --enable-modular-tests enable modular regression tests (requires GLib)
  3. --enable-tests enable/disable all tests, overriding
  4. 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的一些用法》

  1. 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

  1. tar xjf expat-2.3.0.tar.bz2
  2. cd expat-2.3.0/
  3. ./configure --prefix=$PWD/tmp --host=arm-linux-gnueabihf
  4. make
  5. make install

3.2 编译dbus

下载地址:

  1. tar xzf dbus-1.12.20.tar.gz
  2. cd dbus-1.12.20/
  3. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$PWD/../expat-2.3.0/tmp/lib/pkgconfig
  4. ./autogen.sh
  5. ./configure --prefix=$PWD/tmp --host=arm-linux-gnueabihf --enable-tests=no
  6. make
  7. make install

到了这一步,关于dbus的移植就基本完成了,下面的都是关于dbus-glib和它的一些依赖的移植,可以不用往下看,不影响后面文章的学习。但是另外有一点,这里的编译步骤可能会导致程序在目标板运行的时候出现主机路径的问题,所以这里的步骤只能是先了解大体步骤,dbus详细的移植与使用可以跳到【这篇文章】

4.1 先编译glib

要想编译glib必须先编译它依赖的两个库(libffi和zlib):

4.1.1 libffi(版本需>=3.0)

下载地址:https://sourceware.org/libffi/

  1. tar xzf libffi-3.3.tar.gz
  2. cd libffi-3.3/
  3. ./configure --prefix=$PWD/tmp --host=arm-linux-gnueabihf
  4. make
  5. make install

4.1.2 zlib

下载地址:http://zlib.net/

  1. tar xzf zlib-1.2.11.tar.gz
  2. cd zlib-1.2.11/
  3. ./configure --prefix=$PWD/tmp
  4. sed -i 's/=gcc/=arm-linux-gnueabihf-gcc/g' Makefile
  5. make
  6. make install

4.1.3 glib

下载地址:ftp://ftp.acc.umu.se/pub/gnome/sources/glib/

参考文档:

  • INSTALL
  • docs/reference/glib/html/glib-cross-compiling.html
  1. tar xJf glib-2.34.1.tar.xz
  2. cd glib-2.34.1/
  3. vi cross_compile.cache
  4. ./configure --prefix=$PWD/tmp --host=arm-linux-gnueabihf \
  5. LIBFFI_CFLAGS=-I$PWD/../libffi-3.3/tmp/include \
  6. LIBFFI_LIBS="-lffi -L$PWD/../libffi-3.3/tmp/lib" \
  7. ZLIB_CFLAGS=-I$PWD/../zlib-1.2.11/tmp/include \
  8. ZLIB_LIBS="-lz -L$PWD/../zlib-1.2.11/tmp/lib" \
  9. --cache-file=cross_compile.cache
  10. make
  11. make install

cross_compile.cache内容如下:

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

4.2 编译dbus-glib

下载地址:https://dbus.freedesktop.org/releases/dbus-glib/

  1. tar xzf dbus-glib-0.106.tar.gz
  2. cd dbus-glib-0.106/
  3. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$PWD/../glib-2.34.1/tmp/lib/pkgconfig
  4. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$PWD/../dbus-1.12.20/tmp/lib/pkgconfig
  5. ./configure --prefix=$PWD/tmp --host=arm-linux-gnueabihf \
  6. CFLAGS="-I$PWD/../expat-2.3.0/tmp/include" \
  7. LDFLAGS="-lexpat -L$PWD/../expat-2.3.0/tmp/lib"
  8. vi ./dbus/Makefile
  9. SUBDIRS = . examples 改为 SUBDIRS = .
  10. vi ./Makefile
  11. SUBDIRS = dbus tools test doc 改为 SUBDIRS = dbus
  12. make
  13. make install

5.1 编译dbus出错

a. make编译出错:

  1. /usr/include/glib-2.0/glib/gmacros.h:232:53: error: size of array _GStaticAssertCompileTimeAssertion_0 is negative
  2. #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配置时出错:

  1. checking for glib-genmarshal... no
  2. configure: error: Could not find a glib-genmarshal in your PATH

解决:

  1. sudo apt-get install libglib2.0-dev

参考文章:glib配置错误 - Gfim.CSDN

5.3 编译dbus-glib出错

a. make编译出错情况1:

  1. Making all in examples
  2. make[3]: Entering directory '/home/book/work/opensrc/dbus/dbus-glib-0.106/dbus/examples'
  3. /bin/bash ../../libtool --mode=execute ../../dbus/dbus-binding-tool --prefix=some_object --mode=glib-server --output=example-service-glue.h ./example-service.xml
  4. /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
  5. /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
  6. Makefile:788: recipe for target 'example-service-glue.h' failed
  7. make[3]: *** [example-service-glue.h] Error 126

解决:

  1. vi ./dbus/Makefile
  2. SUBDIRS = . examples 改为 SUBDIRS = .

参考文章:How to run dbus and obex-data-server on ARM-xScale - stevenliyong.CSDN

b. make编译出错情况2:

  1. Making all in tools
  2. make[2]: Entering directory '/home/book/work/opensrc/dbus/dbus-glib-0.106/tools'
  3. ../dbus/dbus-binding-tool --mode=glib-client --prefix=dbus_bus --output=dbus-glib-bindings.h ../dbus-bus-introspect.xml
  4. ../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
  5. ../dbus/dbus-binding-tool: line 117: /home/book/work/opensrc/dbus/dbus-glib-0.106/dbus/.libs/lt-dbus-binding-tool: Success
  6. Makefile:598: recipe for target 'dbus-glib-bindings.h' failed
  7. make[2]: *** [dbus-glib-bindings.h] Error 126

解决:

  1. vi ./Makefile
  2. SUBDIRS = dbus tools test doc 改为 SUBDIRS = dbus

参考文章:How to run dbus and obex-data-server on ARM-xScale - stevenliyong.CSDN

  1. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/expat-2.3.0/tmp/lib/pkgconfig
  2. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/libffi-3.3/tmp/lib/pkgconfig
  3. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/zlib-1.2.11/tmp/lib/pkgconfig
  4. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/glib-2.34.1/tmp/lib/pkgconfig
  5. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/dbus-1.12.20/tmp/lib/pkgconfig
  6. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:YOUR_DIR/dbus-glib-0.106/tmp/lib/pkgconfig
  7. 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`