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

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.

Related

How to add many libraries in Qt Creator (via project file)

I have directory with many third-party libs (100 libs) and have directory with include files. How can I add all external libs in my project (in project file)?
I try that:
Myproject.pro
LIBS += -L'C:/Program Files/PCL 1.9.1/lib/'
INCLUDEPATH += 'C:/Program Files/PCL 1.9.1/include'
DEPENDPATH += 'C:/Program Files/PCL 1.9.1/include'
but that not work for me. Through Project->Add library-> External... I can add just one lib and it's so long to choose every lib to add.
Also if use that:
LIBS += -L'C:/Program Files/PCL 1.9.1/lib/' -lpcl_common_debug -lpcl_features_release -lpcl_kdtree_release...
It's so long, and I'm sure, should be short way to do it, like just add directory with libs and directory with include directory. Despite that simple thing, I can't find it at any.
I use qt 5.
Also, if you worked with pcl lib, I will glad to listen how to add this lib with all dependencies. Thanks
Nothing to it. Do it manually in the qmake project file after you’ve seen the pattern hinted at by the “Add Library” dialog box. The -L path only needs to be provided once. Then come the names of libraries with the -l prefix. As far as the build scripts go, it’s trivial stuff. You can begin to complain after your .pro file has more than a few hundred lines :)
I mean, let’s be serious: what’s a dozen or two -l name entries? It really is nothing. It should take you more time to ask such a. question than to actually add the libraries to the project.
Alternatively, use cmake, and assuming that the library has a cmake support module, it’ll all turn trivial as the module will pull in the needed dependencies. I’d advise against using qmake for any new development. It’s extremely unfortunate that Qt Creator still offers qmake templates as a default option. They have no place in anything but legacy code. Go cmake or go bust. I mean it.

Include external library in Qt project indipendently of path

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

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

Error compiling C++ program against GeoIP library

I'm trying to compile a simple program using the GeoIP2 lite API. I've compiled the GeoIP Lite program and it created the library files. The .la file is in /mydir/libmaxminddb-0.5.3/src
I modified my .pro file to include:
LIBS += /mydir/libmaxminddb-0.5.3 -lmaxminddb
but when I compile my project errors with "Cannot find -lmaxminddb"
Can someone tell me what's wrong? I've tried changing directories, adding a "lib" prefix to the maxminddb, and more, but I can't figure it out.
(I'm trying to link against libmaxminddb.a which is pointed to by libmaxminddb.la)
I believe in autoconf and friends the -l flags go in the LDFLAGS variabe, not LIBS.
I found elsewhere that with Qt Creator you can right click the project and add an external library. When I do so, I see the .pro file adds:
LIBS
INCLUDEPATH
DEPENDPATH
So that's what you need to add!
That should be LIBS += -L/mydir/libmaxminddb-0.5.3 -lmaxminddb. Note the extra -L in front of the directory name.