How to add a dependency in a Jolla application project

When creating a Jolla application you may at some point need to add a dependency. Doing so is not trivial and there does not seem to be much documentation on the topic. Consequently I decided to write a short guide on how to add a dependency in a Jolla app project. In my case I will be showing how to add zlib, a popular library for compression.

You need to modify two files the ".yaml" file and the ".pro" file. The first one is used to specify the runtime and build dependencies and the second one is used to specify the compilation method.

The first step is modifying the ".yaml". You probably have something that looks like this (using the file from my project):

Name: Quotes Summary: Quotes display quotes from philosophers Version: 0.1 Release: 1 # The contents of the Group field should be one of the groups listed here: # http://gitorious.org/meego-developer-tools/spectacle/blobs/master/data/GROUPS Group: Qt/Qt URL: http://example.org/ License: LICENSE # This must be generated before uploading a package to a remote build service. # Usually this line does not need to be modified. Sources: - '%{name}-%{version}.tar.bz2' Description: | Short description of my SailfishOS Application Configure: none # The qtc5 builder inserts macros to allow QtCreator to have fine # control over qmake/make execution Builder: qtc5

# This section specifies build dependencies that are resolved using pkgconfig.
# This is the preferred way of specifying build dependencies for your package.
PkgConfigBR:
- sailfishapp >= 1.0.2
- Qt5Core
- Qt5Qml
- Qt5Quick

# Build dependencies without a pkgconfig setup can be listed here
# PkgBR:
# - package-needed-to-build

# Runtime dependencies which are not automatically detected
Requires:
- sailfishsilica-qt5 >= 0.10.9

# All installed files
Files:
- '%{_bindir}'
- '%{_datadir}/%{name}'
- '%{_datadir}/applications/%{name}.desktop'
- '%{_datadir}/icons/hicolor/86x86/apps/%{name}.png'

# For more information about yaml and what's supported in Sailfish OS
# build system, please see https://wiki.merproject.org/wiki/Spectacle

There are two sections that are important: PkgConfigBR and Requires. PkgConfigBR specifies the build dependencies and Requires specifies the runtime dependencies. What you specify is the name of the rpm where your dependency is. In my case the package I rely on is zlib. To do so you add zlib to both sections. Giving you code that will look like this:

# This section specifies build dependencies that are resolved using pkgconfig. # This is the preferred way of specifying build dependencies for your package. PkgConfigBR: - sailfishapp >= 1.0.2 - Qt5Core - Qt5Qml - Qt5Quick - zlib

# Build dependencies without a pkgconfig setup can be listed here
# PkgBR:
# - package-needed-to-build

# Runtime dependencies which are not automatically detected
Requires:
- sailfishsilica-qt5 >= 0.10.9
- zlib

You don't need to specify the development packages (zlib-devel in my case) because this is handled automatically by spectacle and pkg-config (tools used in the build process). Unfortunately modifying the ".yaml" file is not enough. If you try to build now you will get an error:

Missing linker
flag

This error informs that linking did not succeed and this is because the build process did not use the correct linker flags. To solve this one needs to modify the ".pro" file that defines the QT Creator project. This file normally look something like this:

# NOTICE: # # Application name defined in TARGET has a corresponding QML filename. # If name defined in TARGET is changed, the following needs to be done # to match new name: # - corresponding QML filename must be changed # - desktop icon filename must be changed # - desktop filename must be changed # - icon definition filename in desktop file must be changed # - translation filenames have to be changed

# The name of your application
TARGET = Quotes

CONFIG += sailfishapp

SOURCES += src/Quotes.cpp \
src/quotecontroller.cpp \
src/quote.cpp \
src/quotedb.cpp \
src/quotemodel.cpp \
src/idregistry.cpp

OTHER_FILES += qml/Quotes.qml \
qml/cover/CoverPage.qml \
qml/content/images/*.png \
rpm/Quotes.changes.in \
rpm/Quotes.spec \
rpm/Quotes.yaml \
translations/*.ts \
qml/content/quotes_en.json \
Quotes.desktop \
Quotes.png \
qml/pages/AboutPage.qml \
qml/pages/QuotePage.qml \
qml/pages/SearchPage.qml

# to disable building translations every time, comment out the
# following CONFIG line
CONFIG += sailfishapp_i18n
TRANSLATIONS += translations/Quotes-de.ts

HEADERS += \
src/quotecontroller.h \
src/quote.h \
src/quotedb.h \
src/quotemodel.h \
src/idregistry.h

To get it to add the wanted library you need to add the line:

PKGCONFIG += zlib

This will tell QT Creator to pull the linker flags using pkg-config to match the requirements.

For more information on the build process please visit this excellent although a bit outdated guide at flyingsheep.

[feather_share show="google_plus, twitter, Facebook, reddit" hide="pinterest, linkedin, tumblr, mail"]

social