由Qt程序创建Deb包

Author Avatar
Mutse Young 12月 27, 2012

本文主要讲解如何从 Qt 源码创建 Ubuntu deb 安装包。

Qt 程序

编写 Qt4

首先创建一个hello-1.0目录,并在此目录中编辑一个 Qt Hello World 程序,操作如下:

1
2
3
$ mkdir hello-1.0
$ cd hello-1.0
$ vim hello.cpp

hello.cpp 源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel;
label->setText("<center><h1><font color=blue>Hello World!</font></h1></center>");
label->setWindowTitle("QT");
label->resize(200, 50);
label->show();
return app.exec();
}

资源文件

在工程 hello-1.0 目录下新建一个 data 目录(根据实际情况,还可增加 man 和 doc 等目录),用来存放 desktop 和图片等资源文件。

1
$ mkdir data

进入 data 目录,并新建一个 hello.desktop 文件:

1
2
$ cd data
$ vim hello.desktop

hello.desktop 文件的内容如下:

[Desktop Entry]
Version=1.0
Name=Qt Hello World Demo
GenericName=Qt Hello demo
Comment=show a demo of Qt4
Icon=/usr/share/pixmaps/hello.png
Exec=hello
Terminal=false
Type=Application
Categories=Qt;Utility;
X-Ayatana-Desktop-Shortcuts = Hello

[Hello Shortcut Group]
Name = Hello
Exec = hello
TargetEnvironment = Unity

然后将图标文件拷贝至 data 目录下,终端切换至工程目录下:

1
$ cd ..

生成 pro

编辑完 hello.cpp 后,使用 qmake 命令生成 hello.pro:

1
$ qmake -project

在 hello.pro 文件的末尾增加以下信息:

# target 
target.files += hello
target.path = /usr/bin/

# icons
icons.files += data/hello.png
icons.path = /usr/share/pixmaps/

# desktop
desktop.files += data/hello.desktop
desktop.path = /usr/share/applications/

INSTALLS += target icons desktop

安装打包工具

在开始打包前,需要安装 deb 打包工具,可以通过 apt-get 命令安装:

1
$ sudo apt-get install build-essential devscripts ubuntu-dev-tools debhelper dh-make diff patch gnupg fakeroot lintian pbuilder

创建 Deb 包

创建 orig.tar.gz 包

在 hello-1.0 目录下运行 dh_make 命令,创建 orig.tar.gz 包:

1
$ dh_make --createorig --single -e yyhoo2.young@gmail.com -c gpl

回答 dh_make 的问题,然后按下 Enter 确认:

Maintainer name  : mutse
Email-Address    : yyhoo2.young@gmail.com 
Date             : Thu, 27 Dec 2012 22:53:06 +0800
Package Name     : hello
Version          : 1.0
License          : gpl3
Type of Package  : Single
Hit <enter> to confirm: 
Currently there is no top level Makefile. This may require additional tuning.
Done. Please edit the files in the debian/ subdirectory now. You should also
check that the hello Makefiles install into $DESTDIR and not in / .

删除 ex 和 EX 文件

1
2
$ cd debian/
$ ls

debian 目录下的文件如下:

changelog  emacsen-install.ex  hello.doc-base.EX  menu.ex      README.Debian
compat     emacsen-remove.ex   init.d.ex          postinst.ex  README.source
control    emacsen-startup.ex  manpage.1.ex       postrm.ex    rules
copyright  hello.cron.d.ex     manpage.sgml.ex    preinst.ex   source
docs       hello.default.ex    manpage.xml.ex     prerm.ex     watch.ex

删除 debian 目录下后缀为 ex 和 EX 的文件:

1
$ rm *.ex *.EX

创建 Deb 包

在工程 hello-1.0 目录下运行 debuild 命令:

1
2
$ cd ..
$ debuild

输出信息如下:

dpkg-buildpackage -rfakeroot -D -us -uc
dpkg-buildpackage: export CFLAGS from dpkg-buildflags (origin: vendor): -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security
dpkg-buildpackage: export CPPFLAGS from dpkg-buildflags (origin: vendor): -D_FORTIFY_SOURCE=2
dpkg-buildpackage: export CXXFLAGS from dpkg-buildflags (origin: vendor): -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security
dpkg-buildpackage: export FFLAGS from dpkg-buildflags (origin: vendor): -g -O2
dpkg-buildpackage: export LDFLAGS from dpkg-buildflags (origin: vendor): -Wl,-Bsymbolic-functions -Wl,-z,relro
dpkg-buildpackage: 源码包 hello
dpkg-buildpackage: 源码版本 1.0-1
dpkg-buildpackage: 源码修改者 mutse <yyhoo2.young@gmail.com>
dpkg-source --before-build hello-1.0
dpkg-buildpackage: 主机架构 i386
 fakeroot debian/rules clean
dh clean 
dh_testdir
dh_auto_clean
dh_clean
 dpkg-source -b hello-1.0
dpkg-source: info: using source format `3.0 (quilt)'
dpkg-source: info: building hello using existing ./hello_1.0.orig.tar.gz
dpkg-source: info: building hello in hello_1.0-1.debian.tar.gz
dpkg-source: info: building hello in hello_1.0-1.dsc
 debian/rules build
dh build 
dh_testdir
dh_auto_configure
dh_auto_build
make[1]: 正在进入目录 '/home/mutse/workspace/qts/hello/hello-1.0'
g++ -c -pipe -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -D_FORTIFY_SOURCE=2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -o hello.o hello.cpp
g++ -Wl,-Bsymbolic-functions -Wl,-z,relro -o hello hello.o    -L/usr/lib/i386-linux-gnu -lQtGui -lQtCore -lpthread 
make[1]:正在离开目录 `/home/mutse/workspace/qts/hello/hello-1.0'
dh_auto_test
 fakeroot debian/rules binary
dh binary 
dh_testroot
dh_prep
dh_installdirs
dh_auto_install
make[1]: 正在进入目录 `/home/mutse/workspace/qts/hello/hello-1.0'
install -m 755 -p /home/mutse/workspace/qts/hello/hello-1.0/hello /home/mutse/workspace/qts/hello/hello-1.0/debian/hello/usr/bin/
install -m 644 -p /home/mutse/workspace/qts/hello/hello-1.0/data/hello.png /home/mutse/workspace/qts/hello/hello-1.0/debian/hello/usr/share/pixmaps/
install -m 755 -p /home/mutse/workspace/qts/hello/hello-1.0/data/hello.desktop /home/mutse/workspace/qts/hello/hello-1.0/debian/hello/usr/share/applications/
: /home/mutse/workspace/qts/hello/hello-1.0/debian/hello/usr/share/applications/hello.desktop
make[1]:正在离开目录 `/home/mutse/workspace/qts/hello/hello-1.0'
dh_install
dh_installdocs
dh_installchangelogs
dh_installexamples
dh_installman
dh_installcatalogs
dh_installcron
dh_installdebconf
dh_installemacsen
dh_installifupdown
dh_installinfo
dh_pysupport
dh_pysupport: This program is deprecated, you should use dh_python2 instead. Migration guide: http://deb.li/dhs2p
dh_installinit
dh_installmenu
dh_installmime
dh_installmodules
dh_installlogcheck
dh_installlogrotate
dh_installpam
dh_installppp
dh_installudev
dh_installwm
dh_installxfonts
dh_installgsettings
dh_bugfiles
dh_ucf
dh_lintian
dh_gconf
dh_icons
dh_perl
dh_usrlocal
dh_link
dh_compress
dh_fixperms
dh_strip
dh_makeshlibs
dh_shlibdeps
dh_installdeb
dh_gencontrol
dh_md5sums
dh_builddeb
dpkg-deb:正在新建软件包 hello,包文件为 ../hello_1.0-1_i386.deb。
dpkg-genchanges  >../hello_1.0-1_i386.changes
dpkg-genchanges: 上传数据中包含完整的原始代码
dpkg-source --after-build hello-1.0
dpkg-buildpackage: 完整上载(包含原始的代码)
Now running lintian...
W: hello source: superfluous-clutter-in-homepage <insert the upstream URL, if relevant>
W: hello source: bad-homepage <insert the upstream URL, if relevant>
W: hello source: out-of-date-standards-version 3.9.2 (current is 3.9.3)
W: hello: new-package-should-close-itp-bug
W: hello: wrong-bug-number-in-closes l3:#nnnn
E: hello: helper-templates-in-copyright
W: hello: copyright-has-url-from-dh_make-boilerplate
E: hello: copyright-contains-dh_make-todo-boilerplate
W: hello: readme-debian-contains-debmake-template
E: hello: description-is-dh_make-template
E: hello: section-is-dh_make-template
W: hello: superfluous-clutter-in-homepage <insert the upstream URL, if relevant>
W: hello: bad-homepage <insert the upstream URL, if relevant>
W: hello: binary-without-manpage usr/bin/hello
Finished running lintian.
Now signing changes and any dsc files...
signfile hello_1.0-1.dsc mutse <yyhoo2.young@gmail.com>
gpg: failed to create temporary file `/home/mutse/.gnupg/.#lk0x85158d0.mutse.6958': 权限不够
gpg: 密钥块资源‘/home/mutse/.gnupg/secring.gpg’:常规错误
gpg: failed to create temporary file `/home/mutse/.gnupg/.#lk0x85175c0.mutse.6958': 权限不够
gpg: 密钥块资源‘/home/mutse/.gnupg/pubring.gpg’:常规错误
gpg: “mutse <yyhoo2.young@gmail.com>”已跳过:私钥不可用
gpg: /tmp/debsign.hOAkkXC1/hello_1.0-1.dsc: clearsign failed: 私钥不可用
debsign: gpg error occurred!  Aborting....
debuild: fatal error at line 1271:
running debsign failed

上面的 gpg 密钥验证失败可以跳过,切换到 hello-1.0 上级目录,发现生成了一个 deb 包。

验证安装包

1
$ sudo dpkg -i hello_1.0-1_i386.deb

打开 Dash,在搜索栏中输入 hello,即可见 hello 的图标。

参考资料