Qt external libraries include inside custom library - c++

I have a Qt project that looks like this:
myLib/
myLib.pro
(some *.h, *.cpp)
myProject/
myProject.pro
(some *.h, *.cpp)
myLib.pro includes some external libraries using INCLUDEPATH += and LIBS += (for example Eigen)
myProject depends on myLib, so I added the dependency with the Qt wizard which added to myProject.pro something like:
unix|win32: LIBS += -L$$OUT_PWD/../myLib/ -lmyLib
INCLUDEPATH += $$PWD/../myLib
DEPENDPATH += $$PWD/../myLib
The problem is when I compile the library it works, but when I try to compile the project I get errors like
Eigen/Core: No such file or directory
because in my project's source file I include some headers from my own libraries which include some headers from an external libraries.
The only solution I know to solve this is to copy/paste all the includes and links from myLib.pro to myProject.pro also. Is there a better way to do this?
I was thinking maybe having a *.pri for each of the external libraries and include these *.pri in both *.pro. But this is not completely satisfying.
EDIT:
I forgot to mention myLib is a shared library

The first thing I would check is that you actually need these headers in myProject. Think carefully about the API of myLib and what that requires. If you are passing in Eigen types and matrices then yes you will need to add them to your include path and a .pri file might help with this. You may also want to consider putting something like Eigen in a pre-compiled header. If not then you should look at rearranging your header files to make sure they are only pulling in what is needed for that particular header and move as many includes as possible into your source files. Again this will dramatically help with your compile times.
I don't think you mention whether myLib is a shared or static library. If it is a static library then yes you will need to link myProject to everything that myLib depends upon but the flip side is that deployment becomes easier and function calls into that library become more efficient. If it is a shared library then you only need to link to that library.

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.

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

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.

qmake with INCLUDEPATH ignores dependencies

I use qmake to build a project. The project contains several static libs and a executable. The executable links to the static libraries and therefore has the path of the library added to the INCLUDEPATH variable.
When I change something in the header files of the executable everything is rebuild as expected. When changing a header file of the library it just rebuilds the library and relinks the executable. Source files in the executable that include header files from the library are not rebuild correctly.
After investigating the problem I saw that the generated makefiles do not track the dependencies correctly. Only files included with a relative path are tracked. Any header included via INCLUDEPATH is not tracked. Is there something I can do to make it work as expected?
You should add the paths you added to INCLUDEPATH to DEPENDPATH as well.