Qt, compiling program with dynamic libraried in the same path - qt

I am trying to run my program in other computer without libraries. In *.pro file i added:
LIBS += -L"$$OUT_PWD/libs" -ltinyxml2
LIBS += -L"$$OUT_PWD/libs" -lopencv_highgui -lopencv_core -lopencv_imgcodecs -lopencv_imgproc
LIBS += -L"$$OUT_PWD/libs" -lboost_system
Then i copied libraries to other computer to ~/myprogram/libs and binary file to ~/myprogram, but it can not load libraries
./gpAnalizer: error while loading shared libraries: libtinyxml2.so.2:
cannot open shared object file: No such file or directory

You should use QMAKE_RPATHDIR variable.
Add the follow line to your .pro file:
QMAKE_RPATHDIR += $$OUT_PWD/libs
Of course this may work if $$OUT_PWD is ~/myprogram/. If not then replace it with actual path.

Related

Including library files with qmake without hardcoding the compiler version

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

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

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.

Linking dll to c++ in qt for reading .mat files

I am using Qt Creator 2.4.1(Based on Qt 4.7.4) ..
Now I want to open the .mat file which I got from Matlab.
I am using the basic function matOpen
Initially, I knew the headers required. They are mat.h, matrix.h and tmw.h.
Now, before running the code, I need to link the libraries. The required libraries as far as I know are libmat.dll and libmw.dll . I added them to the .pro file as follows.
QT += core
QT -= gui
TARGET = mat_open_test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
HEADERS += \
mat.h \
matrix.h \
tmwtypes.h
LIBS += -L"C:\Users\skanduri\Documents\C ++\mat_open_test-build-desktop- Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\debug\libmat.dll"
LIBS += -L"C:\Users\skanduri\Documents\C ++\mat_open_test-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\debug\libmx.dll"
Still I get error as : undefined reference to matOpen.. m sure the problem is with the linking.. But I dunno how to solve it. and the compiler Qt is using is MinGw .
You are specifying library path only, not the libraries itself. Try
LIBS += -L"C:\Users\skanduri\Documents\C ++\mat_open_test-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\debug"
LIBS += -llibmx
LIBS += -llibmat

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