Qt can't find my shared library - no really it cant - c++

I'm new to Qt and I'm trying to implement a simple shared library for use in another application. I've read the wiki and other docs, but even after carefully following the steps, the compiler will always throw an error saying it can't find my header files.
I'm using Qt MinGW on Windows 10. Here's what I did:
Created and compiled a shared library called libgx
In another project (hello), added the generated liblibgx.a file, using the "Add Library" wizard.
Pulled my hair over this error I get when I try to include the library's header into hello's main.cpp
The wiki is not helpful here at all. It tells me, "MinGW compiled linking libraries are in .a, but you will need to add it manually (as of Qt Creator 2.7). You could also try simply linking the .dll directly cause it would probably work." I'm like WHAT?
hello's .pro file
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = hello
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp\
C:/Users/sagar.JARVISAIO/Documents/libgx/libgx.cpp
HEADERS += mainwindow.h\
C:/Users/sagar.JARVISAIO/Documents/libgx/libgx.h
FORMS += mainwindow.ui
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../build-libgx-Desktop_Qt_5_5_0_MinGW_32bit-Release/release/ -llibgx
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../build-libgx-Desktop_Qt_5_5_0_MinGW_32bit-Release/debug/ -llibgx
else:unix: LIBS += -L$$PWD/../build-libgx-Desktop_Qt_5_5_0_MinGW_32bit-Release/ -llibgx
INCLUDEPATH += $$PWD/../build-libgx-Desktop_Qt_5_5_0_MinGW_32bit-Release/release
DEPENDPATH += $$PWD/../build-libgx-Desktop_Qt_5_5_0_MinGW_32bit-Release/release

I manually added the headers and the library files in the .pro file using the procedure here. Never going to bother with that "wizard" again. It was unnecessarily adding a $$PWD variable in all my paths which I think was causing my problem.

Related

Qt won't recognize headers from sub directory /Include

Environment
I am on Windows 7 64bit
Qt Creator 3.4.2 (opensource)
Based on Qt 5.5.0 (MSVC 2013, 32 bit)
Goal
I am using a 3rd party library. I need to #include the library files like that since that is how the library files include their dependencies.
What I have tried
in myFunction.h (in same directory as .pro file)
#include <lib_header.h>
...
in .pro file
LIBS += -L"$$PWD/Debug/" -llib_name
SUBDIRS += "$$PWD/Include"
DEPENDPATH += "$$PWD/Include"
INCLUDEPATH += "$$PWD/Include"
DEPENDPATH += "C:/Users/Steves Laptop/UX3D/Include"
INCLUDEPATH += "C:/Users/Steves Laptop/UX3D/Include"
VPATH += "$$PWD/Include"
VPATH += "C:/Users/Steves Laptop/UX3D/Include"
...
lib_header.h is in /Include
Results
I get tool tip when hovering over the #include line of the exact location of the file. On compile it says it can't be found. I expect this is a newbie mistake but have spent 3 hrs wasted so far. If in my files I do it like this #include <Include/lib_header.h> it works. I need a way to reference them directly though in order for the rest of the library to work.
Even if someone could provide a link or reference source file that documents Qt's implementation of the #include <> tag that would be sufficient.

Qt compiler cannot find the header file

I know it's a quite common issue but I haven't found a comprehensive answer on the following question.
I have Qt 5.4.1 MSVC2013 build running on Windows 8.1.
Here is a look on my project files:
And here is what my .pro file looks like:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Vfp
TEMPLATE = app
QMAKE_CXXFLAGS = - std=c++11
SOURCES += main.cpp\
Views/mainview.cpp
HEADERS += Views/mainview.h
FORMS += Views/mainview.ui
The problem is, unless I add INCLUDEPATH += Views/ to the .pro file, I cannot include mainview.h in main.cpp file.
Why? Shouldn't HEADERS += Views/mainview.h be enough?
If you're including the header file this way:
#include "mainview.h"
Then yes, you need to add that include path since the compiler (not the IDE) doesn't know where mainview.h is.
Otherwise, you need to specify the relative path to the file, like:
#include "Views/mainview.h"
Documentation doesn't say that HEADERS is for specifying include paths to the compiler. HEADERS is used for generating dependency information and checking whether moc is necessary.
If you add below command to .pro file you will able to compile it.
INCLUDEPATH += ...path/Views/

Cannot open include file pcap.h in QT creator (C1083)

I am trying to link winpcap into my QT creator project.
For that I've added this to my .pro file:
INCLUDEPATH += C:/WpdPack/Include
LIBS += -LC:/WpdPack/Lib -lwpcap - lpacket
When I type #include <pcap.h> the code assistant autocompletes but on compile I get Error:C1083: Cannot open include file: 'pcap.h': No such file or directory.
Any help would be very appreciated.
Typically the solution pops up after asking the question:
I've found a workaround
INCLUDEPATH += C:/WpdPack/Include
LIBS += C:/WpdPack/Lib/wpcap.lib
LIBS += C:/WpdPack/Lib/Packet.lib
CONFIG += no_lflags_merge
And it builds.

Using DLLs Qt - Undefined Reference

I am trying to use and make a DLL in Qt. The DLL, does not use any Qt frameworks. Here is my library's .pro file:
TARGET = MyLib
TEMPLATE = lib
include(Botan.pri)
win32:INCLUDEPATH += "C:/botan/include"
win32:LIBS += "C:/botan/libBotan.a"
unix:INCLUDEPATH += "/usr/local/include/botan-1.10"
unix:LIBS += "/usr/local/lib/libbotan-1.10.a"
HEADERS += \
HEADERS HERE
SOURCES += \
SOUCRES HERE
My library is compiled successfully, and I get a MyLib.dll in my debug/release folder. I then copied my library sources completely, and removed everything besides the header files for the 'includes' folder.
I then created a new project to use my library.
I added this to my project file:
INCLUDEPATH += "C:/Users/Stevie/Desktop/MyLib/include"
LIBS += "C:/Users/Stevie/Desktop/MyLib/MyLib.dll"
The headers have no problem, and it finds my DLL fine (if I change it to a non-existing path, it throw an error. It doesn't as of now.)
Now when I go into my '.cpp' file, I include my header file, and try and use my library and it throws 'undefined reference to MyLib::...'. I have no idea why, as I am including the DLL and I believe it should be found perfectly fine.
Also, I am 99% sure it isn't with Botan, as I use Botan often like this, and it works fine. Anyway, I include the 'Botan.dll' with it anyway just to be sure, but it's not throwing the undefined errors on Botan.
Thanks.
Replace
LIBS += "C:/Users/Stevie/Desktop/MyLib/MyLib.dll"
with
LIBS += -L$$quote(C:/Users/Stevie/Desktop/MyLib)
LIBS += -l$$quote(MyLib)
Does your library have Q_DECL_EXPORT / Q_DECL_IMPORT macros?
After all that clean and rebuild your project, which use library.

QT Creator - Using External Libraries Within My Own Library

I am developing an application in QT Creator in c++ on Linux
I have created my own library so that I can use some common classes throughout a set of applications.
In the library I have created I have used another external static library (libSDL.a).
I have configured my library to a static library (*.a) and it compiles with no problems.
I then added my library to another application and used some of the classes. When trying to compile my application I am getting undefined references from within my library to function calls to the other library.
From my understanding, static libraries are suppose to be copied in during compilation. Why am I getting the undefined references to a library that should be copied into my library?
Here is how the library project is configured in the *.pro file:
QT -= gui
TARGET = FoobarTools
TEMPLATE = lib
CONFIG += staticlib
CONFIG -= shared
DEFINES += FOOBARTOOLS_LIBRARY
INCLUDEPATH += ./include/SDL_Headers/
LIBS += -L./bin/ -lSDL
SOURCES += ...
HEADERS += ...
Here is how my application *.pro file is using my library:
QT -= gui
TARGET = FoobarApp
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
INCLUDEPATH += ./include/
LIBS += -L./bin/ -lFoobarTools
SOURCES += ...
HEADERS += ...
In the application's .pro you need:
INCLUDEPATH += LibraryPath (This points to the header-file's directory.)
DEPENDPATH += LibraryPath (This also points to the header-file's directory.)
LIBS += -LDebugOrReleasePath -lLibraryName (This is the lib-filename minus 'lib' at the beginning and '.a' at the end.)
Once that's done check if the #includes to your custom library are still working.
In the static libary's .pro file you dont need to touch anything, maybe add 'CONFIG += release'.
Both your library and the library it's using should be linked in the application.
INCLUDEPATH += ./include/SDL_Headers/
INCLUDEPATH += ./include/
LIBS += -L./bin/ -lFoobarTools
LIBS += -L./bin/ -lSDL
//And dont forget the Target dependencies.
PRE_TARGETDEPS += ./libFoobarTools.a
PRE_TARGETDEPS ./libSDL.a
If you want to find out more about the reason the library compiles but not the application check out this question.
I think you can find the answer here:
http://gcc.gnu.org/ml/gcc-help/2004-04/msg00104.html
and, more precisely in this follow-up:
http://gcc.gnu.org/ml/gcc-help/2004-04/msg00106.html
I cannot test it right now, but I think the probable cause might be resumed by this:
"linker will "throw away" a library if it comes across it but none of the symbols it defines are needed"
As a first-aid help, add the -lSDL to your second .pro file.
Edit: are you sure that your static library (the first .pro file) really uses some symbols from libSDL? If not, then the compiler will simply ignore the libSDL.a file and will not include it in your static library. Even if you use some symbols from libSDL.a, only those functions will be copied into the executable, while the other symbols would be not (at least, this is what I think). "Static libraries have special rules when it comes to linking. An object from the static library will only added to the binary if the object provides an unresolved symbol." (see: https://stackoverflow.com/a/2649792/1284631). Then, if your executable (the second .pro file) makes use of some not-copied symbols from libSDL, you will have the errors. Quoting the same source: "On Linux, you can change that behavior with the --whole-archive linker option: g++ -Wl,--whole-archive some_static_lib.a -Wl,--no-whole-archive". This way you make sure that you carry the whole static libSDL.a archive within yours.