从零开始在Linux环境下构建Qt项目DEB包

使用dh_make和dpkg-buildpackage构建deb包。
下述Qt项目,可以访问https://github.com/ShawZG/XColorPicker查看。

1. 调整Qt项目

下面是一个可以构建deb包的完整目录结构,其中build和debian目录是后续操作自动生成的。建议调整下,把代码放在src目录中,项目根目录下保留pro文件。data目录中保存的是deb包的desktop文件和desktop图标。build是项目编译后,可执行文件和编译产生的中间文件存放的地方,具体可看后续的pro文件。debian目录保存构建deb包所需文件,由dh_make命令产生,后续会介绍。

$ tree -L 1
.
├── build
├── data
├── debian
├── src
├── XColorPicker.pro
└── XColorPicker.pro.user

4 directories, 2 files

项目的pro文件如下,省略非关键的部分。

TARGET = xcolorpicker

SOURCES += ...
HEADERS += ...

CONFIG(debug, debug|release) {
    BUILD_MODE = debug
} else {
    BUILD_MODE = release
}
# 设置编译生成文件路径
DESTDIR = $${PWD}/build/$${BUILD_MODE}/bin
MOC_DIR = $${PWD}/build/$${BUILD_MODE}/moc
RCC_DIR = $${PWD}/build/$${BUILD_MODE}/rcc
UI_DIR = $${PWD}/build/$${BUILD_MODE}/ui
unix:OBJECTS_DIR = $${PWD}/build/$${BUILD_MODE}/obj/unix
win32:OBJECTS_DIR = $${PWD}/build/$${BUILD_MODE}/obj/win32
macx:OBJECTS_DIR = $${PWD}/build/$${BUILD_MODE}/obj/mac
2. 编辑desktop文件

desktop文件以PackageName.desktop形式保存于data目录下,基本的参数如下,根据自己项目修改。

[Desktop Entry]
# 这一行是注释
Categories=Application;Utility;
Comment=a small desktop application to pick color by hex value
Exec=/usr/bin/xcolorpicker
GenericName=xcolorpicker
Icon=/usr/share/pixmaps/xcolorpicker.svg
Name=xcolorpicker
Terminal=false
Type=Application

desktop文件中,各项属性说明如下:

KeyDescriptionValue TypeREQType
TypeThis specification defines 3 types of desktop entries: Application (type 1), Link (type 2) and Directory (type 3). To allow the addition of new types in the future, implementations should ignore desktop entries with an unknown type.stringYES
VersionVersion of the Desktop Entry Specification that the desktop entry conforms with. Entries that confirm with this version of the specification should use 1.1. Note that the version field is not required to be present.stringNO1-3
NameSpecific name of the application, for example “Mozilla”.localestringYES1-3
GenericNameGeneric name of the application, for example “Web Browser”.localestringNO1-3
NoDisplayNoDisplay means “this application exists, but don’t display it in the menus”. This can be useful to e.g. associate this application with MIME types, so that it gets launched from a file manager (or other apps), without having a menu entry for it (there are tons of good reasons for this, including e.g. the netscape -remote, or kfmclient openURL kind of stuff).booleanNO1-3
CommentTooltip for the entry, for example “View sites on the Internet”. The value should not be redundant with the values of Name and GenericName.localestringNO1-3
IconIcon to display in file manager, menus, etc. If the name is an absolute path, the given file will be used. If the name is not an absolute path, the algorithm described in the Icon Theme Specification will be used to locate the icon.iconstringNO1-3
HiddenHidden should have been called Deleted. It means the user deleted (at his level) something that was present (at an upper level, e.g. in the system dirs). It’s strictly equivalent to the .desktop file not existing at all, as far as that user is concerned. This can also be used to “uninstall” existing files (e.g. due to a renaming) - by letting make install install a file with Hidden=true in it.booleanNO1-3
OnlyShowIn, NotShowInA list of strings identifying the desktop environments that should display/not display a given desktop entry. By default, a desktop file should be shown, unless an OnlyShowIn key is present, in which case, the default is for the file not to be shown. If $XDG_CURRENT_DESKTOP is set then it contains a colon-separated list of strings. In order, each string is considered. If a matching entry is found in OnlyShowIn then the desktop file is shown. If an entry is found in NotShowIn then the desktop file is not shown. If none of the strings match then the default action is taken (as above). $XDG_CURRENT_DESKTOP should have been set by the login manager, according to the value of the DesktopNames found in the session file. The entry in the session file has multiple values separated in the usual way: with a semicolon. The same desktop name may not appear in both OnlyShowIn and NotShowIn of a group.string(s)NO1-3
DBusActivatableA boolean value specifying if D-Bus activation is supported for this application. If this key is missing, the default value is false. If the value is true then implementations should ignore the Exec key and send a D-Bus message to launch the application. See D-Bus Activation for more information on how this works. Applications should still include Exec= lines in their desktop files for compatibility with implementations that do not understand the DBusActivatable key.booleanNO
TryExecPath to an executable file on disk used to determine if the program is actually installed. If the path is not an absolute path, the file is looked up in the $PATH environment variable. If the file is not present or if it is not executable, the entry may be ignored (not be used in menus, for example).stringNO1
ExecProgram to execute, possibly with arguments. See the Exec key for details on how this key works. The Exec key is required if DBusActivatable is not set to true. Even if DBusActivatable is true, Exec should be specified for compatibility with implementations that do not understand DBusActivatable.stringNO1
PathIf entry is of type Application, the working directory to run the program in.stringNO1
TerminalWhether the program runs in a terminal window.booleanNO1
ActionsIdentifiers for application actions. This can be used to tell the application to make a specific action, different from the default behavior. The Application actions section describes how actions work.string(s)NO1
MimeTypeThe MIME type(s) supported by this application.string(s)NO1
CategoriesCategories in which the entry should be shown in a menu (for possible values see the Desktop Menu Specification).string(s)NO1
ImplementsA list of interfaces that this application implements. By default, a desktop file implements no interfaces. See Interfaces for more information on how this works.string(s)NO
KeywordsA list of strings which may be used in addition to other metadata to describe this entry. This can be useful e.g. to facilitate searching through entries. The values are not meant for display, and should not be redundant with the values of Name or GenericName.localestring(s)NO1
StartupNotifyIf true, it is KNOWN that the application will send a “remove” message when started with the DESKTOP_STARTUP_ID environment variable set. If false, it is KNOWN that the application does not work with startup notification at all (does not shown any window, breaks even when using StartupWMClass, etc.). If absent, a reasonable handling is up to implementations (assuming false, using StartupWMClass, etc.). (See the Startup Notification Protocol Specification for more details).booleanNO1
StartupWMClassIf specified, it is known that the application will map at least one window with the given string as its WM class or WM name hint (see the Startup Notification Protocol Specification for more details).stringNO1
URLIf entry is Link type, the URL to access.stringYES2
PrefersNonDefaultGPUIf true, the application prefers to be run on a more powerful discrete GPU if available, which we describe as “a GPU other than the default one” in this spec to avoid the need to define what a discrete GPU is and in which cases it might be considered more powerful than the default GPU. This key is only a hint and support might not be present depending on the implementation.booleanNO1

关于Categories的详细说明,我也整理了一下,可以直接查看Categories详细说明

补充说明:

  1. TYPE表示此项目针对的类型,Application (type 1), Link (type 2) and Directory (type 3).
  2. REG表示此项目在对应TYPE下是否为必须的。
3. 安装设置dh_make
sudo apt-get install dh-make

首先设置两个环境变量,$DEBEMAIL 和 $DEBFULLNAME,这样大多数 Debian 维护工具就能够正确识别你用于维护软件包的姓名和电子邮件地址。

$ cat >>~/.bashrc <<EOF
DEBEMAIL="blueshaw@163.com"
DEBFULLNAME="ShawZG"
export DEBEMAIL DEBFULLNAME
EOF
$ . ~/.bashrc
4. 生成debian目录
$ dh_make -e blueshaw@163.com -p xcolorpicker_1.2.0 -c mit --single --native
Maintainer Name     : unknown
Email-Address       : blueshaw@163.com
Date                : Mon, 08 Jun 2020 23:01:50 +0800
Package Name        : xcolorpicker
Version             : 1.2.0
License             : mit
Package Type        : single
Are the details correct? [Y/n/q]
Done. Please edit the files in the debian/ subdirectory now.

dh_make命令简单介绍
dh_make根据Debian的规则,在项目根目录下生成debian文件夹,所有执行dh_make的文件将保存在这个目录下。

dh_make [-nlsmiadhr] [-c license] [-e address] [-f file] [-t directory] [-o directory] [-p name] [–copyright license] [–email address] [–native] [–file file] [–library] [–single] [–indep] [–multi] [–kmod] [–kpatch] [–addmissing] [–templates directory] [–defaultless] [–overlay directory] [–packagename name] [–rulesformat format] [–help] [–version]

参数说明
-c, --copyright license产生一个copyright文件,license参数可选 gpl, gpl2, gpl3, lgpl, lgpl2,lgpl3, artistic, apache, bsd or x11. 参数大小写不敏感,即GPL和gpl效果一样
-e, --email address指定debian/control中维护者的邮箱
-n, --native创建本地Deb包,不需要项目上一级目录中存在源码文件_.orig.tar.gz,也不会生成.orig文。生成的deb包版本号将没有附加Debian版本号(例如,4.2.1.1,而不是4.2.1.1-1)。
-f, --file file如果不使用-n, --native,可以指定_.orig.tar.gz,不会拷贝当前项目结构到program.orig。
-l, --library项目将打包生成一个Library文件
-s, --single项目将打包成一个deb文件
-i, --indep项目将打包成一个与架构无关的deb文件
-m, --multi项目将打包成多个deb文件
-a, --addmissing为一个deb项目源码目录,添加缺失的example和control文件
-p, --packagename name指定生成的deb包名,可以使用name_version格式,指定包名和版本号
-r, --rulesformat format指定debian/rules文件格式,默认值是 Debhelper v7 minimal rules (dh7), the old rules format (old) or CDBS format (cdbs).

在debian目录下,我们需要手动生成一个packagename.install文件

$ touch xcolorpicker.install 

将多余的文件删除,保留必须的几个文件,最终debian目录下的文件如下:

$ tree
.
├── changelog
├── compat
├── control
├── copyright
├── rules
├── source
│   └── format
└── xcolorpicker.install

1 directory, 7 files
5. 编辑debian目录文件

control文件需要根据自己项目做适当修改。

Source: xcolorpicker
Section: free
Priority: optional
Maintainer: ZhiGuo Xiao <blueshaw@163.com>
Build-Depends: debhelper (>= 11)
Standards-Version: 4.1.3
Homepage: https://github.com/ShawZG/XColorPicker
Vcs-Browser: https://github.com/ShawZG/XColorPicker
Vcs-Git: https://github.com/ShawZG/XColorPicker.git

Package: xcolorpicker
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}
Description: a small desktop applicaton to pick color by hex value.

contol文件中各个条目说明可以参考debian目录中的必须内容,已经被翻译为中文了,且说明很详细。

xcolorpick.install文件内容如下,build和data是项目目录下对应的文件夹。

build/release/bin/xcolorpicker usr/bin
data/xcolorpicker.desktop usr/share/applications
data/xcolorpicker.svg usr/share/pixmaps

将对应的可执行程序xcolorpicker拷贝到/usr/bin/目录下。
将对应的desktop文件xcolorpicker.desktop拷贝到/usr/share/applications/目录下。
将对应的图标文件xcolorpicker.svg拷贝到/usr/share/pixmaps/目录下。

其他debian目录下的文件暂时可以不做修改。

6. 构建deb包

在项目根目录下执行如下命令,构建deb包。如果成功后,会在当前目录的上一级目录生成对应的deb包。

$ dpkg-buildpackage -us -uc

执行上面的命令后,可能会遇到如下的错误

$ dpkg-buildpackage -us -uc
dpkg-buildpackage: error: fakeroot not found, either install the fakeroot
package, specify a command with the -r option, or run this as root

安装fakeroot包即可。

$ sudo apt-get install fakeroot
7. 参考资料
  1. dh_make手册 http://manpages.ubuntu.com/manpages/precise/man8/dh_make.8.html)

  2. 第4章 debian目录中的必须内容 https://www.debian.org/doc/manuals/maint-guide/dreq.zh-cn.html

  3. 第5章 debian目录下的其他文件 https://www.debian.org/doc/manuals/maint-guide/dother.zh-cn.html

  4. 第6章 构建软件包 https://www.debian.org/doc/manuals/maint-guide/build.zh-cn.html

  5. How to make Debian Packages for Qt C++ based Applications

  6. Desktop文件属性说明 https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值