Can't get enumeration on library (undefined reference staticMetaObject) - c++

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.

Related

HDF5 C++ Qt conflicting declaration

Situation: I need to read a .hdf5 file and show the data graphically in an interface created with Qt.
What i did: I created a new project doing nothing but adding the hdf5.lib as an external library to the hdf5_test.pro using the the qt wizard for doing that and #include <hdf5.h> in the mainwindow.h
Problem:
When trying to run the code, i get this Error: conflicting declaration 'typedef long long ssize_t'
Using:
C++
Qt 5.10.1
MinGW 5.3.0 32bit for C++
HDF5 1.10.2
My Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <hdf5.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = hdf5_test
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
win32:CONFIG(release, debug|release): LIBS += -L'C:/Program Files/HDF_Group/HDF5/1.10.2/lib/' -lhdf5
else:win32:CONFIG(debug, debug|release): LIBS += -L'C:/Program Files/HDF_Group/HDF5/1.10.2/lib/' -lhdf5d
else:unix: LIBS += -L'C:/Program Files/HDF_Group/HDF5/1.10.2/lib/' -lhdf5
INCLUDEPATH += 'C:/Program Files/HDF_Group/HDF5/1.10.2/include'
DEPENDPATH += 'C:/Program Files/HDF_Group/HDF5/1.10.2/include'
This might be a simillar problem, but there are no answers.
This is a hack, but the hdf5.h checks for H5_SIZEOF_SSIZE_T to be equal to zero. So using a
#define H5_SIZEOF_SSIZE_T H5_SIZEOF_LONG_LONG
before including hdf5.h could help by not redefining the type.

QtCreator: Are dynamic library generated really "dynamic" on a Mac

I'm working with QtCreator under MacOS to target iPhone for my applications. Right now, I'm trying got compile a 3rd party library (tinyxml).
My .pro file already works for Linux, Windows and Android targets, I'm just trying to extend it to iOS.
My first surprise is that the generated dynamic library has a .a extension. Shouldn't it be .so (or even `.dynlib)?
My second surprise is that when I generate the library with and without CONFIG += staticlib, it always produces the same binary (actually, the same size, content is reported as being different, but it may only be the SONAME tag). On every other platform, static and dynamic library binaries always have a different size.
Am I doing something wrong? Am I actually always generating the library statically (that would explain the extension being always .a), and if so, how to force the dynamic generation?
Dynamic .pro file:
#Generated by SDE CMake scripts!
CONFIG(release, debug|release) {
TARGET = tinyxml
}
CONFIG(debug, debug|release) {
TARGET = tinyxml-g
}
QT -= core
QT -= gui
CONFIG(debug, debug|release) {
DEFINES += _DEBUG
}
win32: DEFINES += TINYXML_EXPORTS
SOURCES += \
/Users/less/Documents/dev/vobs_ext/libcpp/tinyxml/src/tinystr.cpp \
/Users/less/Documents/dev/vobs_ext/libcpp/tinyxml/src/tinyxml.cpp \
/Users/less/Documents/dev/vobs_ext/libcpp/tinyxml/src/tinyxmlerror.cpp \
/Users/less/Documents/dev/vobs_ext/libcpp/tinyxml/src/tinyxmlparser.cpp
TEMPLATE = lib
INCLUDEPATH += \
/Users/less/Documents/dev/vobs_ext/libcpp/tinyxml/./tinyxml
Static .pro file:
#Generated by SDE CMake scripts!
CONFIG(release, debug|release) {
TARGET = tinyxmls
}
CONFIG(debug, debug|release) {
TARGET = tinyxmls-g
}
QT -= core
QT -= gui
CONFIG(debug, debug|release) {
DEFINES += _DEBUG
}
win32: DEFINES += TINYXML_EXPORTS
QMAKE_CXXFLAGS += -DTINYXML_STATIC
SOURCES += \
/Users/less/Documents/dev/vobs_ext/libcpp/tinyxml/src/tinystr.cpp \
/Users/less/Documents/dev/vobs_ext/libcpp/tinyxml/src/tinyxml.cpp \
/Users/less/Documents/dev/vobs_ext/libcpp/tinyxml/src/tinyxmlerror.cpp \
/Users/less/Documents/dev/vobs_ext/libcpp/tinyxml/src/tinyxmlparser.cpp
TEMPLATE = lib
CONFIG += staticlib
INCLUDEPATH += \
/Users/less/Documents/dev/vobs_ext/libcpp/tinyxml/./tinyxml
Got it.
For windows and Android target, default library building is dynamic, so I never had to put anything else than TEMPLATE += lib. On Mac, it's static by default. So, to generate a dynamic library (.dylib), you must request it by adding CONFIG += shared.
This compiles, but crashs upon deployment. I posted this on a different thread: QtCreator for iOS: How to deploy a dylib shared library with my application

QGraphicsItem no such file

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

LNK2001: Unresolved external symbol when using qwt plot in Qt creator

I am subclassing QwtPlot. I have the following error:
moc_myplot.obj:-1: error: LNK2001: unresolved external symbol "public: static struct QMetaObject const QwtPlot::staticMetaObject" (?staticMetaObject#QwtPlot##2UQMetaObject##B)
I have tried to the following things: run qmake, rebuild, clean, delete debug folder, recompile qwt library. That doesn't help. Here is minimal code:
myplot.h:
#ifndef MYPLOT_H
#define MYPLOT_H
#include <QObject>
#include <qwt_plot.h>
class MyPlot : public QwtPlot
{
Q_OBJECT
public:
MyPlot();
};
#endif // MYPLOT_H
myplot.cpp:
#include "myplot.h"
MyPlot::MyPlot()
{
}
Here is .pro file:
#-------------------------------------------------
#
# Project created by QtCreator 2015-06-22T19:33:24
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MyPlot
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
myplot.cpp
HEADERS += mainwindow.h \
myplot.h
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../../qwt-6.1.2/lib/ -lqwt
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../../qwt-6.1.2/lib/ -lqwtd
else:unix: LIBS += -L$$PWD/../../../../../qwt-6.1.2/lib/ -lqwt
INCLUDEPATH += $$PWD/../../../../../qwt-6.1.2/include
DEPENDPATH += $$PWD/../../../../../qwt-6.1.2/include
I am using Qt Creator 3.4.1 Based on Qt 5.4.2 (MSVC 2013, 32 bit). Kit: Desktop Qt 5.4.2 MSVC2013 64bit. Compiler: Microsof Visual C++ Compiler 12.0(amd64). If I comment Q_OBJECT macro in myplot.h everything is ok. I can use qwt_plot without subclassing, so that this->setCentralWidget(new QwtPlot()); line in mainwindow.cpp is ok.
It seems this is an old issue that was present in at least version 4.6.
The workaround is basically a preprocessor define of QWT_DLL from the "very lowest library that calls QWT".

How to include Winpcap to Qt creator?

I tried to use different combination in .pro file but always get these errors :
error: C2065: 'PCAP_SRC_IF_STRING' : undeclared identifier
error: C3861: 'pcap_findalldevs_ex': identifier not found
Here is my .pro file :
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = snif_del
TEMPLATE = app
LIBS += WS2_32.lib
INCLUDEPATH += D:/libs/WpdPack/Include
LIBS += -L D:/libs/WpdPack/Lib/ -lwpcap -lpacket
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
Also i added #include "pcap.h" to my code.
Your .pro file should look like this:
INCLUDEPATH += D:/libs/WpdPack/Include
LIBS += "-LD:/libs/WpdPack/Lib" -lwpcap -lws2_32
If your program uses Win32 specific functions of WinPcap, add
DEFINES += WPCAP
If your program uses the remote capture capabilities of WinPcap, add
DEFINES += HAVE_REMOTE
In your code you should add
#include <winsock2.h>
#include <pcap.h>