Link error while compiling qt example on visual studio - c++

I have a problem in compiling a qt example from c++ GUI programming with qt 4 second edition book on visual c++ express 2010.Since qt visual studio add-in doesn't work with express edition , I configured it by myself by just adding library dependencies: qtmaind.lib QtCored4.lib QtGuid4.lib .Also I can compile a sample code 'Hello,Qt!' without error.
My project contains two .cpp files and a header file:
findDialog.h:
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QtGui\qdialog.h>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class findDialog : public QDialog
{
Q_OBJECT
public:
findDialog(QWidget* parent = 0);
signals:
void findNext(const QString &str , Qt::CaseSensitivity cs);
void findPrevious(const QString &str , Qt::CaseSensitivity cs);
private slots:
void findClicked();
void enableFindButton(const QString& text);
private:
QLabel* label;
QLineEdit* lineEdit;
QCheckBox* caseCheckBox;
QCheckBox* backwardCheckBox;
QPushButton* findButton;
QPushButton* closeButton;
};
#endif
findDialog.cpp:
#include <QtGui\QtGui>
#include "findDialog.h"
findDialog::findDialog(QWidget* parent) : QDialog(parent)
{
label = new QLabel(tr("Find &what:"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Match &case"));
backwardCheckBox = new QCheckBox(tr("Search &backward"));
findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);
findButton->setEnabled(false);
closeButton = new QPushButton(tr("Close"));
connect(lineEdit , SIGNAL(textChanged(const QString&)) , this , SLOT(enableFindButton(const QString&)));
connect(findButton , SIGNAL(clicked()) , this , SLOT(findClicked()));
connect(closeButton , SIGNAL(clicked()) , this , SLOT(close()));
QHBoxLayout* topLeftLayout = new QHBoxLayout;
topLeftLayout->addWidget(label);
topLeftLayout->addWidget(lineEdit);
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addLayout(topLeftLayout);
leftLayout->addWidget(caseCheckBox);
leftLayout->addWidget(backwardCheckBox);
QVBoxLayout *rightLayout = new QVBoxLayout;
rightLayout->addWidget(findButton);
rightLayout->addWidget(closeButton);
rightLayout->addStretch();
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
setLayout(mainLayout);
setWindowTitle(tr("Find"));
setFixedHeight(sizeHint().height());
}
void findDialog::findClicked()
{
QString text = lineEdit->text();
Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;
if(backwardCheckBox->isChecked())
emit findPrevious(text , cs);
else
emit findNext(text , cs);
}
void findDialog::enableFindButton(const QString& text)
{
findButton->setEnabled(!text.isEmpty());
}
main.cpp:
#include <QtGui\qapplication.h>
#include <iostream>
#include "findDialog.h"
int main(int argc , char* argv[])
{
QApplication app(argc , argv);
findDialog* dialog = new findDialog;
dialog->show();
return app.exec();
}
When I compile this project , I get 6 link errors :
LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall findDialog::metaObject(void)const " (?metaObject#findDialog##UBEPBUQMetaObject##XZ)
LNK2001: unresolved external symbol "public: virtual void * __thiscall findDialog::qt_metacast(char const *)" (?qt_metacast#findDialog##UAEPAXPBD#Z)
LNK2001: unresolved external symbol "public: virtual int __thiscall findDialog::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#findDialog##UAEHW4Call#QMetaObject##HPAPAX#Z)
LNK2001: unresolved external symbol "public: static struct QMetaObject const findDialog::staticMetaObject" (?staticMetaObject#findDialog##2UQMetaObject##B)
LNK2019: unresolved external symbol "protected: void __thiscall findDialog::findNext(class QString const &,enum Qt::CaseSensitivity)" (?findNext#findDialog##IAEXABVQString##W4CaseSensitivity#Qt###Z) referenced in function "private: void __thiscall findDialog::findClicked(void)" (?findClicked#findDialog##AAEXXZ)
LNK2019: unresolved external symbol "protected: void __thiscall findDialog::findPrevious(class QString const &,enum Qt::CaseSensitivity)" (?findPrevious#findDialog##IAEXABVQString##W4CaseSensitivity#Qt###Z) referenced in function "private: void __thiscall findDialog::findClicked(void)" (?findClicked#findDialog##AAEXXZ)
Thank you in advance and sorry for my bad english.

As you do not have Qt Integration, I checked one of my Qt project for the settings.
On the property page of FindDialog.h, change Item Type from C/C++ header to Custom Build Tool. When press Apply, a new Custom Build Tool Item would appear on the left. Expand it, in General you will find Command Line, enter
"$(QTDIR)\bin\moc.exe" -DUNICODE -DWIN32 -DQT_THREAD_SUPPORT -DQT_NO_DEBUG -DNDEBUG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_DLL "-I.\GeneratedFiles" "-I$(QTDIR)\include" "-I.\GeneratedFiles\$(ConfigurationName)\." "-I." "-I$(QTDIR)\include\QtCore" "-I$(QTDIR)\include\QtGui" ".\FindDialog.h" -o ".\GeneratedFiles\$(ConfigurationName)\moc_$(InputName).cpp"
This is my command line for a release build. You should change it according to your configuration. Remember to added the generated cpp to your project.
Under Custom Build Tool, there's an item called Outputs, which should be
".\GeneratedFiles\$(ConfigurationName)\moc_FindDialog.cpp"
As I may be still missing something, the following is the section for this file in vc's project file:
<File
RelativePath=".\FindDialog.h"
>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCustomBuildTool"
Description="Moc&apos;ing FindDialog.h..."
CommandLine=""$(QTDIR)\bin\moc.exe" -DUNICODE -DWIN32 -DQT_THREAD_SUPPORT -DQT_NO_DEBUG -DNDEBUG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_DLL "-I.\GeneratedFiles" "-I$(QTDIR)\include" "-I.\GeneratedFiles\$(ConfigurationName)\." "-I." "-I$(QTDIR)\include\QtCore" "-I$(QTDIR)\include\QtGui" ".\FindDialog.h" -o ".\GeneratedFiles\$(ConfigurationName)\moc_$(InputName).cpp"
"
AdditionalDependencies=""$(QTDIR)\bin\moc.exe";.\FindDialog.h"
Outputs="".\GeneratedFiles\$(ConfigurationName)\moc_FindDialog.cpp""
/>
</FileConfiguration>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCustomBuildTool"
Description="Moc&apos;ing FindDialog.h..."
CommandLine=""$(QTDIR)\bin\moc.exe" -DUNICODE -DWIN32 -DQT_THREAD_SUPPORT -DQT_CORE_LIB -DQT_GUI_LIB "-I.\GeneratedFiles" "-I$(QTDIR)\include" "-I.\GeneratedFiles\$(ConfigurationName)\." "-I." "-I$(QTDIR)\include\QtCore" "-I$(QTDIR)\include\QtGui" ".\FindDialog.h" -o ".\GeneratedFiles\$(ConfigurationName)\moc_$(InputName).cpp"
"
AdditionalDependencies=""$(QTDIR)\bin\moc.exe";.\FindDialog.h"
Outputs="".\GeneratedFiles\$(ConfigurationName)\moc_FindDialog.cpp""
/>
</FileConfiguration>
</File>
Note this is from a vcproj for VS2008.

Perhaps you can solve your problem with a lot of work based on Kia.celever's diagnosis and further info from
a claim of success
one source (1) is based on
fairly typical posting in QT forum (summary: expect problems)
But all this work won't further your knowledge/skills wrt programming in general, c++, or qt. If Visual Studio is important for you, invest in an unrestricted version for professional use and install the add-in and avoid the hassle of configuring a tool for a use it is not meant for. Otherwise, install QT-Creator (see (2); Qt Creator IDE and tools).

The link errors are coming from Qt's Meta Object Compiler.
Simply when you write Q_OBJECT macro in a class, It defines some member functions in the class which are needed by Qt to handle events....
Qt MOC will create a new .cpp file and write those function's body in it.
Link errors clearly show that specific .cpp file for the FindDilog (named moc_FindDialog.cpp) is missing. Which means the moc file is not generated by Qt.
Maybe Visual Studio has forgot to call Qt Moc to generate .cpp file. and maybe Visual Studio compiler is not compiling it...

Make sure findDialog.h is explicitly included in your .pro file.
If it wasn't, add it and make sure to run qmake again before trying to build your project.

Related

How to resolve LNK2019 error in Visual Studio when using Qt and DCMTK libraries?

I know this question has been asked a lot of times already but I'm sorry, I just can't figure out what is wrong with what I've been doing.
What I want is to read a DICOM image (by pixel because I have to perform operations on it) and display it on a Qt GUI.
I have Windows 8 and Visual Studio 2013. I downloaded Qt opensource and also VS's plugin for it. I built Qt using cmake-gui. Then, I followed what was said on a discussion here in stackoverflow entitled "How to use DCMTK in Qt". I downloaded DCMTK 3.6.0, configured and generated it using cmake-gui, built its ALL_BUILD and INSTALL projects in VS. So far, everything is successful. Then I tried creating a simple program that will read a DICOM image and display it.
I created a new Qt Application and named it MainWindow. Here is my mainwindow.cpp:
#include "mainwindow.h"
#include <dcmtk\config\osconfig.h>
#include <dcmtk\ofstd\ofcond.h>
#include <dcmtk\ofstd\ofstring.h>
#include <dcmtk\dcmdata\dctk.h>
#include <dcmtk\dcmdata\dcfilefo.h>
#include <dcmtk\dcmdata\dcitem.h>
#include <dcmtk\dcmdata\dcdeftag.h>
#include <dcmtk\dcmdata\dctagkey.h>
void MainWindow::tryDCMTK() {
DcmFileFormat fileformat;
OFCondition status = fileformat.loadFile("C:/Users/Kriselle/Documents/000004.dcm");
if (status.good())
{
OFString patientsName;
if (fileformat.getDataset()->findAndGetOFString(DCM_PatientName, patientsName).good())
{
printf("Patient's Name: %s",patientsName);
}
else
printf("Error: cannot access Patient's Name!");
}
else
printf("Error: cannot read DICOM file (%s)", status.text());
}
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
ui.setupUi(this);
}
MainWindow::~MainWindow() {}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtWidgets/QMainWindow>
#include "ui_mainwindow.h"
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindowClass ui;
void tryDCMTK();
};
#endif // MAINWINDOW_H
MainWindow.pro
# ----------------------------------------------------
# This file is generated by the Qt Visual Studio Add-in.
# ------------------------------------------------------
TEMPLATE = app
TARGET = MainWindow
DESTDIR = ../Win32/Debug
QT += core widgets gui
CONFIG += debug console
DEFINES += WIN64 QT_DLL QT_WIDGETS_LIB _REENTRANT
INCLUDEPATH += ./GeneratedFiles \
. \
./GeneratedFiles/Debug \
"C:/Program Files (x86)/DCMTK/include"
DEPENDPATH += .
MOC_DIR += ./GeneratedFiles/debug
OBJECTS_DIR += debug
UI_DIR += ./GeneratedFiles
RCC_DIR += ./GeneratedFiles
HEADERS += ../../../../../../../DICOMSDL/include/dicom.h \
./dicomcfg.h \
./mainwindow.h
SOURCES += ./main.cpp \
./mainwindow.cpp
FORMS += ./mainwindow.ui
RESOURCES += mainwindow.qrc
QMAKE_CFLAGS_RELEASE -= -MD
QMAKE_CFLAGS_RELEASE = -MT
QMAKE_CFLAGS_DEBUG -= -MDd
QMAKE_CFLAGS_DEBUG = -MTd
QMAKE_CXXFLAGS_RELEASE -= -MD
QMAKE_CXXFLAGS_RELEASE += -MT
QMAKE_CXXFLAGS_DEBUG -= -MDd
QMAKE_CXXFLAGS_DEBUG += -MTd
#a example: LIBS += -L"../../../test_dcmtk/DCMTK/lib" \
LIBS += -L"C:/Program Files (x86)/DCMTK/lib" \
-lconfig \
-lofstd \
-ldcmdata \
-loflog \
-lws2_32 \
-lnetapi32 \
-lwsock32 \
-ladvapi32
When I tried to run it, the following 7 LNK2019 errors occurred:
1>mainwindow.obj : error LNK2019: unresolved external symbol "public: __cdecl OFString::OFString(void)" (??0OFString##QEAA#XZ) referenced in function "private: void __cdecl MainWindow::tryDCMTK(void)" (?tryDCMTK#MainWindow##AEAAXXZ)
1>mainwindow.obj : error LNK2019: unresolved external symbol "public: __cdecl OFString::~OFString(void)" (??1OFString##QEAA#XZ) referenced in function "private: void __cdecl MainWindow::tryDCMTK(void)" (?tryDCMTK#MainWindow##AEAAXXZ)
1>mainwindow.obj : error LNK2019: unresolved external symbol "public: class OFCondition __cdecl DcmItem::findAndGetOFString(class DcmTagKey const &,class OFString &,unsigned long,bool)" (?findAndGetOFString#DcmItem##QEAA?AVOFCondition##AEBVDcmTagKey##AEAVOFString##K_N#Z) referenced in function "private: void __cdecl MainWindow::tryDCMTK(void)" (?tryDCMTK#MainWindow##AEAAXXZ)
1>mainwindow.obj : error LNK2019: unresolved external symbol "public: __cdecl DcmFileFormat::DcmFileFormat(void)" (??0DcmFileFormat##QEAA#XZ) referenced in function "private: void __cdecl MainWindow::tryDCMTK(void)" (?tryDCMTK#MainWindow##AEAAXXZ)
1>mainwindow.obj : error LNK2019: unresolved external symbol "public: virtual __cdecl DcmFileFormat::~DcmFileFormat(void)" (??1DcmFileFormat##UEAA#XZ) referenced in function "private: void __cdecl MainWindow::tryDCMTK(void)" (?tryDCMTK#MainWindow##AEAAXXZ)
1>mainwindow.obj : error LNK2019: unresolved external symbol "public: class DcmDataset * __cdecl DcmFileFormat::getDataset(void)" (?getDataset#DcmFileFormat##QEAAPEAVDcmDataset##XZ) referenced in function "private: void __cdecl MainWindow::tryDCMTK(void)" (?tryDCMTK#MainWindow##AEAAXXZ)
1>mainwindow.obj : error LNK2019: unresolved external symbol "public: virtual class OFCondition __cdecl DcmFileFormat::loadFile(char const *,enum E_TransferSyntax,enum E_GrpLenEncoding,unsigned long,enum E_FileReadMode)" (?loadFile#DcmFileFormat##UEAA?AVOFCondition##PEBDW4E_TransferSyntax##W4E_GrpLenEncoding##KW4E_FileReadMode###Z) referenced in function "private: void __cdecl MainWindow::tryDCMTK(void)" (?tryDCMTK#MainWindow##AEAAXXZ)
1>C:\Users\Kriselle\documents\visual studio 2013\Projects\MainWindow\x64\Debug\\MainWindow.exe : fatal error LNK1120: 7 unresolved externals
I have looked into other discussions:
*Link 1** said that the libraries must be linked in properties->linker->additional library dependencies. I already did that. Screenshots of my additional library dependencies and environment variables are available in the zip file attached below.
*Link 2** has a different error output. (I was already past that).
*Link 3** said "look for linker flags/settings inside a project configuration dialog" but I don't know what's wrong with my linker settings. I even listed the libraries inside the lib directory in my Additional Dependencies. (A screenshot of my Additional Dependencies is also included in the zip file below.)
I have also done what was suggested at *Link 4**.
Furthermore, I believe I didn't commit the same mistake as displayed in *Link 5** because I have no parameters for my function.
The images are here: https://db.tt/CmpJndan
The links of online discussions I have looked into are here: https://db.tt/AOsewqUg
As much as I would like to make it easier for you to see the images and navigate to the links, I am very sorry, I can only post a maximum of two links due to my lack of reputation.
Please help me. Thank you very much for your time!
EDIT: I changed the order of my libraries according to their dependencies and added NetAPI32.lib and WSock32.lib to my libraries thanks to the link Hans gave in the comments. But I still get the same errors.

Adding htmlhelp to project

I'm trying, without much luck, to add chm options to an existing project.
Main.cpp has this:
#include "HtmlHelp.h"
int OpenHelp(LPTSTR arg1)
{
HWND _Hhdl = HtmlHelp(GetDesktopWindow(), arg1, HH_DISPLAY_TOPIC, NULL);
return 1;
}
htmlhelp.lib is set in Linker/Input/additional Dependencies.
The error I'm getting is:
main.obj : error LNK2019: unresolved external symbol _HtmlHelpW#16 referenced in function "int __cdecl OpenHelp(wchar_t *)" (?OpenHelp##YAHPA_W#Z)
I'm new to C++ so I'm assuming my function is at fault somehow.
The idea was the function would be passed a sting in the "C:\Help.chm\::/Topic.html" format.

wxWidgets compilation error (fatal error LNK1120: 26 unresolved externals)

I'm trying to build a minimal wxWidgets application with as small a size as is easily possible. (Easily for me, that is).
It's a Hello World GUI program that doesn't do anything else. So, to my knowledge, I only need wxBase and wxCore, which I built using Visual C++ 2008 Express Edition in /MT mode.
My application looks like this:
#include "wx/app.h"
#include "wx/frame.h"
#include "wx/menu.h"
#include "wx/statusbr.h"
#include "wx/msgdlg.h"
class MyApp: public wxApp
{
virtual bool OnInit();
};
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
DECLARE_EVENT_TABLE()
};
enum
{
ID_Quit = 1,
ID_About,
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Quit, MyFrame::OnQuit)
EVT_MENU(ID_About, MyFrame::OnAbout)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame( _("Hello World"), wxPoint(50, 50),
wxSize(450,340) );
frame->Show(true);
SetTopWindow(frame);
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame( NULL, -1, title, pos, size )
{
wxMenu *menuFile = new wxMenu;
menuFile->Append( ID_About, _("&About...") );
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, _("E&xit") );
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, _("&File") );
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText( _("Welcome to wxWidgets!") );
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(TRUE);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox( _("This is a wxWidgets Hello world sample"),
_("About Hello World"),
wxOK | wxICON_INFORMATION, this);
}
Its a near exact copy of the Hello World program in the wxWidgets documentation. I just changed the include files. Replacing them with #include "wx/wx.h" doesn't solve the problem either by the way.
The build errors I get are:
test.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall wxApp::Initialize(int &,wchar_t * *)" (?Initialize#wxApp##UAE_NAAHPAPA_W#Z)
test.obj : error LNK2001: unresolved external symbol "protected: void __thiscall wxStringBase::InitWith(wchar_t const *,unsigned int,unsigned int)" (?InitWith#wxStringBase##IAEXPB_WII#Z)
test.obj : error LNK2001: unresolved external symbol "wchar_t const * const wxEmptyString" (?wxEmptyString##3PB_WB)
test.obj : error LNK2001: unresolved external symbol "wchar_t const * const wxStatusLineNameStr" (?wxStatusLineNameStr##3QB_WB)
test.obj : error LNK2001: unresolved external symbol "wchar_t const * const wxFrameNameStr" (?wxFrameNameStr##3QB_WB)
C:\Users\microsoft\Documents\Visual Studio 2008\Projects\wxAnother\Release\wxAnother.exe : fatal error LNK1120: 5 unresolved externals
I was going to include all the changes I made to the project's properties, but I noticed that all these errors have something to do with wchar_t, which might be enough for somebody to tell me what's causing the error.
What's causing these pesky unresolved external errors and how do I solve the problem (get rid of them)?
Probably you compiled wxWidgets libs as Multi-Threaded DLL and your project is Multi-Threaded. Or (since all errors have string params in description) your wx libs are compiled with Multi-Byte character set and app with Unicode or vise versa.
You also haven't mentioned which wx libs you mentioned in LInker -> Input -> Additional Dependencies.
It sounds like you are trying to outsmart the compiler/linker.
Why?
If there is a genuine reason for you to need a small executable size then wxWidgets is not the way to go. Take a look at FLTK http://fltk.org/

Unresolved externals and some warnings while building the qt solution

I have Qt libraries 4.8.1 for Windows (VS 2010 ultimate) with Qt Visual Studio Add-in. This is my very simple app :
#include<qobject.h>
#include<qstring.h>
#include<memory>
class MyClass : public QObject{
Q_OBJECT
public:
MyClass( const QString &text, QObject *parent = 0 ) : m_text(text) {}
public slots:
void setText( const QString &text );
signals:
void textChanged( const QString& );
private:
QString m_text;
};
void MyClass::setText( const QString &text ){
if( m_text == text ) return;
m_text = text;
emit textChanged( m_text );
}
int main(int argc, char *argv[]){
std::shared_ptr<MyClass> a(new MyClass("foo"));
std::shared_ptr<MyClass> b(new MyClass("bar"));
QObject::connect( a.get(), SIGNAL(textChanged(const QString&)),
b.get(), SLOT(setText(const QString&)) );
a->setText("changed");
}
Errors I get related to unresolved externals : Error 6 error LNK2001:
unresolved external symbol "public: virtual int __thiscall
MyClass::qt_metacall(enum QMetaObject::Call,int,void * *)"
(?qt_metacall#MyClass##UAEHW4Call#QMetaObject##HPAPAX#Z)
Error 4 error LNK2001: unresolved external symbol "public: virtual
struct QMetaObject const * __thiscall MyClass::metaObject(void)const "
(?metaObject#MyClass##UBEPBUQMetaObject##XZ)
Error 5 error LNK2001: unresolved external symbol "public: virtual
void * __thiscall MyClass::qt_metacast(char const *)"
(?qt_metacast#MyClass##UAEPAXPBD#Z)
Error 3 error LNK2019: unresolved external symbol "protected: void
__thiscall MyClass::textChanged(class QString const &)" (?textChanged#MyClass##IAEXABVQString###Z) referenced in function
"public: void __thiscall MyClass::setText(class QString const &)"
(?setText#MyClass##QAEXABVQString###Z)
and two warnings :
Warning 1 warning MSB8017: A circular dependency has been detected
while executing custom build commands for item
"GeneratedFiles\Debug\main.moc". This may cause incremental build to
work incorrectly.
Warning 2 warning : No resources in
'C:\Users\Anonymous\documents\visual studio
2010\Projects\qtWorld\qtWorld\qtworld.qrc'.
I didn't use any qmake / nmake . They ain't required when you get latest Qt Visual Studio Add-in 1.1.11 (even Intellisense recognizes the keywords slots: signals:), right?
Now my questions :
I heard many errors are resolved by just rebuilding the whole solution , why is that ?
Please explain why I get these errors in deep and their possible solutions.
p.s please be good at explanation, don't be rep whorer
If you have QObject class defined in cpp file then you should also include moc for that file.
For example your MyClass is defined in main.cpp then you should add
#include "main.moc"
to end of your main.cpp file.
Thanks to #Tomma for providing the unexplained answer :P
Explanation why above code didn't work :
However, the moc is typically applied to header files only. Since your class definition is part of the main program file, the IDE does not recognize it.
Thanks to #koahnig for providing nice explanation
The constructor of your class is not defined, only declared. Same thing for the textChanged() method. At least that is what I see from the code that you posted. Define them and the error should go away.

C++ : wxWidget HelloWorld

When compiling my wxWidget HelloWorld application, I am getting the following errors:
Warning 1 warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library wxWidget__HelloWorld wxWidget__HelloWorld
Error 2 error LNK2001: unresolved external symbol "public: virtual bool __thiscall wxApp::Initialize(int &,wchar_t * *)" (?Initialize#wxApp##UAE_NAAHPAPA_W#Z) minimal.obj wxWidget__HelloWorld
Error 3 error LNK2001: unresolved external symbol "public: virtual void __thiscall wxAppConsole::OnAssertFailure(wchar_t const *,int,wchar_t const *,wchar_t const *,wchar_t const *)" (?OnAssertFailure#wxAppConsole##UAEXPB_WH000#Z) minimal.obj wxWidget__HelloWorld
Error 4 error LNK2001: unresolved external symbol "public: virtual void __thiscall wxAppConsole::OnAssert(wchar_t const *,int,wchar_t const *,wchar_t const *)" (?OnAssert#wxAppConsole##UAEXPB_WH00#Z) minimal.obj wxWidget__HelloWorld
Error 5 error LNK2019: unresolved external symbol "protected: void __thiscall wxStringBase::InitWith(wchar_t const *,unsigned int,unsigned int)" (?InitWith#wxStringBase##IAEXPB_WII#Z) referenced in function "public: __thiscall wxStringBase::wxStringBase(wchar_t const *)" (??0wxStringBase##QAE#PB_W#Z) minimal.obj wxWidget__HelloWorld
Error 6 error LNK2019: unresolved external symbol "public: int __cdecl wxString::Printf(wchar_t const *,...)" (?Printf#wxString##QAAHPB_WZZ) referenced in function "public: void __thiscall MyFrame::OnAbout(class wxCommandEvent &)" (?OnAbout#MyFrame##QAEXAAVwxCommandEvent###Z) minimal.obj wxWidget__HelloWorld
Error 7 error LNK2001: unresolved external symbol "wchar_t const * const wxEmptyString" (?wxEmptyString##3PB_WB) minimal.obj wxWidget__HelloWorld
Error 8 error LNK2001: unresolved external symbol "wchar_t const * const wxStatusLineNameStr" (?wxStatusLineNameStr##3QB_WB) minimal.obj wxWidget__HelloWorld
Error 9 error LNK2001: unresolved external symbol "wchar_t const * const wxFrameNameStr" (?wxFrameNameStr##3QB_WB) minimal.obj wxWidget__HelloWorld
Error 10 error LNK2019: unresolved external symbol "void __cdecl wxOnAssert(wchar_t const *,int,char const *,wchar_t const *,wchar_t const *)" (?wxOnAssert##YAXPB_WHPBD00#Z) referenced in function "public: __thiscall wxStringBase::wxStringBase(class wxStringBase const &)" (??0wxStringBase##QAE#ABV0##Z) minimal.obj wxWidget__HelloWorld
Error 11 fatal error LNK1120: 9 unresolved externals F:\C++\_2008_\wxWidget__HelloWorld\Debug\wxWidget__HelloWorld.exe wxWidget__HelloWorld
My source code is as follows:
//
Name: minimal.cpp
// Purpose: Minimal wxWidgets sample
// Author: Julian Smart
#include "wx/wx.h"
// Declare the application class
class MyApp : public wxApp
{
public:
// Called on application startup
virtual bool OnInit();
};
// Declare our main frame class
class MyFrame : public wxFrame
{
public:
// Constructor
MyFrame(const wxString& title);
// Event handlers
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
private:
// This class handles events
DECLARE_EVENT_TABLE()
};
// Implements MyApp& GetApp()
DECLARE_APP(MyApp)
// Give wxWidgets the means to create a MyApp object
IMPLEMENT_APP(MyApp)
// Initialize the application
bool MyApp::OnInit()
{
// Create the main application window
MyFrame *frame = new MyFrame(wxT("Minimal wxWidgets App"));
// Show it
frame->Show(true);
// Start the event loop
return true;
}
// Event table for MyFrame
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
END_EVENT_TABLE()
void MyFrame::OnAbout(wxCommandEvent& event)
{
wxString msg;
msg.Printf(wxT("Hello and welcome to %s"),
wxVERSION_STRING);
wxMessageBox(msg, wxT("About Minimal"),
wxOK | wxICON_INFORMATION, this);
}
void MyFrame::OnQuit(wxCommandEvent& event)
{
// Destroy the frame
Close();
}
#include "mondrian.xpm"
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title)
{
// Set the frame icon
SetIcon(wxIcon(mondrian_xpm));
// Create a menu bar
wxMenu *fileMenu = new wxMenu;
// The "About" item should be in the help menu
wxMenu *helpMenu = new wxMenu;
helpMenu->Append(wxID_ABOUT, wxT("&About...\tF1"),
wxT("Show about dialog"));
fileMenu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"),
wxT("Quit this program"));
// Now append the freshly created menu to the menu bar...
wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append(fileMenu, wxT("&File"));
menuBar->Append(helpMenu, wxT("&Help"));
// ... and attach this menu bar to the frame
SetMenuBar(menuBar);
// Create a status bar just for fun
CreateStatusBar(2);
SetStatusText(wxT("Welcome to wxWidgets!"));
}
What is missing?
Make sure you match setting on your project to what all your dependencies are using (actually you should match dependencies :)).
Settings that can cause linking problems with MS toolchain (besides obvious not linking the libraries at all):
Use unicode/multibyte character set
Treat wchar_t as built-in type.
When you know that damn unresolved-wchar_t*-containing-symbol is in the damn library you just linked into, this is probably one of those two.
Runtime (multi/single threaded [debug] [dll]).
This is the reason for your LIBCMTD warning. And for missing/conflicting symbols like __free or malloc or other standard looking things. And for mysterious no reason crashes when crossing dll boundaries, or even on empty place if you somehow manage to link 2 different runtimes into one binary (I've seen it!) .
Suspect preprocessor definitions like _LIB, _DLL, QT_DLL, etc.
Those are used by some libraries to decide if code supposed to be linked statically or dynamically. They usually affect headers that accompany a lib or dll. You have to know if you need them or not. RTFM or look at configs for working example projects for those.
So for your problem, first make sure you add whatever wxWidget libraries you must (and whatever dependencies they need). Search for any of the missing symbols and let google guide you. Somebody would have same problem and would have posted it somewhere before figuring it out themselves.
A good search term is
virtual bool __thiscall wxApp::Initialize
Special care is needed for runtime. When you got all libs you need, but you get libcmt* or msvc* warnings or conflicts then go through all your projects settings and check that 4 items I listed are correct and consistent. You have to know them for dependencies too, if you didn't build them yourself. Use linker verbosity flag too see exactly who brings in unwanted runtime.
Other compiler and linker settings might affect things too, so go with a fine comb through them all.
Most of those changes require clean recompile.
This is the fun of building C++ code.
Looks to me like the error you get when you link you the wrong C Runtime Library. When you build wxWidgets it uses the Multi-threaded DLL option and Multi-threaded Debug DLL options by default for Release and Debug build respectively.
To change this in you app you need to go:
Build->Properties->C/C++->Code Generation and then change the Runtime Library option and rebuild your app.
If you would prefer to statically link against the C Runtime Library so you don't need to DLL you could run a find replace again all of the vcproj files in wxWighets\build\msw and replace
RuntimeLibrary="3" with RuntimeLibrary="1"
and
RuntimeLibrary="2" with RuntimeLibrary="0"
This will change DLL builds as well however and this may not be what you want.