Replacing RPATH when Deploying Qt Shared Libraries - qt

Setup
I have a local installation of Qt located in my home directory: /home/user/Qt/... (from now on, devdir).
The Qt application that I'm trying to package installs the relevant Qt shared libraries to /usr/lib/myapplication (from now on, installdir).
My packaging process is currently set up like this:
qmake > dh_make -s --createorig > debuild
Problem
I am trying to set RPATH in myapplication.pro to only link to libraries in installdir, but it is currently linking to both installdir and devdir.
I think it has to do with qmake creating dependencies to the installation libraries automatically. To try to stop it, I have run the build process with qmake -nodepend, but that hasn't stopped the link to devdir from happening.
How do I force qmake to link only to libraries in installdir?
Code
In myapplication.pro:
QMAKE_LFLAGS = -Wl,-rpath,/usr/lib/myapplication
The resulting link flags in the Makefile are:
LFLAGS = -Wl,-rpath,/usr/lib/myapplication -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-rpath,/home/user/Qt/5.3/gcc_64 -Wl,-rpath,/home/user/Qt/5.3/gcc_64/lib

The path to /home/user/Qt/5.3/gcc_64 can be removed by overwriting QMAKE_RPATHDIR. To suppress both paths the variable QMAKE_LFLAGS_RPATH should be empty as in Setting RPATH order in QMake
# rpath variables for unix
unix: {
# suppress the default RPATH
QMAKE_LFLAGS_RPATH =
# add custom path
QMAKE_LFLAGS = -Wl,-rpath,/usr/lib/myapplication
}

Related

How to specify Libs variable of pkgconfig during its generation by qmake?

I want to set Libs: line via qmake .pro file and I try doing it like this:
LIBS += -ldl
to make my target executable be also linked with dl library because my library I am trying to link it with uses dl inside. However, this does not work, and qmake puts this string into Libs.private section instead which is not passed to gcc command line during compilation and so I am unable to produce an executable:
Libs.private: -ldl
while I want it to be in Libs: line like that:
Libs: -lmylibrary -ldl
I also can't find any references to QMAKE_PKGCONFIG_* variables anywhere in the web, is there any?
Is used for generating .pc files:
CONFIG += create_pc create_prl no_install_prl
By greping for QMAKE_PKGCONFIG_ I found the following in ~/5.12.0/clang_64/mkspecs/features/qt_module.prf
QMAKE_PKGCONFIG_DESTDIR
QMAKE_PKGCONFIG_LIBDIR
QMAKE_PKGCONFIG_INCDIR
QMAKE_PKGCONFIG_CFLAGS
QMAKE_PKGCONFIG_NAME
QMAKE_PKGCONFIG_FILE
QMAKE_PKGCONFIG_REQUIRES
QMAKE_PKGCONFIG_DESCRIPTION
QMAKE_PKGCONFIG_INSTALL_REPLACE
From the man page for pkg-config:
Libs.private:
This line should list any private libraries in use.
Private libraries are libraries which are not exposed through your library,
but are needed in the case of static linking.
This differs from Requires.private in that it references
libraries that do not have package files installed.
So i guess as long as you don't export them through your lib they will end up in Libs.private.

Can you specify the path to exe in qmake?

Let's talk about it in linux terms. I have a ".so" file. I want to make the executable dependent on it, look for it in the same directory. How do you do it through qmake? Or can this only be achieved through the use of QLibrary?
For example, when you have a ".so" file and want to use it in your project, in qmake you write:
LIBS += -L"path to the folder that contains your .so" -lSoName
But the path is hardcoded, as you can see, and I'm wondering what to write there to make the executable look for the ".so" in the same directory.
You use RPATH
You can configure your binary or library to find shared library (or dll) in current directory using RPATH directive that is emedded in the binary itself which the loader respects at runtime
1- Add the following in your .pro file
unix {
message("Adding RPATH to the app")
QMAKE_LFLAGS += -Wl,-rpath=\'\$$ORIGIN/\'
QMAKE_RPATH =
}
This will set RPATH of executable to current directory and your executable will first try to look for that .so in your current directory and then in standard directory (this process is explained here)
2- After you compile and create binary VERIFY that RPATH is set correctly
objdump -x <path/to/binary> |grep RPATH
it should say $ORIGIN
Compile time configuration:
CXXFLAGS += -L"/path/to/libmarylin.so/file" -lmarylin
There are a few ways to do it.
If the executable is going in the current directory, just do LIBS += -L$$(PWD) -lSoName.
If you're putting the executable into some other sub-directory, specified by some qmake variable like DESTDIR, use LIBS += -L$$(DESTDIR) (or whatever variable holds that directory).
Alternatively, you can add the directory with the executable to the runtime path of the executable, which gives the dynamic linker a list of directories to search for any unresolved libraries at runtime. This can be done with QMAKE_RPATHDIR += -L$$(PWD) and LIBS += -lSoName, which will tell the linker to look for unresolved libraries in the current directory where qmake is run.
Some operating systems may also include the current directory in the runtime search path for libraries by default, in which case just doing LIBS += -lSoName should be sufficient. That is platform-dependent, though, while the above solutions are not.

Using Qt Creator to develop an Apache2 module

I'm developing an Apache2 module as a subproject of a larger project. The source file is (for example) module_example.c. Apache2 modules are compiled with apxs2, thus:
% apxs2 -c module_example.c
I've successfully added rules (patterned on how ODB works) to my Qt Creator .pro file to find and use apxs2, like this:
APXS_FLAGS =
APXS_FILES += module_example.c
for(dir, APXS_FILES) {
APXS_PWD_FILES += $$PWD/$${dir}
}
apxs.name = apxs2 -c ${QMAKE_FILE_IN}
apxs.input = APXS_PWD_FILES
apxs.output = ${QMAKE_FILE_BASE}.so
apxs.commands = apxs2 $$APXS_FLAGS -c ${QMAKE_FILE_IN}
apxs.depends = $$APXS_PWD_FILES
apxs.clean = module_${QMAKE_FILE_BASE}.so
QMAKE_EXTRA_COMPILERS += apxs
QtCreator correctly compiles the module using apxs2 (although it leaves the binaries in the source directory instead of the build directory, which isn't ideal), but then also tries to compile it with GCC (which fails). How do I tell Qt Creator to use my "extra compiler" instead of the normal one? Changing the extension to something else (module_example.apxs, for example) doesn't appear to be an option, because apxs2 has no option to specify the extension of C source files. Any ideas?
It's not Qt Creator that does it, but qmake. You need to remove module_example.c from SOURCES. The problem is in the part of the .pro file you're not showing.
If you wish to easily access the file from Qt Creator, add it to OTHER_FILES. It will be shown in the project structure in the IDE, but won't be compiled by default.
Also, it's up to you to tell apxs to output to the build path. You need ${OUT_PWD}.

GLIBC_2.9 missing

I am working in embedded system domain and i was trying to cross-compile a QT- program for the ARMv7 platform.
I am using the following cross-compiler ---
" angstrom-2011.03-x86_64-linux-armv7a-linux-gnueabi-toolchain-qte-4.6.3.tar "
I had all the shared libraries required by the program inside /usr/lib folder ( I am using Angstrom-linux in my target platform ) so i guess there is no need to give path of my shared libraries to the loader. But still it is showing some error that some of the shared libraries require GLIBC_2.9 ( "error : GLIBC_2.9 not found" ). I don't have a gcc compiler for my target platform so i am cross-compiling my programs, is that the reason ???????????
This is what you should do in a qmake based project for cross-compiling in your scenario:
qmake -r -spec linux-arm-gnueabi-g++
and the qmake project file should contain this:
LIBS += /usr/local/.../lib # path to your embedded target libraries
Note that you may also need to set up the following variable, too, for having the includes right:
INCLUDEPATH += /usr/local/.../include # path to your embedded target headers

How to Include OpenSSL in a Qt project

I'm new to Qt, I've done some Googleing and can't find a detailed enough answer.
I need to use OpenSSL in my qmake-based Qt project. How do I go about downloading/installing/linking it so I can just do an include statement and use its functions in my code?
Assuming Windows, you can download its installation from Win32 OpenSSL Installation Project page. You can choose one for 64-bit windows developing or for 32-bit. Just run the setup and everything will be done easily. The default installation directory is : C:\OpenSSL-Win32
In Qt creator, if you then want to link a library to your project you can just add this line to your .pro file(project file ) :
LIBS += -L/path/to -llibname
So here's what we do for this library( for example to link ubsec.lib )
LIBS += -LC:/OpenSSL-Win32/lib -lubsec
Pay attention to -L and -l.See this question. You don't even need to specify .lib at the end of the library name.
For including .h files add this line to your .pro file :
INCLUDEPATH += C:/OpenSSL-Win32/include
after that you can include a file like this :
#include <openssl/aes.h>
From George at Unable to use AES files of OpenSSL in Qt Creator:
If this is on Linux, add the following into your .pro file:
PKGCONFIG += openssl
It will handle all necessary header paths, compile-linker options and
the libraries.
And make sure you have the openssl-devel package installed in your
system.
I was working on Win 7 (32) with Qt5.5, and non of these answers worked for me.
So I just want to share a solution that finally worked:
1. I have OpenSSL installed in C:\OpenSSL-Win32
2. In c:\OpenSSL-Win32\MinGW there are two library files:
libeay32.a & ssleay32.a
3. I've made a copy of each of them an renamed the extensions:
libeay32.a -> libeay32.lib & ssleay32.a -> ssleay32.lib
4. I linked libraries in my .pro file this way:
LIBS += -LC:/OpenSSL-Win32/lib/MinGW -llibeay32
LIBS += -LC:/OpenSSL-Win32/lib/MinGW -lssleay32
INCLUDEPATH += C:/OpenSSL-Win32/include
5. I copied 3 .dll files from C:\OpenSSL-Win32:
(libeay32.dll, libssl32.dll, ssleay32.dll)
to my build/debug folder:
(build-XXXXX-Desktop_Qt_5_5_1_MSVC2012_32bit-Debug/debug)
I hope this will help.
If you use cmake as build system for your project, then you may include FindOpenSSL.cmake as follows:
#set(OPENSSL_USE_STATIC_LIBS TRUE) # if you want to use static libssl.a and libcrypto.a
include(FindOpenSSL)
#add_executable(${PROJECT_NAME} ...) or add_library(${PROJECT_NAME} ...)
target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_DL_LIBS} OpenSSL::SSL OpenSSL::Crypto)
${CMAKE_DL_LIBS} is required on Linux systems to avoid link-time errors like "dlopen symbol not found...". On windows it became empty.
If openssl installation directory is not being standard, then you should provide OPENSSL_ROOT_DIR to cmake, e.g. to add set(OPENSSL_ROOT_DIR "C:/msys64/mingw32") before include or to specify -DOPENSSL_ROOT_DIR:PATH=C:/msys64/mingw32 to cmake executable (in "Projects"->"Build settings"->"CMake" tab).
if you are on win7,and your qt version is mingw, and u install openssl from http://slproweb.com/products/Win32OpenSSL.html ,make sure your lib should be in the OpenSSL-Win32/lib/MinGW, and add a "lib" pre to the libeay32.a and ssleay32.a 。

Resources