Including library files with qmake without hardcoding the compiler version - qt

When I include an external library using Qt Creator, it adds something like the following to my .pro file:
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../build-mylibrary-Desktop_Qt_5_15_1_MSVC2019_64bit-Release/mylibrary/release/ -lmylibrary
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../build-mylibrary-Desktop_Qt_5_15_1_MSVC2019_64bit-Debug/mylibrary/debug/ -lmylibrary
else:unix: LIBS += -L$$PWD/../../../build-mylibrary-Desktop_Qt_5_15_1_MSVC2019_64bit-Release/mylibrary/ -lmylibrary
INCLUDEPATH += $$PWD/../../../mylibrary
DEPENDPATH += $$PWD/../../../mylibrary
The path to the library is specific to the compiler and Qt version. I have several libraries that I would like to distribute. There are various dependencies between them. The libraries can be build by different compilers and different Qt versions. So I don't want to distribute the code with a .pro file that assumes MSVC2019 and Qt 5.15.1 (as in the example above). Is there a standard solution to this problem? Or do I just need to expect people who download the code to change the folder names?

For internal usage, you can use the following qmake variables to dynamically build the libs path (based on the current target):
QT_VERSION
QMAKE_MSC_VER
For external people, such an approach may not work out of the box, as the build path is configured by the external user, not by the .pro-file.
If your goals is to resolve the linking/include dependencies between your libs, SUBDIRS may be a solution.
Note that qmake is being replaced in favour of cmake. Maybe cmake has a better solution, but I'm not very familiar with it.

A possible solution is to use DESTDIR in your .pro files.
For example:
DESTDIR = ../bin
LIBS *= -L$$OUT_PWD/$$DESTDIR -lmylib

Related

LNK 2019 Error: Qt Project works in MINGW, not in MSVC

I have a project with several linked libraries. One particular library links to another and calls a symbol, which the linker claims is undefined.
I've linked to the library that should have this symbol in the .pro file as such:
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../SourceLibrary/release/ -lSourceLibrary
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../SourceLibrary/debug/ -lSourceLibrary
else:unix: LIBS += -L$$OUT_PWD/../SourceLibrary/ -lSourceLibrary
INCLUDEPATH += $$PWD/../SourceLibrary
DEPENDPATH += $$PWD/../SourceLibrary
This links properly using MINGW, so I'm not concerned about typos in library names or function definitions/declarations.
After examining SourceLibrary in Dependencies, I noticed that the required symbols are indeed not there.
This caused me to examine the Makefile for SourceLibrary, however the file with the required symbols was seemingly referenced in the same fashion as the other files that had symbols.
I also checked the intermediary .obj file that was supposed to contain the symbols with dumpbin, and the file indeed had these, and with the same mangling as the linker error report.
Further investigation using dumpbin /ALL revealed that the symbols were in the SourceLibrary.dll file, contradicting Dependencies, as well as the linker error.
In summary, I don't understand why both the linker and Dependencies don't see symbols that should be in a library. I suspect MSVC/MINGW compiler differences, but obviously only have a suspicion at this point.

How to add opencv DLL on Windows to QTCreator project?

I've built opencv and added these lines to .pro file:
INCLUDEPATH += C:/opencv-3.4.1/build/install/include
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_core341d.lib
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_highgui341d.lib
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_imgcodecs341d.lib
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_imgproc341d.lib
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_features2d341d.lib
now if I start the project, it links successfully but fails to start:
Starting C:\Users\steve\Documents\build-qttest4-Desktop_Qt_5_11_0_MSVC2015_64bit-Debug\debug\qttest4.exe...
The program has unexpectedly finished.
The process was ended forcefully.
Can I somehow tell QTCreator to add DLL files to put opencv DLL files to build-qttest4-Desktop_Qt_5_11_0_MSVC2015_64bit-Debug folder?
Here is a solution (which doesn't match top google search tutorial):
INCLUDEPATH += C:/opencv-3.4.1/build/install/include
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_core341d.lib
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_highgui341d.lib
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_imgcodecs341d.lib
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_imgproc341d.lib
LIBS += C:\opencv-3.4.1\build\LIB\Debug\opencv_features2d341d.lib
LIBS += -L"C:/opencv-3.4.1/build/bin/Debug"
and don't forget to run Build->Run qmake.
There are 2 ways to use DLL in some Windows Application
Use WinAPI LoadLibrary function and GetProcAddress function.
Set inside a Qt PRO file a location of LIB file that will tell to application that binary code is in YourLibraryName.DLL. But when you run debugger, application doesn't know where is DLL. One of possible solutions is to put full path to opencv DLL folder to PATH environment variable in windows Control Panel

Importing Libraries into QTCreator

I am creating an OpenGL project in QT Creator, and would like to import GLFW and potentially other libraries later on.
I compiled and imported GLFW and was able to import it via
#include <GLFW/glfw3.h>
Qt Creator was even able to autofill the glfw methods. However, when I attempted to call one of them (e.g. glfwInit();, the compiler threw me the following error:
Undefined symbols for architecture x86_64:
"_CFArrayAppendValue", referenced from:
_addJoystickElement in libglfw3.a(iokit_joystick.m.o)
"_CFArrayApplyFunction", referenced from:
__glfwInitJoysticks in libglfw3.a(iokit_joystick.m.o)
_addJoystickElement in libglfw3.a(iokit_joystick.m.o)
"_CFArrayCreateMutable", referenced from:
... etc
I'm assuming it was unable to find the implementation of these methods.
I imported GLFW through the following procedure:
1. I used CMake to build a Unix Script. 2. I ran "make" in terminal on the generated makefile. This created a file called "libglfw3.a" 3. I imported the library into the project through QT, setting the Library as libglfw3.a and include path as "mypath/include/" (this folder contains another folder called GLFW, which contains glfw3.h).
QT Creator then entered the following into the QT project file.
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/glfw-3.1.1/src/release/ -lglfw3
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/glfw-3.1.1/src/debug/ -lglfw3
else:unix: LIBS += -L$$PWD/glfw-3.1.1/src/ -lglfw3
INCLUDEPATH += $$PWD/glfw-3.1.1/include
DEPENDPATH += $$PWD/glfw-3.1.1/include
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/glfw-3.1.1/src/release/libglfw3.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/glfw-3.1.1/src/debug/libglfw3.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/glfw-3.1.1/src/release/glfw3.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/glfw-3.1.1/src/debug/glfw3.lib
else:unix: PRE_TARGETDEPS += $$PWD/glfw-3.1.1/src/libglfw3.a
Help would be greatly appreciated. I've spent at least 10 hours trying to figure out how to import a GL library into QT Creator, but all I can find are CMake tutorials for XCode, Visual Studio, etc.
I would ideally like to this by just modifying the QT .pro file, because I don't have experience with CMake, but if there are no alternatives, that's fine.
I'm developing with QT creator on Mac 10.10.15.
If anyone's curious, I figured it out.
I followed the procedure here: Compiling with GLFW3, linker errors 'undefined reference'
I didn't at first think this was relevant because it was specifically referring to building on XCode, but it turns out you have to do the exact same thing with QT Creator.
right click the project, Import Library -> System/Library/Frameworks -> and import Cocoa, CoreVideo, and IOKit.
My .pro file had the following lines added to them after I did all that
mac: LIBS += -framework Cocoa
else:unix|win32: LIBS += -lCocoa
mac: LIBS += -framework IOKit
else:unix|win32: LIBS += -lIOKit
mac: LIBS += -framework CoreVideo
else:unix|win32: LIBS += -lCoreVideo

Qt creator: include path for external lib not found

I'm trying to add net-snmp lib to my project using win7 + cygwin:
Project file seems to contain valid lib and path entries:
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/C:/usr/lib/
-lnetsnmp else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/C:/usr/lib/ -lnetsnmpd
INCLUDEPATH += $$PWD/C:/usr/include DEPENDPATH += $$PWD/C:/usr/include
But I can't build the project, because include file is not found, also it exists under given directory physically (c:/usr/include/net-snmp/net-snmp-config.h)
../snmptest1221313123/main.cpp:1:38: fatal error: net-snmp/net-snmp-config.h: No such file or directory
#include
I've read all "add external lib" topics, but it doesn't help to solve this.
Re-running qmake, re-opening Qt creator, or running qmake -r from terminal doesn't help neither.
Remove $$PWD/ (both from include and libs) and try again: run qmake and rebuild.

Qt: Static linking of libraries

I'm developing a Qt application in windows. I have created a lib file in visual studio. I have included the header file containing function prototypes in my Qt App and also added lib file in .pro file as follows
win32: LIBS += -L$$PWD/../Lib/ -lCpLib
INCLUDEPATH += $$PWD/../Lib
DEPENDPATH += $$PWD/../Lib
win32: PRE_TARGETDEPS += $$PWD/../Lib/CpLib.lib
I called the functions from my Qt app. It shows compile error as "undefined reference"
Please help me.
Regards....
The specification of your LIBS is passing linker arguments in gcc format (i.e. using -L to specify the directory and -l to specify the library name).
As you have created your lib file in Visual Studio, you'll need to specify the full library name
e.g.
win32: LIBS += yourlibrary.lib
alternatively, recompile the libraries using MinGW

Resources