Linux进程间通信:dbus的使用(1)—— 交叉编译dbus/glib/dbus-glib

1、编译说明

打开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的一些用法》


2、环境准备

sudo apt-get install autoconf autoconf-archive libtool pkg-config libglib2.0-dev

3、交叉编译dbus

源码包下载:地址

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

下载地址:

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、交叉编译dbus-glib

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 		# 不支持在这里指定CC
sed -i 's/=gcc/=arm-linux-gnueabihf-gcc/g' Makefile # 修改gcc为交叉编译的gcc,加“=”防止重复替换
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 	# 新建文件的内容在下方给出
# 注意:这里不需要执行./autogen.sh
# 不知道是环境没配好,还是官方本身就不维护,因为INSTALL文档的步骤里也不用
# 还有一点就是执行./autogen.sh不就是为了生成configure嘛,已经提供了也不需要再次生成
./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 = . #examples
vi ./Makefile
	SUBDIRS = dbus tools test doc 改为 SUBDIRS = dbus #tools test doc
make
make install

5、可能会遇到的问题

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 = . #examples

参考文章: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 #tools test doc

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


6、编译自己编写的dbus-glib程序

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`

参考文章

  • 6
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux系统提供了各种系统调用API用于进程之间的通信:    无名管道PIPE    命名管道FIFO    消息队列    共享内存    信号量    文件锁    信号signal....其中还包括system V和POSIX 两种接口标准,除此之外,Linux系统自身还扩展了自己的一套API接口用于进程间通信,比如signalfd、timerfd、eventfd等。本视频教程为《Linux系统编程》第05期,本期课程将会带领大家学习Linux下将近15种进程间通信IPC工具的使用,了解它们的通信机制、编程实例、使用场景、内核中的实现以及各自的优缺点。本课程会提供PDF版本的PPT课件和代码,学员购买课程后可到课程主页自行下载嵌入式自学路线指导图:------------------------------------------------------------------------------------------------------                   《嵌入式工程师自我修养》嵌入式自学系列教程                                          作者:王利涛------------------------------------------------------------------------------------------------------一线嵌入式工程师精心打造,嵌入式学习路线六步走: 第 1 步:Linux三剑客零基础玩转Linux+UbuntuGit零基础实战:Linux开发技能标配vim从入门到精通基础篇:零基础学习vim基本命令vim从入门到精通定制篇:使用插件打造嵌入式开发IDEmakefile工程实践基础篇:从零开始一步一步写项目的Makefilemakefile工程实践第2季:使用Autotools自动生成Makefile软件调试基础理论printf打印技巧Linux内核日志与打印使用QEMU搭建u-boot+Linux+NFS嵌入式开发环境第 2 步:C语言嵌入式Linux高级编程第1期:C语言进阶学习路线指南第2期:计算机架构与ARM汇编程序设计第3期:程序的编译、链接和运行原理第4期:堆栈内存管理第6期:数据存储与指针第7期:嵌入式数据结构与Linux内核的OOP思想第8期:C语言的模块化编程第9期:CPU和操作系统入门      搞内核驱动开发、光会C语言是不行的!      你还需要学习的有很多,包括:计算机体系架构、ARM汇编、程序的编译链接运行原理、CPU和操作系统原理、堆栈内存管理、指针、linux内核中的面向对象思想、嵌入式系统架构、C语言的模块化编程.....第 3 步:Linux系统编程第00期:Linux系统编程入门第01期:揭开文件系统的神秘面纱第02期:文件I/O编程实战第03期:I/O缓存与内存映射第04期:打通进程与终端的任督二脉第05期:进程间通信-------------------we are here!‍    第 4 步:Linux内核编程‍    练乾坤大挪移,会不会九阳神功,是一道坎。搞驱动内核开发,懂不懂内核也是一道坎。第 5 步:嵌入式驱动开发    芯片原理、datasheet、硬件电路、调试手段、总线协议、内核机制、框架流程....第 6 步:项目实战    嵌入式、嵌入式人工智能、物联网、智能家居...

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

R-QWERT

你的鼓励是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值