I'm having the famous linking error when trying to combine Qt and VTK. I've done all the steps of installing Qt with VTK correctly so I don't know what could possibly be wrong.
I'm using windows 8 with Qt5 and VTK 6.3 on visual studio.
Here is the error I get:
Drawing.obj : error LNK2019: symbole externe non résolu "__declspec(dllimport) public: __thiscall QVTKWidget::QVTKWidget(class QWidget *,class QFlags<enum Qt::WindowType>)" (__imp_??0QVTKWidget##QAE#PAVQWidget##V?$QFlags#W4WindowType#Qt#####Z) référencé dans la fonction "public: __thiscall Drawing::Drawing(void)" (??0Drawing##QAE#XZ)
1>Drawing.obj : error LNK2019: symbole externe non résolu "__declspec(dllimport) public: virtual __thiscall QVTKWidget::~QVTKWidget(void)" (__imp_??1QVTKWidget##UAE#XZ) référencé dans la fonction "public: __thiscall Drawing::~Drawing(void)" (??1Drawing##QAE#XZ)
And here is the code I use:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QVtk w;
w.show();
//start();
return a.exec();
}
I followed everything that was written here: Combining Qt 5.4.1 with vtk 6.2.0 (using CMake GUI 3.2.1) on windows but somehow I get this error.
Also, I've added: $(QTDIR)\lib to my additional libraries $(QTDIR)\include to my "other include directories" and the followings to my additional dependencies.
qtmaind.lib
C:\Users\Lonni\VTK\Build2012Qt\lib\MinSizeRel\QVTKWidgetPlugin.lib
Qt5Cored.lib
Qt5Guid.lib
Qt5Locationd.lib
Qt5Multimediad.lib
Qt5MultimediaWidgetsd.lib
Qt5Testd.lib
Qt5AxContainerd.lib
Qt5AxBased.lib
Qt5OpenGLd.lib
opengl32.lib
glu32.lib
Qt5Sensorsd.lib
Qt5Svgd.lib
Qt5Widgetsd.lib
EDIT:
I did not use CMake for the project as I have issues when using Cmake even with the small example provided on the wiki which gives the following error with the following CMakeLists.txt. NB: I use Visual Studio as a generator.
CMakeLists:
cmake_minimum_required(VERSION 2.8)
if(POLICY CMP0020)
cmake_policy(SET CMP0020 NEW)
endif()
PROJECT(RenderWindowNoUiFile)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
if(${VTK_VERSION} VERSION_GREATER "6" AND VTK_QT_VERSION VERSION_GREATER "4")
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
find_package(Qt5Widgets REQUIRED QUIET)
else()
find_package(Qt4 REQUIRED)
include(${QT_USE_FILE})
endif()
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
file(GLOB UI_FILES *.ui)
file(GLOB QT_WRAP *.h)
file(GLOB CXX_FILES *.cxx)
if(${VTK_VERSION} VERSION_GREATER "6" AND VTK_QT_VERSION VERSION_GREATER "4")
qt5_wrap_ui(UISrcs ${UI_FILES} )
# CMAKE_AUTOMOC in ON so the MocHdrs will be automatically wrapped.
add_executable(RenderWindowNoUiFile MACOSX_BUNDLE
${CXX_FILES} ${UISrcs} ${QT_WRAP})
qt5_use_modules(RenderWindowNoUiFile Core Gui)
target_link_libraries(RenderWindowNoUiFile ${VTK_LIBRARIES})
else()
QT4_WRAP_UI(UISrcs ${UI_FILES})
QT4_WRAP_CPP(MOCSrcs ${QT_WRAP})
add_executable(RenderWindowNoUiFile MACOSX_BUNDLE ${CXX_FILES} ${UISrcs} ${MOCSrcs})
if(VTK_LIBRARIES)
if(${VTK_VERSION} VERSION_LESS "6")
target_link_libraries(RenderWindowNoUiFile ${VTK_LIBRARIES} QVTK)
else()
target_link_libraries(RenderWindowNoUiFile ${VTK_LIBRARIES})
endif()
else()
target_link_libraries(RenderWindowNoUiFile vtkHybrid QVTK vtkViews ${QT_LIBRARIES})
endif()
endif()
Error:
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.2/Modules/FindQt4.cmake:1326 (message):
Found unsuitable Qt version "" from NOTFOUND, this code requires Qt 4.x
Call Stack (most recent call first):
CMakeLists.txt:18 (find_package)
Related
I am getting several undefined reference errors with using Qt. I am using MinGW as my compiler and I am using Qt version 5.15.2.
This is my CMake file:
cmake_minimum_required(VERSION 3.17)
project(jerseytraffic)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(QT_VERSION 5)
set(REQUIRED_LIBS Core Gui Widgets)
set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets)
set(CMAKE_PREFIX_PATH "A:\\CppProjects\\CppLibraries\\Qt5\\5.15.2\\mingw81_64\\lib\\cmake\\")
find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED})
This is main.cpp:
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
qDebug() << "Hello World";
return QCoreApplication::exec();
}
These are the errors I'm getting:
CMakeFiles\jerseytraffic.dir/objects.a(main.cpp.obj): In function `main':
A:/CppProjects/jerseytraffic/main.cpp:5: undefined reference to `_imp___ZN16QCoreApplicationC1ERiPPci'
A:/CppProjects/jerseytraffic/main.cpp:6: undefined reference to `_imp___ZNK14QMessageLogger5debugEv'
A:/CppProjects/jerseytraffic/main.cpp:6: undefined reference to `_imp___ZN6QDebugD1Ev'
A:/CppProjects/jerseytraffic/main.cpp:7: undefined reference to `_imp___ZN16QCoreApplication4execEv'
A:/CppProjects/jerseytraffic/main.cpp:5: undefined reference to `_imp___ZN16QCoreApplicationD1Ev'
A:/CppProjects/jerseytraffic/main.cpp:6: undefined reference to `_imp___ZN6QDebugD1Ev'
A:/CppProjects/jerseytraffic/main.cpp:5: undefined reference to `_imp___ZN16QCoreApplicationD1Ev'
CMakeFiles\jerseytraffic.dir/objects.a(main.cpp.obj): In function `ZN7QString8fromUtf8EPKci':
A:/CppProjects/CppLibraries/Qt5/5.15.2/mingw81_64/include/QtCore/qstring.h:706: undefined reference to `_imp___ZN7QString15fromUtf8_helperEPKci'
CMakeFiles\jerseytraffic.dir/objects.a(main.cpp.obj): In function `ZN6QDebug10maybeSpaceEv':
A:/CppProjects/CppLibraries/Qt5/5.15.2/mingw81_64/include/QtCore/qdebug.h:129: undefined reference to `_imp___ZN11QTextStreamlsEc'
CMakeFiles\jerseytraffic.dir/objects.a(main.cpp.obj): In function `ZN6QDebuglsEPKc':
A:/CppProjects/CppLibraries/Qt5/5.15.2/mingw81_64/include/QtCore/qdebug.h:159: undefined reference to `_imp___ZN11QTextStreamlsERK7QString'
CMakeFiles\jerseytraffic.dir/objects.a(main.cpp.obj): In function `ZN15QTypedArrayDataItE10deallocateEP10QArrayData':
A:/CppProjects/CppLibraries/Qt5/5.15.2/mingw81_64/include/QtCore/qarraydata.h:239: undefined reference to `_imp___ZN10QArrayData10deallocateEPS_jj'
This is what I get in the CMake output when it reloads:
"A:\ProgramFiles\CLion 2020.3.2\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" A:\CppProjects\jerseytraffic
-- Configuring done
-- Generating done
-- Build files have been written to: A:/CppProjects/jerseytraffic/cmake-build-debug
[Finished]
One thing I have tried is modifying the cmake file by adding this around the add_executable line, but it did not make any changes:
find_package(Qt5 COMPONENTS Core REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} Qt5::Core)
Some other things that I have attempted include:
set(CMAKE_BUILD_TYPE "Debug -G \"MinGW Makefiles\"")
set(CMAKE_CXX_COMPILER "C:\\MinGW\\bin\\gcc.exe")
option(G "MinGW Makefiles")
I added this CMake option in Settings > Build, Execution, Deployment > CMake > Debug > CMake options: -G "MinGW Makefiles" %*
However, none of these seemed to fix the error.
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.
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.
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'm new to Qt, and trying to compile and link a simple "Hello, World" program using Microsoft's "CL.exe". Any advice on how to do this?
The program is:
#include <QtGui>
int main ( int argc, char * argv [] )
{
QApplication app ( argc, argv ) ;
QLabel label ( "Hello, world!" ) ;
label.show() ;
return app.exec() ;
}
I compile and link with:
C:\PROGRA~1\MICROS~1.0\VC\bin\cl.EXE -nologo -Zm200 -Zc:wchar_t- -O2 -MD ^
-W3 -w34100 -w34189 -DUNICODE ^
-DWIN32 -DQT_LARGEFILE_SUPPORT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB ^
-DQT_THREAD_SUPPORT -DQT_NO_DYNAMIC_CAST ^
-I"C:\Qt\2010.05\qt\include\QtCore" ^
-I"C:\Qt\2010.05\qt\include\QtGui" ^
-I"C:\Qt\2010.05\qt\include" ^
-I"." ^
-I"C:\Qt\2010.05\qt\include\ActiveQt" ^
-I"release" ^
-I"C:\Qt\2010.05\qt\mkspecs\win32-msvc2008" ^
-I"C:\Progra~1\MICROS~1.0\VC\include" ^
Hello.cpp ^
/link /LIBPATH:"C:\Qt\2010.05\qt\lib" ^
/LIBPATH:"C:\Progra~1\MICROS~1.0\VC\lib" ^
/LIBPATH:"C:\Progra~1\MID05A~1\VC\PLATFO~1\Lib"
My linker errors include:
Hello.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) pub
lic: virtual __thiscall QApplication::~QApplication(void)" (__imp_??1QApplicatio
n##UAE#XZ) referenced in function _main
Hello.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) pub
lic: virtual __thiscall QLabel::~QLabel(void)" (__imp_??1QLabel##UAE#XZ) referen
ced in function _main
Hello.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) pub
lic: static int __cdecl QApplication::exec(void)" (__imp_?exec#QApplication##SAH
XZ) referenced in function _main
Hello.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) pub
lic: void __thiscall QWidget::show(void)" (__imp_?show#QWidget##QAEXXZ) referenc
ed in function _main
.
.
.
My Hello.pro file contains:
######################################################################
# Automatically generated by qmake (2.01a) Mon Dec 13 15:58:13 2010
######################################################################
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# Input
SOURCES += Hello.cpp
You should be linking against QtCore and QtGui libraries but you don't. What does your .pro file look like?
Edited to add after the .pro file was posted: Your .pro file looks all right. You're not overwriting the QT variable which by default includes core gui, which should result in QtCode and QtGui being linked to. It's also evident by the fact that both QtCore and QtGui are present in your include paths.
I haven't used cl.exe with Qt but you could try adding LIBS += -lQtCore -lQtGui to force linking to QtCore and QtGui. (There's probably a more elegant fix though.)
Your PATH should include the qt/bin directory. In a Visual Studio Command Prompt you should do a "qmake hello.pro" which creates three makefiles, and then "nmake". If you want to use vcxproj files, you should change your TEMPLATE to vcapp.
trying to compile and link a simple
"Hello, World" program using
Microsoft's "CL.exe"
Hey i think there are easier ways of getting qt applications compiled on visual studio. Instead of struggling with commandline options why not install the qt visual studio addin that makes compiling projects soooooooo easy? Just a suggestion :)