Including external libraries to Qt - c++

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.

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.

Adding static library link line to my Qt project file makes no difference

I've read a lot of "duplicate" posts on the subject of statically linking your Qt project on windows to statically built Qt core libs. However, I was left very confused when I compared it to what I am seeing. What I gathered from all the posts is if you already have the statically built Qt libs (which I seem to have here C:\Qt\5.4\mingw491_32\lib) all you need to do is to include a line like so
LIBS += -LC:/Qt/5.4/mingw491_32/lib -lQt5Core -lQtGui
in your .pro file without having to specify -static anywhere. However, this line seems to do exactly ZERO. When I comment it out my project builds exactly the same way without it. The exe is the same size and when I try to run it from command prompt a runtime error appears in a dialog box saying it can't find Qt5Core.dll. Its clearly still linking dynamically. What am I doing wrong? Here is the whole .pro file.
#QT +=
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = testproj
TEMPLATE = app
SOURCES += main.cpp mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
QMAKE_CXXFLAGS +=-std=c++11
#LIBS += -LC:/Qt/5.4/mingw491_32/lib -lQt5Core
I clearly have major holes in my understanding of how Qt linking works on windows. Any help on this topic is highly appreciated.
Qt does not distribute static builds.
It seems you only have a dynamic build and got confused by the .lib/.a files. On Windows, there are 3 types of library:
Dynamic libraries (.dll)
Static libraries (.a or .lib depending on the toolchain)
Import libraries (.a or .lib depending on the toolchain)
Import libraries are only used with dynamic libraries. The import libraries are used by the linker when generating the exe, while the dynamic libraries are used at run time.
So if you have Qt5Core.lib or Qt5Core.a does not mean you have a static build of Qt.
To build your own static version of Qt, I suggest your read Building a static Qt for Windows using MinGW on Qt's wiki.
The major hint is: no matter how Qt is built, you shouldn't need to change anything in the project that uses Qt. Qmake and the configuration files in a given Qt install govern how the applications that use Qt are linked against Qt.
Another hint: a given Qt installation can be either configured for static or dynamic linking, not both. Thus any sort of configuration on the project end is superfluous: a given Qt install already "knows" how to link Qt correctly.
Any given .pro file should work whether Qt is linked statically or dynamically. You should not have any static-linking-specific entries. Remove the LIBS line from the project.
If your executable is dynamically linked, the only reason is that you're using a dynamically linked build of Qt. You should make a static build yourself first, and then use it with your project, and your executable will be statically linked against Qt. It's that simple.
Qt Creator doesn't care about these details either. All it does is invoke qmake and then the make tool. But each qmake is specific to a particular installation of Qt, and that qmake is configured to access that installation's configuration files, and those files contain the data needed to select the proper linking etc. So all of the information is specific to a particular kit one is using, and specifically to the Qt "version" that kit is using (really, a Qt installation because there can be multiple installs of the same version, configured differently).

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

Qt 3rd Party Library Static Linking (QtSerialPort)

Turns out static linking was working, but only for Qt libraries. My 3rd party library QtSerialPort is not linking statically. After some reasearch, I've found that I either have to build this library statically or I have to link directly to a .pri file in my .pro file.
I'm not sure how to do either since it seems QtSerialPort has not been designed for static linking.
The .pri method I really don't understand and has been vaguely described in these two links:
http://qt-project.org/forums/viewthread/15223
http://www.qtcentre.org/archive/index.php/t-54505.html
Does anyone have any adivce on how to get either of these methods to work? Or possibly another method?
Also, MSVCP100.dll is not linking statically if anyone could give me any advice on that.
==================================================================================
I am trying to get Qt to statically link libraries so that I can make a standalone application. I have followed various tutorials on how to build Qt statically then building a static application but I am not having much luck. I believe I have succesfully built Qt with static linking because the application has grown in size from 79KB to 7+MB but I am still getting errors saying QtCore4.dll and QtSerialPort.dll are missing. Also, another issue I'm having when using this static configuration, which isn't too serious, is that when I close my program Windows thinks it has crashed and gives me a window saying MyProgram.exe has stopped working...
I am on a Windows machine using MSVC 2010 with Qt 4.8.5 and am using the third party library QtSerialPort.
What I've done accoring to the guides I've been reading is:
Download and extract qt-everywhere-opensource-src-4.8.5.zip
Open /mkspec/mwin32-msvc2010/qmake.conf and change the follwing lines to
CONFIG += qt warn_on release incremental flat link_prl precompile_header autogen_precompile_source copy_dir_files debug_and_release debug_and_release_target
and
QMAKE_CLFAGS_RELEASE = -O2 -MT
I then open the MSVC2010 command prompt and cd to this . I then enter the commands
configure -static -release -platform win32-msvc2010
nmake sub-src
After this is done I open my project and add
CONFIG += static
to the .pro file. In QtCreator I then go into Projects, Manage Kits then to Qt Versions and browse to the qMake I just generated. I add a new Kit with this version of qMake. I then clean all and switch to this new kit and run qmake from QtCreator. I then use msvc2010 command prompt to go to the directory where the files are generated and then
nmake release
This generates a rather large .exe but like I said, it's still depending on a couple .dll's.
For static linking of external library one have a couple options, both have their pros and cons.
I. Compile the library for static linking yourself. Link to it.
Look for possible existing configuration switches for static linking. There can be something like QTSERIALPORT_STATIC = no, etc. in the library's .pro/.pri files. Just say yes for the library to compile for static linking and go to the step 4!
In .pro/.pri file replace CONFIG += dll with CONFIG += static.
Remove export declarations from the library. Typically Qt library symbols are declared with some definition like QTSERIALPORT_EXPORT which expands to Q_DECL_EXPORT/Q_DECL_IMPORT in shared library build / its header files usage when linking. You'll need to find where this QTSERIALPORT_EXPORT is defined and replace it with empty definition:
#define QTSERIALPORT_EXPORT // in source file
or
DEFINES += QTSERIALPORT_EXPORT # in .pro/.pri file
Build the library.
Link to the library .lib/.a file, use the library header files for symbol declarations in your project.
II. Include the library source files into your project and compile them within it (no linking at all).
Include all the source files of the library into your project (add to SOURCES in qmake project file)
Determine all the stuff the library depends on (other libraries, Qt options, etc.) and include it also into your .pro file.
OR
Include the proper .pri file into your .pro if the library author provides it for in-project compilation (i.e. include(QtSerialPort.pri) or something.)
Remove export/import declarations from the library source code — as described in the item 3 of part I.
Build your project.