There are 2 lib in project (TEMPLATE = lib in .pro file) lib1 and lib2.
And before today lib1 depend on lib2:
LIBS += -llib2 in lib1.pro
also macros was defined and used in class i want to import
#if defined(LIB2_BUILD)
#define LIB2_EXPORT Q_DECL_EXPORT
#else
#define LIB2_EXPORT Q_DECL_IMPORT
#endif
all was fine, but now i need to use some class from lib1 in lib2.
I done:
LIBS += -llib1 in lib2.pro
and define EXPORT masroc for second lib and use it for class wich I want to use in lib2.
but when I add #include "../lib1/header.h" I get error
error: QGraphicsItem: No such file or directory #include <QGraphicsItem>
maybe I forget anything?
sorry, if my description of problem is bad.
UPD
If I delete #include "../lib1/header.h" it compile and work.
UPD2
I found that I can't include <QGraphicsItem> in file from lib2 - same error.
Now I have in .pro file QT += core widgets gui, but before I do this it was:
QT += core widgets
QT -= gui
I run qmake and rebuild project. Nothing change.
SOLUTION
problem was in other libraries which use lib2 and didn't have QT += gui
Related
I have a C++ project using CMake I want my #include to give some idea as to were the thing they are including comes from without having ... For the example below I can use Common.h from MainWindow.h by using either #include "Common.h" or #include "../common/Common.h" depending on how I set my CMake up.
Is there a way to include Common.h from MainWindow.h by using #include "common/Common.h"aka using a relative path from some base folder (src would work)?
The folder structure is roughly:
src:
common:
Common.h
gui:
MainWindow.h
MainWindow.cpp
...
CMake for #include "../common/Common.h" in stc/gui/MainWindow.h.
src:
CMakeLists.txt
common:
Common.h
gui:
MainWindow.h
MainWindow.cpp
...
src/CMakeLists.txt
# ... CMake boiler plate. ...
add_executable(GUI common/Common.h gui/MainWindow.h gui/MainWindow.cpp)
CMake for #include "Common.h"
src:
CMakeLists.txt
common:
CMakeLists.txt
Common.h
gui:
MainWindow.h
MainWindow.cpp
...
src/common/CMakeLists.txt
# ... CMake boiler plate. ...
add_libary(GUICommon Common.h)
src/CMakeLists.txt
# ... CMake boiler plate. ...
add_executable(GUI gui/MainWindow.cpp)
add_subdirectory(common)
target_link_libraries(GUI PRIVATE GUICommon)
Thanks for any help!
You need to add the include directories for your GUICommon target.
After the following line:
add_libary(GUICommon Common.h)
add
target_include_directories(GUICommon PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
When you link to GUICommon using target_link_libraries() the include directory will be added to the current target since you have the PUBLIC in the target_include_directories()
There are a few questions which seem to be similar, but nothing really helps me out. I want to create a static library inside a project and use it in the same project, but linking error occurs.
A good example, which meets my conditions very well is attached to the Qt Ticket QTBUG-45706 https://bugreports.qt.io/browse/QTBUG-45706. In a simple explanation, we have an app which should use some self-made libraries. Just modifiy a few things to see my problem.
app -> main.cpp
#include <QCoreApplication>
#include <lib.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Lib l1;
return a.exec();
}
lib.pro
CONFIG += staticlib
If you now compile the project, you will see the following error
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl Lib2::Lib2(void)" (__imp_??0Lib2##QEAA#XZ) referenced in function main
Use Qt Creator 4.0.3 based on Qt 5.6.1, qmake with mscv2013
What is needed to bring this to work?
CLARIFY:
The project structure is as follow:
subdirs_test.pro (subdir project)
\- app (app project, includes lib and lib2)
\-- app.pro
\-- main.cpp
\- lib (static library)
\-- lib.pro
\-- lib.h
\-- lib_global.h
\-- lib.cpp
\- lib2 (static library)
\-- lib2.pro
\-- lib2.h
\-- lib2_global.h
\-- lib2.cpp
The 'app' project should use the classes from lib and lib2, which are static libraries.
As suggested, use the "Add Library..." doesn't change a thing. In my case, this code will be generated.
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../lib/release/ -llib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../lib/debug/ -llib
else:unix: LIBS += -L$$OUT_PWD/../lib/ -llib
INCLUDEPATH += $$PWD/../lib
DEPENDPATH += $$PWD/../lib
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../lib/release/liblib.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../lib/debug/liblib.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../lib/release/lib.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../lib/debug/lib.lib
else:unix: PRE_TARGETDEPS += $$OUT_PWD/../lib/liblib.a
You can use QtCreators Subdirs project. Here's a detailed step by step instructions how to achieve that with QtCreator.
Pick Subdirs Project from the New Project wizard menu.
Add Subrojects by clicking on created Subdirs project with right
mouse button and selecting New Subproject....
By following wizards you should have a GUI or console Subproject and
a library Subproject. Then click on subproject where you want to link
your library subproject with right mouse button and select Add
Library....
Select Internal library in the dialog and you will be prompted to
choose library you want to add.
Make sure your library subproject is included before gui/console
subproject as subdir project will fail to build.
TEMPLATE = subdirs
SUBDIRS += \
LibProject \
CoreProject
Can you try doing next steps:
Right button on project
Add library
Choose type (external or other)
Set flag on static, like this picture
I have an application which uses a Qt library (shared library). On my library, I have a class with several enumerations which I want use on main application.
I build my library project without problem but I when I build main application project, I have the error :
moc_myapp.cpp:-1: erreur : undefined reference to `MyClass::staticMetaObject'
I didn't find information about this error.
This is my Biblio .pro
QT -= gui
QT += quick multimedia network
TARGET = MyBiblio
TEMPLATE = lib
DEFINES += MYBIBLIO_LIBRARY
SOURCES += myBiblio.cpp
HEADERS += myBiblio.h\
myBiblio_global.h \
myClass.h
This myClass.h :
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QObject>
class MyClass : public QObject
{
Q_OBJECT
public:
enum MyEnumeration {Enum1, Enum2, Enum3};
Q_ENUMS(MyEnumeration)
};
#endif // MYCLASS_H
and myApp .pro :
TEMPLATE = app
QT += qml quick multimedia network widgets sql xml
SOURCES += main.cpp \
myapp.cpp
HEADERS += \
myapp.h
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/pathTo/build-MyBiblio_Qt_5_2_1/release/ -lMyBiblio
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/pathTo/build-MyBiblio_Qt_5_2_1/debug/ -lMyBiblio
INCLUDEPATH += $$PWD/pathTo/MyBiblio
DEPENDPATH += $$PWD/pathTo/MyBiblio
On myapp.cpp, I use enumeration define of the library :
MyClass::MyEnumeration
and i include "myclass.h"
i use Qt 5.2 under Windows.
Thank you for your help
When creating a shared library that you would like to link against, then you need to ensure that the symbols that are going to be used outside the library are properly exported when the library is created. And subsequently imported when you are linking against the library. This can be done using Q_DECL_EXPORT and Q_DECL_IMPORT
You already have the following define in your lib pro:
DEFINES += MYBIBLIO_LIBRARY
Modify your myClass.h as follows:
#if defined MYBIBLIO_LIBRARY
#define MYBIBLIO_LIBRARY_DLLSPEC Q_DECL_EXPORT
#else
#define MYBIBLIO_LIBRARY_DLLSPEC Q_DECL_IMPORT
#endif
class MYBIBLIO_LIBRARY_DLLSPEC MyClass : public QObject
{
Please, read How to create a library with Qt and use it in an application for more information.
I'm trying to use preprocessor directives in C++ for avoiding to compile code that requires a .lib, in case the library cannot be linked.
My .pro file contains:
INCLUDEPATH += "C:/Program Files/Windows Kits/8.0/Include/um"
LIBS += -L"C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86" -l"winscard"
and my directives are of the form:
#ifdef _WINSCARD_H_
// do something
#endif
or
#ifndef _WINSCARD_H_
// do something
#endif
This winscard comes with this windows sdk and I can definitely use its functionalities. The problems arise when I try to restrict the compilation based on these conditional directives.
Code compiles fine when using
INCLUDEPATH += "C:/Program Files/Windows Kits/8.0/Include/um"
LIBS += -L"C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86" -l"winscard"
in the .pro file.
Code is skipped during the compilation phase, as if the library is missing when using the above .pro configurations and the conditional directive, even though the library is available and linked:
#ifdef _WINSCARD_H_
// code that needs to be compiled only when library is present and linked.
#endif
The only change is the introduction of #ifdef _WINSCARD_H_.
It is possible to generate conditional build in qmake basing on file existence.
You can add in your .pro file something like that
exists( C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86/winscard* ) {
message( "Configuring for winscard..." )
INCLUDEPATH += "C:/Program Files/Windows Kits/8.0/Include/um"
LIBS += -L"C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86" -l"winscard"
DEFINES += _WINSCARD_H_
}
The block after the built-in function exists() is parsed only when the path is found (it is possible to use asterisk to match part of filename). Here _WINSCARD_H_ is defined only if required file is found.
So, that macro can be used in source code for conditional compilation.
See qmake Test Functions Reference for details.
I have a codebase where there are some shared libraries. For example LIB1 does some sort of processing that APP1 needs (depends on).
The qmake *.pro files are setup like the following. There is one root *.pro that is a TEMPLATE=subdirs that lists APP1 and LIB1 as it's subdirs.
LIB1.pro:
TARGET = LIB1
TEMPLATE = lib
QT += core
QT += xml
DESTDIR = path/to/libs/directory
SOURCES += \
File1.cpp \
FileN.cpp
HEADERS += \
File1.h \
FileN.h
APP1.pro:
#------ dependencies ------#
LIBS += -Lpath/to/libs/directory
# LIB1
INCLUDEPATH += path/to/libs/directory/LIB1lib
DEPENDPATH += path/to/libs/directory/LIB1lib
LIBS += -lLIB1
HEADERS = \
FileNplus1.h \
FileM.h
SOURCES = \
FileNplus1.cpp \
FileM.cpp
The problem is that when you compile the root *.pro file LIB1 will compile but APP1 fails to compile with the error QDomElement: No such file or directory because APP1.pro doesn't have QT += xml.
There is a hack that I use but I would like to overcome this hack. The hack involves adding the following line to APP1.pro:
# add this to APP1.pro... let's it compile again
QT += xml
Is there anyway to setup your qmake *.pro files such that, APP1.pro depends on LIB1 without needing to modify APP1.pro to add the QT += xml?
(The reason for this question is let's say you have other libraries that depend on other stuff... I would like to have Qt/Qmake take care of the dependencies like for example the Qt += xml dependency.)
qmake allows you to create your own configuration features. (last paragraph)
So you can create your own feature and move your lib1-linking code to it. You can add QT+=xml here. And in app.pro you'll just write CONFIG += lib1