I have the following qmake subdirs project structure and configuration:
* project
* app
- app.pro: INCLUDEPATH += ../support-library
- app-main.cpp: #include <support.h>
* support-library
- support.h
* net-library
- include/net-class.h
* tools (subdirs project)
* tool_1
- tool_1.pro: INCLUDEPATH += ../../net-library/include
- tool-main.cpp: #include <net-class.h>
* tool_2
- tool_2.pro: INCLUDEPATH += ../../support-library
It works, but to be more verbose (and to avoid problems with possible equal file names in the different projects) I consider to add the library name to the include directive.
So for the main app the following configuration would work:
app.pro: INCLUDEPATH += ../
app-main.cpp: #include <support-library/support.h>
However, this would make all headers in all other projects "visible" to the app. Does this cause any disadvantages during the compilation? Or is it by other means bad practice?
Additionally, while support-library is only useful in this project, net-library could possibly used in other projects. Therefore I keep the headers of the public net-library API in a separate include directory. So to use #include <net-library/net-class.h> I would need to place the headers in the project structure as net-library/include/net-library/net-class.h. But it feels needless to have this redundant directory.
Is there any better solution/best practice for this?
Related
I created a static library whit this structure:
QT -= gui
TARGET = Test
TEMPLATE = lib
CONFIG += staticlib
SOURCES += test.cpp
HEADERS += test.h
unix {
target.path = /usr/lib
INSTALLS += target
}
I know that i have also to include in the .pro file of the project those rows
LIBS += -L$$PWD/libs/ -lTest
INCLUDEPATH += $$PWD/libs
and then the header.
#include "test.h"
My question is: if i have to add to the library more than one source file like:
SOURCES += test.cpp \
control.cpp
HEADERS += test.h \
control.h
how can create a global header file to import in my project (indeed to put in the INCLUDEPATH directory also the control.h header?
What i mean is: it's possible to include together to the generated lib(.a) only one header file that contains the others headers?
thanks in advance
You can create any custom header file in QtCreator (File -> New File -> C++ -> C++ Header File), then your .pro will contain it in HEADERS and you can put any #include you want in it.
However in my opinion it's a very bad practice to have all .h included in only one. Not only because of the compilation time when you'll have plenty of classes, but also because when classes have circular references, you'll get stucked (this is I think the main reason not to do what you're trying to do).
My 2 cents.
My questions may seems like it is a real duplicate, but however I checked many related questions, my problem still remains.
I found and compiled a project called SMTPEmail, so I have my .dll and .lib files under the directories Libraries/SMTPClient/debug and Libraries/SMTPClient/release.
When I try to include the header files in example in case of emailadress.h: #include <emailaddress.h> or #include <SMTPEmail/emailaddress.h>, I got the error Cannot open include file 'emailaddress.h'. The Q_DECL_EXPORT modifier is used in the header files.
SMTPEmail.pro:
...
QT += core network
TARGET = SMTPEmail
TEMPLATE = lib
DEFINES += SMTP_BUILD
win32:CONFIG += dll
QMAKE_CXXFLAGS += -fPIC
...
MyProject.pro:
...
INCLUDEPATH += ./Libraries/SMTPClient/debug
DEPENDPATH += ./Libraries/SMTPClient/debug
win32:LIBS += ./Libraries/SMTPClient/debug/SMTPEmail.lib
...
I also tried:
LIBS += -L./Libraries/SMTPClient/debug/ -lSMTPEmail
and
LIBS += -L$$_PRO_FILE_PWD_/Libraries/SMTPClient/debug/ -lSMTPEmail
and
LIBS += -L$$PWD_/Libraries/SMTPClient/debug/ -lSMTPEmail
and
LIBS += -L./MyProject/Libraries/SMTPClient/release/ -lSMTPEmail
The only thing that I didn't do is copying or linking the header files which are inside the library to my app?
I have the strong feeling that I missed a small step somewhere, can you help me pointing out what I am doing wrong?
I think your problem is (at least) this line:
INCLUDEPATH += ./Libraries/SMTPClient/debug
I am almost certain that this is not the right path to the include path where the headers can be found including emailadress.h.
You have explained the LIBS values that you have tried as well as the lib path in great length, but you are getting an include error from the compiler rather than a linkage problem with the libraries from the linker. I would suggest to figure out where the headers are located and add it to the include path as follows:
# This is just pseudo code, but you need something like this
INCLUDEPATH += $$PWD/Includes/SMTPClient
Answering your question of:
The only thing that I didn't to is copying the header files which should be inside the library, is that right?
It depends on what you mean. If you mean whether the library should be self-contained, then the answer is no, unless you are using dynamic library loading with manual symbol resolution and the like, which I do not recommend for simple cases.
If you mean, it is shipped with the project that you are trying to reuse, then sure, and that is why you would need to specify the includepath in your project to that path.
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.
Maybe I'm missing something, but it seems really stupid to me that the only tutorial provided for QtTest framework has you testing the QString class.
The typical use case for unit testing is....testing classes you wrote yourself, but there is no mention on how to reference your classes in a different project for testing in the tutorial and google has failed me as well(and I really doubt copy pasting classes is a good way to do it).
I even thumbed through 3 different Qt books with no mention of QtTest.
You can add include paths to other project directories in your .pro file like so:
INCLUDEPATH += <directory>
Then it should be able to find the headers that you are including.
Edit: Based on comment
That's another story altogether. Undefined reference usually means you are missing a dependency. This can usually be resolved with one of two things.
The simplest is to include the missing source file:
INCLUDEPATH += ../myotherproject/
SOURCES = main.cpp ../myotherproject/missingsource.cpp
Perhaps the better solution is to expose reusable code by compiling it as a library and linking to it. E.g. a .DLL or .LIB on Windows and .SO or .A on Linux.
INCLUDEPATH += ../myotherproject/
win32:LIBS += ../myotherproject/linkme.lib
Can you show us the specific errors you are getting?
I suggest that you put all sources and headers which both your main application project and your unit test project need into one .pri (.pro include) file. Put this file in the main project. Then include this file in both projects.
Note that whenever adding a new class to the main project, QtCreator automatically appends the SOURCES += and HEADERS += lines to the .pro file, but you want them to be in the .pri file, so you need to move them afterwards manually. I think that there is no solution to tell QtCreator where to put them.
Main project:
myproject.pro
myproject.pri
main.cpp
someclass.h
someclass.cpp
myproject.pro:
QT += ...
TARGET = ...
...
SOURCES += main.cpp # "private" to this project
include(myproject.pri) # needed in unit test
myproject.pri:
SOURCES += someclass.cpp
HEADERS += someclass.h
Unit test project:
unittest.pro
main.cpp
test.h
test.cpp
unittest.pro:
QT += ...
TARGET = ...
...
SOURCES += main.cpp test.cpp
HEADERS += test.h
# include the classes from the main project:
INCLUDEPATH += ../myproject/
include(../myproject/myproject.pri)
I'm quite a newbie with C++ and maybe that's a very stupid question, but how do one include a header from a static linked library?
I've created a static library in Qt Creator with the following .pro file:
QT -= gui
TARGET = Foobar
TEMPLATE = lib
CONFIG += staticlib
SOURCES += thefoobar.cpp \
sub/subbar.cpp
HEADERS += thefoobar.h \
sub/subbar.h
compiled it and put the resulting libFoobar.a into the "extstaticlibs" folder of my target project.
In my target projects .pro file i've added the following lines:
LIBS += -L$$PWD/extstaticlibs/ -lFoobar
INCLUDEPATH += $$PWD/extstaticlibs
The target project compiles without problems. But when I try to include the header thefoobar.h in one of my code files:
#include "thefoobar.h"
it always results in an error:
error: thefoobar.h: No such file or directory
Any suggestions for the correct syntax would be very much appreciated.
Kristoffer
Check where you have placed your "thefoobar.h" header file . Place it in the "extstaticlibs/" folder .
If I follow your description correctly, you ONLY put the static library into your extstaticlibs directory.
You need to carry over your thefoobar.h file too. If you follow the common structure you could make:
extstaticlibs/include <- thefoobar.h goes here
extstaticlibs/lib <- libFoobar.a goes here
You then need to modify your project file like this:
LIBS += -L$$PWD/extstaticlibs/lib -lFoobar
INCLUDEPATH += $$PWD/extstaticlibs/include
Of course you can all throw it in one directory, if you want, but it may be helpful to sort things out in the beginning.