Include external library in Qt project indipendently of path - c++

I am developing a C++ app with Qt that depends on the GNU Scientific Library (GSL).
So far, I have linked GSL in the .pro file using LIBS += /path/to/my/GSL/location -l<name_of_the_library> and it works, but it depends on where I installed GSL.
I'd like the .pro file not to depend on the user-specific GSL path, and possibly include the precompiled libraries (for different OSes) in the project folder.
Is this possible?

In this case it works on all platforms. You have to separate the directory from the library name
LIBS += -L/path/to -lpsapi
In this case You store your lib files in the project directory.
LIBS += -L"$$_PRO_FILE_PWD_/3rdparty/libs/" -lpsapi
look at this for more: https://doc.qt.io/qtcreator/creator-project-qmake-libraries.html

Related

LIBS vs PRE_TARGETDEPS in .pro files of Qt

I am a newbie with in Qt & started appreciating the framework that qmake provides in .pro files.
Primary objective of my question is to understand in detail the difference between qmake variables "LIBS" & "PRE_TARGETDEPS" with static linking of libraries.
My Qt App uses a bunch of C++ static libraries that it depends on. Again, the static libraries have interdependencies between themselves. Each library has a .pro file included in it to support qmake way of building. And of course the app also has a .pro file.
Now in the static libraries, if libStaticA is dependent on libStaticB where both are C++ libraries. And both of them have a .pro file each.
Is it enough to mention the dependency in libStaticA.pro with +LIBS & -l like below ?
+LIBS += -L/path_To_libStaticB/ -llibStaticB
Or is it enough to mention the dependency with PRE_TARGETDEPS like below
+PRE_TARGETDEPS += /path_To_libStaticB/libStaticB.a
Or should I mention both ?
+PRE_TARGETDEPS += /path_To_libStaticB/libStaticB.a
+LIBS += /path_To_libStaticB/libStaticB.a
What is the relevance of LIBS & PRE_TARGETDEPS ?
PS: My development machine is osx.
Thanks in advance for any explanations to clarify my understanding here
LIBS:
Specifies a list of libraries to be linked into the project. If you use the Unix -l (library) and -L (library path) flags, qmake handles the libraries correctly on Windows (that is, passes the full path of the library to the linker). The library must exist for qmake to find the directory where a -l lib is located.
PRE_TARGETDEPS:
Lists libraries that the target depends on. Some backends, such as the generators for Visual Studio and Xcode project files, do not support this variable. Generally, this variable is supported internally by these build tools, and it is useful for explicitly listing dependent static libraries.
Qt uses the PRE_TARGETDEPS variable to store dependencies for statically linked libraries. It forces your library to get relinked everytime you build your application.
If you don't have this variable specified and you update and rebuild your library, your program will still use the old library.
For your question, if you use static libraries, you should (almost) always use both, LIB and PRE_TARGETDEPS.
Quote: Qmake variable reference
Also interesting: Adding libraries to Qt Projects

Static linking against non-binary libs

I want to make use of two libraries QCustomPlot and Eigen with Qt Creator on OS X.
Both do not need to be installed and work fine if I just put them into my project folder and add them to the project. They do not have to be installed, "you can use the header files right away".
However I want them to be more independet than that, located outside the project to be used by other projects as well and I don't want their headers and source files to appear with my project files. But I do not know how to link them statically.
INCLUDEPATH += /../../Eigen/Eigen \ and
Add Library... -> External Library
apperently does not work, second one because their is no library file to open.
I have no experience with libraries and tend to find this topic highly complicated.
For the template only include library INCLUDEPATH should be sufficient as noted in comments. Generally, you can do it by manually modifying YourProject.pro file like:
LIBS += -L$$PWD/path_relative_to_pro_file/lib -lmylibfile1 -lmyflibfile2
INCLUDEPATH += $$PWD/path_relative_to_pro_file/lib/include
And your library file names end with .lib.
In case if you want your project to be recompiled because of external library change:
DEPENDPATH += $$PWD/path_relative_to_pro_file/lib
DEPENDPATH += $$PWD/path_relative_to_pro_file/lib/include

Why does Qt add 'PWD' to libraries that are NOT referenced relatively?

Whenever I add a library in the .pro file of a Qt project (in Qt 5.2) it adds a $$PWD before the path of the libraries and include path. For example
INCLUDEPATH += $$PWD/C:/opencv/opencv-msvc2013/install/include
and
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/C:/opencv/opencv-msvc2013/install/x64/vc12/lib/
My question is
why does Qt start the path with the PWD (Present Working Directory) keyword if its providing an absolute path to the libraries? What logic/good programming practice is this convention following?
And most importantly why it does not result in an error? How does Qt know when to search relative to a working directory and when not to? (Since both cases start with the PWD keyword)
PS : I removed the $$PWD keywords and my code worked just fine as well.
In case both the project and the library are in the same drive, that would not happen and the relative path is generated automatically. But on Windows if you add a library which is in another drive, it would add $$PWD followed by an absolute path.
This sounds like a bug and it has been reported here but it's still unresolved.

how to use a library in c++ Qt creator

I downloaded a library for processing DicOM FILES .
The tree of library is :
|--include
|-----dicom.h
|-----dicomcfg.h
|--lib
|--------dicomsdl.lib
But I do not know how to use and built-in Qt Creator .
If anyone can help me I will be very grateful.
I'll assume you use qmake as your build system.
To link your "external" library you need following changes in your .pro file:
#path to includes directory of your library, qmake will try to find includes there
INCLUDEPATH += /path/to/library/include
#path to pre-compiled library directory
LIBS += -L/path/to/precomp/library
#link your pre-compiled library, -l<library name> w/o .so, .a, .lib, lib- prefix
LIBS += -ldicomsdl
Maybe you'll need to build the library from source with your compiler. There could be some troubles if compilers don't match.

Including external libraries to Qt

I am actually new at Qt and would be grateful if someone could explain how to deal with external C++ libraries in theses 3 cases and what is the easiest way to get a library working with Qt (if you could just point me out to some places where I can read about it):
- source .h and header .cpp files both available
- source .h and DLL
- source .h and .a files
I usually use the following procedure:
1- Cmake to generate make files
2- Building using Mingw:
Cd c:/test
qmake test.pro
mingw32-make
3- Including project to Qt:
INCLUDEPATH += C:/test/build/include
LIBS += C:\test\build\x64\mingw\lib\file.dll.a \ ...
I usually use Cmake first then qmake to build, but sometimes one is not working or often Qt option is not available in Cmake. I always read carefully the instructions. In general, how an experience programmer would make decisions on how to include a library?
You do not need cmake and qmake together -- One is enough. I mainly work with qmake when i'm in Qt Creator since it is well integrated with the IDE. Generally what you are doing is correct. You include headers under HEADERS +=, sources under SOURCES +=, libraries under LIBS += and the path to the include files under INCLUDEPATH +=.
Instead of manually adding the external library to the .pro file u can do one thing.
Right click on your main project, then select "add library" option then it will ask for
1. External library
2. Internal Library
3. System Library
then select External library , and rest all thing is done by the Qt Creator i.e it will automatically add the path to the .pro file and link the library to your project.