I have a problem subclassing a VTK class, but I think the problem could be linked to any kind of library.
I did a vtkInteractorStyleImage subclass, just overriding a method:
#ifndef dcmInteractorStyleImage_h
#define dcmInteractorStyleImage_h
#include "vtkInteractionStyleModule.h" // For export macro
#include "vtkInteractorStyleImage.h"
class VTKINTERACTIONSTYLE_EXPORT dcmInteractorStyle : public vtkInteractorStyleImage
{
public:
static dcmInteractorStyle *New();
vtkTypeMacro(dcmInteractorStyle, vtkInteractorStyleImage);
virtual void OnLeftButtonDown();
void PrintSelf(ostream& os, vtkIndent indent);
};
#endif
The thing is I use it in a Qt class. So I include this in my CMakeLists.txt (simplified):
set(GENERIC_VIEW_CPP
View/UI/dcminteractorstyle.cpp # my new class
)
SET(GENERIC_VIEW_H
View/UI/dcminteractorstyle.h # my new class
)
add_library(generic ${GENERIC_VIEW_CPP} ${GENERIC_VIEW_H})
...
add_library(ui_qt ${UI_QT_CXX}
${UI_FORM_HEADERS} ${UI_RESOURCES_RCC}
${MODEL_WRAPPED_HEADERS}
${GENERIC_VIEW_CPP} ${GENERIC_VIEW_H} #necesary here?
)
qt5_use_modules(ui_qt Core Gui Widgets)
----------------------------------------------------------
# VTK
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
# ITK
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
if (ITKVtkGlue_LOADED)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
else()
find_package(ItkVtkGlue REQUIRED)
include(${ItkVtkGlue_USE_FILE})
set(Glue ItkVtkGlue)
endif()
------------------------------------------------
set_source_files_properties(${UI_RESOURCES_RCC} PROPERTIES GENERATED ON)
add_executable(UtilidadDICOM WIN32 main.cpp ${UI_RESOURCES_RCC})
target_link_libraries(UtilidadDICOM
model
ui_qt
generic
${Glue}
${VTK_LIBRARIES}
${ITK_LIBRARIES}
)
The error trying to compile:
ui_qt.lib(viewerwidget.cpp.obj):-1: error: LNK2019: unresolved
external symbol "public: static class dcmInteractorStyle * __cdecl
dcmInteractorStyle::New(void)" (?New#dcmInteractorStyle##SAPAV1#XZ)
referenced in function "public: static class vtkSmartPointer __cdecl vtkSmartPointer::New(void)"
(?New#?$vtkSmartPointer#VdcmInteractorStyle####SA?AV1#XZ)
ui_qt is a library declared in CMake, as you can see above.
Any ideas? Thank you.
Short answer is that your class is not being linked. Try seeing the value in generic just before linking it to your executable. Do a message just before target_link_libraries:
MESSAGE(${generic})
and see if the generic.lib is actually being linked.
Related
I want to make two QObject libraries using cmake on windows: ProA & ProB, where ProB depend on ProA.
The code of ProA looks like:
class ProA_Export ProA: public QWidget
{
Q_OBJECT
public:
...
};
The ProB links ProA in CMake: target_link_libraries(ProB ProA).
Then, when I #include "ProA.h" in ProB, it reproted:
unresolved external symbol "public: static struct QMetaObject const ProA::staticMetaObject"
If I remove the Q_OBJECT from ProA, everything is OK except that the signal-slot of QT do not work.
So, how can I solve the unresolved external symbol problem caused by Q_OBJECT?
Any suggestion is appreciated~~~
--------------------- update --------------------
Finally, I find that it is connect cause the problem.
I change
connect(m_combobox, SIGNAL(currentIndexChanged(int)), this, SLOT(indexChangeSlot(int)));
to
connect(m_combobox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &ProA::indexChangeSlot)
Then, everything is OK. In addition, I find Q_OBJECT is not necessary.
Hope it can help somebody.
In order to test some functions of a Qt application (named qtapp), I build the Qt application as lib/dll library. The ctor and dtor have been correctly exported in the library. However compiling the test project (a Qt console project named consoleTest) is always running to the following link errors:
Link:
1> Bibliothek "C:\Users\gmbh\test\x64\Debug\consoleTest.lib" und Objekt C:\Users\gmbh\test\x64\Debug\consoleTest.exp" werden erstellt.
1>moc_testFour.obj : error LNK2001: Nicht aufgelöstes externes Symbol ""public: static struct QMetaObject const qtapp::staticMetaObject" (?staticMetaObject#qtapp##2UQMetaObject##B)".
1>testFour.obj : error LNK2001: Nicht aufgelöstes externes Symbol ""public: static struct QMetaObject const qtapp::staticMetaObject" (?staticMetaObject#qtapp##2UQMetaObject##B)".
1>C:\Users\gmbh\test\x64\Debug\consoleTest.exe : fatal error LNK1120: 1 nicht aufgelöste Externe
The Qt application is quite simple:
#ifndef QTAPP_H
#define QTAPP_H
#include <QtWidgets/QMainWindow>
#include "ui_qtapp.h"
class __declspec(dllexport) qtapp : public QMainWindow
{
Q_OBJECT
public:
qtapp(QWidget* parent = 0);
~qtapp();
private:
Ui::qtappClass ui;
};
#endif // QTAPP_H
Implementation:
#include "qtapp.h"
__declspec(dllexport) qtapp::qtapp(QWidget* parent)
: QMainWindow(parent)
{
ui.setupUi(this);
}
__declspec(dllexport) qtapp::~qtapp()
{
}
If I simply include #include "qtapp.h" in the test project, the compiler yields the above error.
My questions:
What are the causes for the errors?
How to correctly deploy a Qt
application as lib?
My compiler is visual studio 2013, qt has the version of 5.4.2
That's actually a question independent of Qt. When linking an application against the library you need to have __declspec(dllimport) instead of __declspec(dllexport).
To achieve that, one usually uses defines that change when linking the library vs linking the application against the library. Microsoft has a help page on that "Importing into an Application Using __declspec(dllimport)".
The Qt documentation provides another one. The one thing where Qt can help, is replacing the __declspec by a cross-platform Q_DECL_IMPORT.
Projects that use the QtOpenGL fail to link to gl calls, but only on Windows (Linux is happy). The MSVC 2008 error messages for the following minimal case project are:
1>gllink.obj : error LNK2019: unresolved external symbol __imp__glLoadIdentity#0 referenced in function "protected: virtual void __thiscall ImageWidget::initializeGL(void)" (?initializeGL#ImageWidget##MAEXXZ)
1>gllink.obj : error LNK2019: unresolved external symbol __imp__glMatrixMode#4 referenced in function "protected: virtual void __thiscall ImageWidget::initializeGL(void)" (?initializeGL#ImageWidget##MAEXXZ)
1>gllink.exe : fatal error LNK1120: 2 unresolved externals
These errors disappear when I manually add "opengl32.lib" to the "Additional Dependencies" list, but I believe this should not be necessary, since this should either be done by FindQt4.cmake, or should be taken care of by the dependency of QtOpenGL on opengl32. Of course, I must be mistaken, so I would really appreciate some input on how to properly fix this project.
gllink.cpp
#include <QtOpenGL>
#include <QWidget>
class ImageWidget : public QGLWidget
{
public:
ImageWidget(QWidget* parent = 0) :
QGLWidget(parent)
{
}
protected:
void initializeGL()
{
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
};
int main()
{
ImageWidget w;
return 0;
}
CMakeLists.txt
PROJECT( gllink )
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )
FIND_PACKAGE( Qt4 4.6.0 REQUIRED COMPONENTS QtCore QtGui QtOpenGL )
INCLUDE( ${QT_USE_FILE} )
ADD_EXECUTABLE( gllink gllink.cpp )
TARGET_LINK_LIBRARIES( gllink ${QT_LIBRARIES} )
FindQt4 doesn't take care of that, you have to search for OpenGL by yourself. Now CMake provides a FindOpenGL package, so fixing your project is just a matter of adding find_package( OpenGL ) and linking the libraries to your target:
PROJECT( gllink )
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )
FIND_PACKAGE( OpenGL )
FIND_PACKAGE( Qt4 4.6.0 REQUIRED QtCore QtGui QtOpenGL )
INCLUDE( ${QT_USE_FILE} )
ADD_EXECUTABLE( gllink gllink.cpp )
TARGET_LINK_LIBRARIES( gllink ${OPENGL_LIBRARIES} ${QT_LIBRARIES} )
Btw, you should check if those libraries are really found and warn the user about the problem if they're not :)
I created a new 'C++ library' project in Qt, which has the following header file:
#include "Test_global.h"
#include <QString>
#include <QTcpServer>
class TESTSHARED_EXPORT Test : QTcpServer
{
Q_OJECT
public:
Test();
~Test();
signals:
void NewMessage(QString);
};
(The implementation file is basically empty.)
When I try to build the object, I get errors:
Test.h:8: error: ISO C++ forbids declaration of ‘Q_OJECT’ with no type
Test.h:10: error: expected ‘;’ before ‘public’
Test.cpp:3: error: definition of implicitly-declared 'Test::Test()'
So it looks like moc isn't processing the file at all. What have I done wrong?
It should be Q_OBJECT, not Q_OJECT.
Greetings all,
I am trying to implement a QT Plugin with CMake. But this "Q_EXPORT_PLUGIN2" directive stops my class from compiling. I can compile the plugin if I commented this out,but it won't work as a plugin if I do so.
QT doc says:
Q_EXPORT_PLUGIN2 ( PluginName, ClassName )
The value of PluginName should
correspond to the TARGET specified in
the plugin's project file
What about in CMake case? What should be the value for 'PluginName'?
Here is my Plugin Interface :
#ifndef RZPLUGIN3DVIEWERFACTORY_H_
#define RZPLUGIN3DVIEWERFACTORY_H_
#include <QObject>
#include "plugin/IRzPluginFactory.h"
class RzPlugin3DViewerFactory :public QObject,public IRzPluginFactory{
Q_OBJECT
Q_INTERFACES(IRzPluginFactory)
private:
QString uid;
public:
RzPlugin3DViewerFactory();
virtual ~RzPlugin3DViewerFactory();
IRzPlugin* createPluginInstance();
IRzPluginContext* createPluginContextInstance();
QString & getPluginUID();
};
#endif /* RZPLUGIN3DVIEWERFACTORY_H_ */
And implementation
#include "RzPlugin3DViewerFactory.h"
#include "RzPlugin3DViewer.h"
RzPlugin3DViewerFactory::RzPlugin3DViewerFactory() {
uid.append("RzPlugin3DView");
}
RzPlugin3DViewerFactory::~RzPlugin3DViewerFactory() {
// TODO Auto-generated destructor stub
}
IRzPlugin* RzPlugin3DViewerFactory::createPluginInstance(){
RzPlugin3DViewer *p=new RzPlugin3DViewer;
return p;
}
IRzPluginContext* RzPlugin3DViewerFactory::createPluginContextInstance()
{
return NULL;
}
QString & RzPlugin3DViewerFactory::getPluginUID()
{
return uid;
}
Q_EXPORT_PLUGIN2(pnp_extrafilters, RzPlugin3DViewerFactory)
Error Message is :
[ 12%] Building CXX object
CMakeFiles/RzDL3DView.dir/RzPlugin3DViewerFactory.cpp
.obj
C:\svn\osaka3d\trunk\osaka3d\rinzo-platform\src\dlplugins\threedviewer\RzPlugin3
DViewerFactory.cpp:36: error: expected
constructor, destructor, or type
conversi on before '(' token make[2]:
*** [CMakeFiles/RzDL3DView.dir/RzPlugin3DViewerFactory.cpp.obj]
Error 1
make[1]: *
[CMakeFiles/RzDL3DView.dir/all] Error
2 make: * [all] Error 2
Ok , I fixed the problem by giving the project name specified in Cmake file.
PROJECT (RinzoDLPlugin3DViewer CXX C)
So,now in CPP file its
Q_EXPORT_PLUGIN2(RinzoDLPlugin3DViewer , RzPlugin3DViewerFactory)
and included qpluginh.h
#include <qplugin.h>
I think the macro should be Q_EXPORT_PLUGIN2(pnp_rzplugin3dviewerfactory, RzPlugin3DViewerFactory) or whatever you have listed as the target name in the .pro file. In fact, the "pnp" part stands for "Plug & Paint" which is the Qt demo program for writing plugins :)
Edit:
Since I misunderstood how CMake works, this information isn't really relevant to the OP. I did do a quick search however and turned up this discussion of Qt, plugins and CMake. I hope there is some useful info there.
http://lists.trolltech.com/qt-interest/2007-05/msg00506.html