How to include a .lib file dynamically? - qt

I've got a problem with my project. I am developing it on Qt Creator, and I need to add a .lib file to my project. So, I wrote this line in my .pro :
LIBS += "C:\My_Path\My_Project\lib\file.lib"
This is working fine. My only issue is that I need to write the entire path to the .lib file. Since it's a group project, it needs to be compilable on other computers without changing the path to this file every times.
I tried many syntaxes, like :
LIBS += -L"lib/" -l"file.lib"
or
LIBS += "lib\file.lib"
or
LIBS += "file.lib"
(putting the .lib file at the root and adding it to the project in Qt Creator)
None of them works :/
I also tried to add the .lib file in a QResource file but a syntax like this one :
LIBS += ":/lib/file.lib"
in a .pro file doesn't seem to work...
I am now running out of ideas :(
Help please :)

Did you try
LIBS += -Llib -lfile
or
LIBS += -L./lib -lfile?
Remember if you aren't using the full path, you don't add .lib to the library name and you don't use quotes. If the path has spaces (or you just want to be safe), use $$quote(-L<path name>)
As stated here the Unix style paths work on Windows as well where you normally need to provide the full path. qmake will fill it in for you.
I converted
LIBS += ../../lib/phraseBox.lib
to
LIBS += $$quote(-L../../lib) -lphraseBox
and
LIBS += $$quote(-L../../lib) phraseBox.lib
In one of my project with no problem on Windows. You might want to post your project files somewhere, or post the actual error messages to narrow this down.

Ok, I found the solution :
the lib/ directory has to be placed in the same directory that the Makefile (the real one, not the .pro).
Since I copied the lib/ directory in the build-desktop/ directory, everything is working fine using relative paths :)

From your tries I guess you want to link to the library using relative path which should work just fine by default.
Try this:
LIBS += -L$$relative/directory/path -llibname
If it does not work, then you might want to try setting multiple INCLUDEPATH. :/

Related

Qt: mingw compiled library does only work with both library.so and library.lib file present

I compiled a library using the MinGW toolchain provided with Qt 5.0.2 on Windows. As a result I received a library.so file. First I failed using the library in a Qt application, but now I found out that everything works fine when I make a copy of the liblibrary.so file and call it liblibrary.dll or liblibrary.lib (which is the only file ending supported by the add library wizard in QtCreator).
Now I wonder if this is normal or if I should change something in order not to have both files (which are exact copies). Leaving one away makes the application crash during start up. I added the library as follows to my Qt pro file:
LIBS += -L"../path/to/library" -llibrary
INCLUDEPATH += $$quote(../path/to/library)
EDIT: I compiled the library using the MinGW of Qt, not as Qt project but using mingw32-make and the provided Makefile. As a result I get the liblibrary.so.
EDIT: It seems to work also when renaming the copy to liblibrary.dll instead of .lib. But still, I need two files to make the application work -- the .so and the .dll.
Chris
That's weird, I think you should get a *.a and *.dll files when building a shared lib with MinGW on Windows, as said in the documentation:
In windows, MinGW will output .a and .dll, MSVC2010 will ouput .lib and .dll. In linux, MinGW will output .so, .so.1, .so.1.0 and .so.1.0.0 – .lib, .a and .so are import libraries.
You definitely shouldn't rename your file!
Be careful to:
not to include the "lib" prefix after "-l" in your project file.
put everything after after "-l" in lower case as you're on Windows
not adding any extension to your library name after "-l"
add and reference the .h file used in your library
A real example using QtWebsocket lib:
INCLUDEPATH += "$${PWD}/include/"
LIBS += -L"$${PWD}/libs/" -lqtwebsocket
...
HEADERS += ... \
$${PWD}/include/QWsSocket.h \
...
In my include/ folder, I have the following file:
QWsSocket.h (taken from original project - required)
In my libs/ folder, I have the following file:
libQtWebsocket.a
QtWebsocket.dll
Edit: I struggled with this too initially. Have you tried to build your lib as a static lib instead (CONFIG += staticlib in your library project)? This might help you getting you *.pro file right before switching to using the shared library.
Edit 2: Ok, the fact that you get a *.so file is still a bit odd. In this question
the user has the same issue as you and keep both files, which is just a workaround. According to a later answer it seems that you need to modify your makefile to generate a file with the proper extension. Maybe this will help: http://www.mingw.org/wiki/sampleDLL

Adding each subdirectory's path to qt pro file variable

I want to add path of each subdir having .h file to a qt pro file variable
I found this http://doc.qt.digia.com/qt/qmake-project-files.html#built-in-functions-and-control-flow for use of a for loop but don know how to use if to do above task.
INCLUDEPATH +=$$(MITK_INCLUDE_PATH)
When I wrote this (where path variable having only outer dir's path) its allowing me to include a header in source as:
> #include"ITK-src/Code/BasicFilters/itkAbsImageFilter.h"
and qmake is running properly but when I am building this project its asking for another .h on which above .h depend. Before this I have worked with adding 3'rd party libs in which I gave path of each header file's location to INCLUDEPATH as here but if there is any other way I want to know possibility.

Problem with linking

I made a custom library and i compiled it into a dll (qustom.dll).
Now i want to compile another project using this dll.
What i did is to import the header files in the project and add this line in my .pro file
LIBS += -lqustom
but i get "error: cannot find -lqustom"
also i tried
LIBS += qustom.dll
and
LIBS += -libqustom.a
qustom.dll is in the project directory
Also i tried
OTHER_FILES += \
qextserialport1.dll
but didn't work either
Am i missing something here?
Missing a path perhaps? -L/path/to/dlls -ldllname
Qt plays nicely with CMake. I use that and I never get dependency/path headaches like this any more.

How to use CTelephony API with Qt Symbian

I want to use CTelephony API in Qt Symbian project but after including etel3rdparty.h and etel3rdparty.lib as library and header in .pro file like this:
LIBS += C:/NokiaQtSDK/Symbian/SDK/epoc32/release/armv5/lib/etel3rdparty.lib
INCLUDEPATH += C:/NokiaQtSDK/Symbian/SDK/epoc32/include
I am getting a lot of compilation errors.
Please share if someone faced and find the solution of this problem.
You are not supposed to include each library with the complete path in the .pro file. You need only the name of the lib, without the extension, prefixed with -l, i.e. in your case
LIBS += -letel3rdparty
Also, you would normally not need to add epoc32\include to the include path, as it's added by default.

How can I set where a Qt app finds a Qt module?

I would like to include libQtGui.so.4 libQtNetwork.so.4 and libQtCore.so.4 in the same directory as where my app resides. How would I make Qt understand this? y purpose is to have a standalone app that uses shared libraries
Setting the LD_LIBRARY_PATH environment variable is one option. For example:
export LD_LIBRARY_PATH=/path/to/dir/with/libs:$LD_LIBRARY_PATH
Another option is to set the RPATH of your Qt application during linking. Setting the RPATH to the value "$ORIGIN" will cause the dynamic linker to look in the same directory as your Qt application at runtime. For example, if using qmake, add the following snippet to your project file:
unix:!mac{
QMAKE_LFLAGS += -Wl,--rpath=\\\$\$ORIGIN
QMAKE_LFLAGS += -Wl,--rpath=\\\$\$ORIGIN/lib
QMAKE_LFLAGS += -Wl,--rpath=\\\$\$ORIGIN/libs
QMAKE_RPATH=
}
This will set the RPATH to "$ORIGIN:$ORIGIN/lib:$ORIGIN/libs", meaning that the dynamic linker will first look in the location of your Qt application, then in a lib subdirectory at its location, then in a libs subdirectory at its location, and finally in any system defined locations.
UNIX / Linux is going to look in LD_LIBRARY_PATH (if set) first before looking in the system standard libs. So if you set that, you can indeed override. Just like setting the PATH on Windows. Same effect. The ordering matters.
You can add ./ or . to LD_LIBRARY_PATH as well.
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
LD_LIBRARY_PATH and QMAKE_RPATH never worked for me. Instead, I set QMAKE_RPATHDIR in my .pro file. For example after having built and installed (make install) Qt, it has been placed in /usr/local/Trolltech/Qt-4.8.5/lib/. I then write the following in my .pro file:
QMAKE_RPATHDIR += /usr/local/Trolltech/Qt-4.8.5/lib/
Note 1: Relative paths seem not to work. Prefer absolute paths.
Note 2: When you then make, you can see that the following option is given to the linker: -Wl,-rpath,/usr/local/Trolltech/Qt-4.8.5/lib/
Note 3: To be sure that the binary links dynamically to the correct library, you can display the version of Qt at runtime delivered by qVersion().

Resources