QMAKE: Link static library against another static library - c++

I have a QMAKE staticlib project (libtest.a) that deppends to ther subproject (liblogger.a)
I try to add his to test.pro:
LIBS += -L$$OUT_PWD/../libs/logger/ -llogger
PRE_TARGETDEPS += $$OUT_PWD/../libs/logger/liblogger.a
but, finaly, qmake pack .a library without liblogger.a
ar cqs libtest.a libtest.o
I can't use QMAKE_AR += or QMAKE_LIBFLAGS +=, and I need to build static library with this AR command
ar cqs libtest.a libtest.o ../libs/logger/liblogger.a
any idea?

One possible strategy is to add a custom build target that depends on both your project (libtest.a) and the subproject (liblogger.a), which outputs the unified library you're looking for by calling ar.
You can check qmake's documentation here.
Afterwards, set that custom build step as a dependency of the project that includes main().

Related

Libharu explain link in QT c++

I download libharu.zip, try to include it into QT creator c++.
But I don't have lib*.a files in libharu. What do I need to do? It has some makefiles etc.
I find some info about makefile, but don`t understand how it works in QT creator.
nmake -f Makefile.win
What do I need to run this command?
You will need a .so or a .a for linking, in qt creator edit your .pro:
INCLUDEPATH += <path_to_lib>/your_lib_include
When you have a .so for linking:
LIBS += -L<path_to_lib> -l<libname>
When you have a .a for linking:
LIBS += <path_to_lib>/libname.a

How can I make OpenCV the default library for my qt projects?

Please suppose that I want to link OpenCV libraries in Qt-creator, in common, I will add headers usingINCLUDEPATH and link libraries using the LIBS variable, which is used in the qmake file but if we use OpenCV in most of our projects then we have to include OpenCV library every time, so is there any way to add opencv libraries automatically at the time of creating a project.
I use the below command to add OpenCV libraries for my projects every time.
INCLUDEPATH += -I/usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_stitching -lopencv_superres ...and etc.
UPDATE
I will use the following headers for OpenCV4:
INCLUDEPATH += /usr/local/include/opencv4
1) You can create a .prf (project feature) file in your mkspecs/features directory:
/usr/share/qt5/mkspecs/features/opencv.prf
INCLUDEPATH += -I/usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_stitching -lopencv_superres ...and another libraries
Now simply add CONFIG += opencv to your .pro file to have it working. Or you can even auto-enable this feature by editing mkspecs/qconfig.pri:
/usr/share/qt5/mkspecs/qconfig.pri
...
CONFIG += ... opencv
...
BTW. qconfig.pri is a part of qt_config, which is loaded by all QMake's machine-dependent specs, so it should always work. However, it's also possible to patch only a specific spec (for example, /usr/share/qt5/mkspecs/linux-g++/qmake.conf, or whatever is appropriate for your configuration). Of course, it's even possible to add all these INCLUDEPATH+=... and LIBS+=... straight into that qmake.conf and get rid of the .prf file completely.
2) Alternatively, if you don't want to pollute Qt installation, you can use manual include:
opencv.pri
INCLUDEPATH += -I/usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_stitching -lopencv_superres ...and another libraries
myprogram.pro
include(path/to/opencv.pri)
...
When you installed opencv you must also install the opencv.pc file, this file can be used to make it simple, since Qt supports package.config, if so, it replaces what it shows by the following:
unix: CONFIG += link_pkgconfig
unix: PKGCONFIG += opencv
Actually Qt Creator offers a simple way, you just have to right click on the name of your project and select the option Add Library:
Then you will get a dialog where you must select the type of library:
In this case I used the fourth option, and put the name of the library: opencv.
Then you press the next and finish buttons.

How to link to SDL2 libraries under Qt Creator

I have compiled the latest SDL2 libraries, obtained from the 'official' mercurial repository, and followed the instructions for the Ubuntu/Linux build.
But Qt creator fails to link the statically built libraries. Here's the qmake script:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
unix:!macx: LIBS += -L/usr/local/lib/libSDL2.a
INCLUDEPATH += /usr/local/include
SOURCES += main.cpp
The linker reports several undefined references, including SDL_Init.
You have to change your LIBS line to this:
LIBS += -L/usr/local/lib -lSDL2
as -L let you define the path where linker looks for libraries to link, while -l defines which library to link to. On Unix systems the library called ASD is represented by a libASD.so file (in this example .so is for shared library, in your case there is .a as it is static library).
EDIT:
I've prepared very simple main.cpp:
#include <SDL/SDL.h>
int main()
{
SDL_Init(SDL_INIT_VIDEO);
return 0;
}
build SDL 2.0.3 as static library with /usr/local prefix and I needed to add 2 other libraries to my .pro file to compile this. Here it is:
TEMPLATE = app
CONFIG += console
CONFIG -= qt
CONFIG -= app_bundle
SOURCES += main.cpp
LIBS += -L/usr/local/lib -lSDL2 -ldl -lpthread
INCLUDES += /usr/local/include
And now it compiles flawlessly.

Add static .a library in QtCreator

I want to use functionality of a math library ALGLIB and it's offered in .h and .cpp files. So I build it and added all the .o files to alglib.a. I copied it to my source directory and added these lines to my .pro file:
INCLUDEPATH += /path/to/ALGLIB/cpp/src
LIBS += -Lalglib
Well - I still get those "undefined reference to ..." errors when trying to build.
-L sets a directory in which the linker should search for libraries.
-l sets a library file to link in the following way: -lalglib will look for a file named libalglib.a in all directories that are set with -L
Adding a file to LIBS without anything will link that exact file.
So either:
LIBS += alglib.a
or, provided that the alglib file name is libalglib.a:
LIBS += -Lalglib-directory -lalglib

How to maintain self-made libraries?

I'm using Qt Creator 2.7.0 on ubuntu 13.04.
I've just recently ran into the idea of using libraries, and they're a whole new thing for me.
I've just figured that I need to have the following in my application's .pro file to use a library of my own:
LIBS += -L<lib's dir> -l<lib's name>
INCLUDEPATH += <headers' dir>
// for example:
LIBS += -L$$PWD/../MyLib/release/ -lMyLib
INCLUDEPATH += $$PWD/../MyLib/src/
As you see, I have all my projects in a folder called Programming (the .. in this case)
Each of my project have .pro and .pro.user files in the root, source files in a sub folder /src and the release in an other sub folder /release.
So, this is what my Programming folder looks like:
Programming
MyLib
MyLib.pro
MyLib.pro.user
src
myclass.h
myclass.cpp
release
libMyLib.a
Makefile
myclass.o
MyApp
MyApp.pro
MyApp.pro.user
src
main.cpp
release
main.o
Makefile
MyApp
However, I figured that I could create a folder Programming/libs/, and add libMyLib.a and myclass.h files inside that libs folder.
I would do the same for all of my libraries, and then I could always include them like this:
LIBS += -L$$PWD/../lib/ -lMyLib
INCLUDEPATH += $$PWD/../lib/
The problem is, I'd get include path for every library stored on my computer and the libs folder would become a mess, especially if there are two headers with same name on different libraries.
I'm really new to using libraries, is there a general solution on how they should be located on your computer, and how to include them into your projects?
You could mimic libraries like Boost and have a directory tree like this:
MyLib
build
Makefile, .pro or .sln file here
lib
MyLib
// your .so / .a here
include
MyLib
// your .h here
src
// your .cpp here
CMake or qmake file here
This way you have an out-of-source build tree (in build/) so that your binary files are not mixed up with your source files.
The lib/ and include/ directories are handy because you can then define an install build target. If you then type
make install
it will copy everything to usr/local/lib and user/local/include so that your App can simply do #include <MyLib/some_header.h> and you can link directly against your library binaries (because you copied everything to a location in your system wide path). There is also no danger of name clashes because you wrapped it inside your own MyLib subdirectory.