Qt 4.8 Link error with Visual Studio 2010 - c++

I am receiving a link error when trying to compile my "hello world" Qt program.
Error 1 error LNK2001: unresolved external symbol "__declspec(dllimport) protected: virtual void __thiscall QObject::disconnectNotify(char const *)" (__imp_?disconnectNotify#QObject##MAEXPBD#Z) C:\Users\Matthew\documents\visual studio 2010\Projects\FBIDecryptor\FBIDecryptor\main.obj FBIDecryptor
I believe I am missing a library input, but I don't know which library.
This is the code I am trying to execute:
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt");
label->show();
return app.exec();
}

You are missing reference to QtCore, QtWidget and other Core library modules. Adding the Qt4.8/bin path to the environment variable should fix this.
Found a useful set up guide here

Related

Linker error in simple Qt C++ application

I'm trying to compile some relatively simple code in Qt C++ but I'm getting the following linker error and can't figure out what the issue is.
main.obj:-1: erreur : LNK2019: unresolved external symbol
"class QUuid __cdecl uuid::fromString(class QString const &)"
(?fromString#uuid##YA?AVQUuid##ABVQString###Z) referenced in function _main
Other files compiles fine but somehow these two (uuid.h/uuid.cpp) have this error.
Also if that's relevant, if I right-click on "uuid.cpp" in Qt Editor and select "Compile", I get this error, which also doesn't happen for other cpp files:
Start : "C:\Qt\Tools\QtCreator\bin\jom.exe" -f Makefile.Debug debug/uuid.obj
Error: Target debug/uuid.obj doesn't exist.
Any idea what the issue could be?
uuid.h
#ifndef UUID_H
#define UUID_H
#include <stable.h>
namespace uuid {
QUuid fromString(const QString& s);
}
#endif // UUID_H
uuid.cpp
#include <stable.h>
#include "uuid.h"
namespace uuid {
QUuid fromString(const QString& s) {
// ....
}
}
main.cpp
#include <stable.h>
#include "uuid.h"
int main(int argc, char *argv[]) {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QUuid test = uuid::fromString("f601c97e494a46a7b393bb0366e679a1");
return app.exec();
}
Removing the build folder, and in particular the make files, fixed the issue. Running the Clean command from Qt Creator was not enough.

included header for static library giving dllimport error

I have the code presented later using Xerces-c, which can be built as a static or dynamic library. Failing to include of course results in a compiler error, however when I add #include <xercesc/util/PlatformUtils.hpp> visual studio 2012 gives me a linker errors saying:
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static void __cdecl xercesc_3_1::XMLPlatformUtils::Initialize(char const * const,char const * const,class xercesc_3_1::PanicHandler * const,class xercesc_3_1::MemoryManager * const)" (__imp_?Initialize#XMLPlatformUtils#xercesc_3_1##SAXQBD0QAVPanicHandler#2#QAVMemoryManager#2##Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static void __cdecl xercesc_3_1::XMLPlatformUtils::Terminate(void)" (__imp_?Terminate#XMLPlatformUtils#xercesc_3_1##SAXXZ) referenced in function __catch$_main$0
1>main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static char const * const xercesc_3_1::XMLUni::fgXercescDefaultLocale" (__imp_?fgXercescDefaultLocale#XMLUni#xercesc_3_1##2QBDB)
Based on the dllimport part of the error it seems that it's failing to find a dll. This is confirmed by that when I build Xerces-c as a dynamic library and link to it the error goes away. However if I build Xerces-c as a static library and link to it the same error remains. So my question is why am I getting an error asking for a dll when I'm including and linking to a static library?
using namespace xercesc;
int main(int argc, char* argv[])
{
std::ifstream inputFile(argv[1]);
char c = inputFile.get();
while (inputFile.good()) {
std::cout << c;
c = inputFile.get();
}
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
// Do your failure processing here
return 1;
}
// Do your actual work with Xerces-C++ here.
//XercesDOMParser parser;
//parser.useScanner(XMLUni::fgDGXMLScanner);
XMLPlatformUtils::Terminate();
// Other terminations and cleanup.
return 0;
}
You need to compile your application with XERCES_STATIC_LIBRARY preprocessor macro to disable DLL import/export for Xerces library.
Also check that you link against static version of .lib files.

Qt+VS2010 - Unresolvable external symbol LNK2001

All I have is a main.cpp:
#include "myclass.h"
#include <QApplication>
#include <QTextEdit>
#include <QtGui>
class Notepad : public QWidget {
Q_OBJECT
public:
Notepad();
private slots:
void quit();
private:
QTextEdit *textEdit;
QPushButton *quitButton;
};
Notepad::Notepad()
{
textEdit = new QTextEdit();
quitButton = new QPushButton(tr("Quit"));
connect(quitButton, SIGNAL(clicked()), this, SLOT(quit()));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(textEdit);
layout->addWidget(quitButton);
setLayout(layout);
setWindowTitle(tr("Notepad"));
}
void Notepad::quit()
{
QMessageBox messageBox;
messageBox.setWindowTitle(tr("Notepad"));
messageBox.setText(tr("Do you really want to quit?"));
messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
messageBox.setDefaultButton(QMessageBox::No);
if(messageBox.exec() == QMessageBox::Yes)
qApp->quit();
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
return app.exec();
}
Error info:
1>main.obj : error LNK2001: Unresolvable external symbol "public: virtual struct QMetaObject const * __thiscall Notepad::metaObject(void)const " (?metaObject#Notepad##UBEPBUQMetaObject##XZ)
1>main.obj : error LNK2001: Unresolvable external symbol "public: virtual void * __thiscall Notepad::qt_metacast(char const *)" (?qt_metacast#Notepad##UAEPAXPBD#Z)
1>main.obj : error LNK2001: Unresolvable external symbol "public: virtual int __thiscall Notepad::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#Notepad##UAEHW4Call#QMetaObject##HPAPAX#Z)
1>main.obj : error LNK2001: Unresolvable external symbol "public: static struct QMetaObject const Notepad::staticMetaObject" (?staticMetaObject#Notepad##2UQMetaObject##B)
I'm new to VS and Qt, Hope to find a solution to this, thanks.
The problem is likely that you have Q_OBJECT in your cpp file
class Notepad : public QWidget {
Q_OBJECT
To make it work you need to manually add moc step for your cpp to compilation process. Or move it to the header file, where it's done automatically
You use Q_OBJECT in your Notepad class, so you should moc it, compile the moc output and link with resulting .obj file.
Usually, classes are defined in header files, so you just run moc on the Notepad.h, make it generate moc_Notepad.cpp and compile the latter. In your case you should run moc on your main.cpp, generate something like main.moc and at the bottom of main.cpp add #include "main.moc".
I found my solution for this (FINALLY!) and thought I'd post it here.
I found this page: http://msdn.microsoft.com/en-us/library/aa267384%28v=vs.60%29.aspx
I'll paste the key piece here incase it disappears:
To use this run-time library
Single-threaded (libc.lib)
Multithreaded (libcmt.lib)
Multithreaded using DLL (msvcrt.lib)
Debug Single-threaded (libcd.lib)
Debug Multithreaded (libcmtd.lib)
Debug Multithreaded using DLL (msvcrtd.lib)
What this tells you is the runtile library you need, and which ones you don't.
So when I set release mode, Multi-threaded DLL (/MD), it didn't work for me, the issue was it had in the linker (under Input) the wrong values for the /NODEFAULTLIB, it had 3 entries, one of them being the one I was trying to use the msvcrt.lib. As soon as I changed it to: (leaving all the other values that weren't NODEFAULTLIB items)
/NODEFAULTLIB:libc.lib
/NODEFAULTLIB:libcmt.lib
/NODEFAULTLIB:libcd.lib
/NODEFAULTLIB:libcmtd.lib
/NODEFAULTLIB:msvcrtd.lib
(notice the absence of the one I'm interested it the non debug multithreaded dll lib msvcrt.lib)
It worked 110%!!!
Please like if you find useful, stack wasn't letting me post for a bit, hopefully it will let me post this one.

Linker error with Qt Signal/Slot example

I have a Qt application with multiple classes that use signals and slots and it compiles just fine. However, when I make a custom class inside the main CPP (main.cpp) file, I get a linker error.
Here is the code I use:
class Counter : public QObject
{
Q_OBJECT
public:
Counter() { m_value = 0; }
int value() const { return m_value; }
public slots:
void setValue(int value)
{
if(value!=m_value)
{
m_value = value;
qDebug() << "Value " << value;
emit valueChanged(value);
}
}
signals:
void valueChanged(int newValue);
private:
int m_value;
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
Counter a, b;
QObject::connect(&a, SIGNAL(valueChanged(int)), &b, SLOT(setValue(int)));
a.setValue(12); // a.value() == 12, b.value() == 12
b.setValue(48); // a.value() == 12, b.value() == 48
QTimer::singleShot(0, &app, SLOT(quit()));
return app.exec();
}
Here are the errors:
Error 4 error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Counter::metaObject(void)const " (?metaObject#Counter##UBEPBUQMetaObject##XZ)
Error 5 error LNK2001: unresolved external symbol "public: virtual void * __thiscall Counter::qt_metacast(char const *)" (?qt_metacast#Counter##UAEPAXPBD#Z)
Error 6 error LNK2001: unresolved external symbol "public: virtual int __thiscall Counter::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#Counter##UAEHW4Call#QMetaObject##HPAPAX#Z)
Error 7 error LNK2019: unresolved external symbol "protected: void __thiscall Counter::valueChanged(int)" (?valueChanged#Counter##IAEXH#Z) referenced in function "public: void __thiscall Counter::setValue(int)" (?setValue#Counter##QAEXH#Z)
This linker error does not occur when I place the counter in a separate header file. What's the reason for this behavior?
I'm assuming you're working with qmake.
The moc is made to run on header files automatically by default, because that's where classes are declared in general. Notice that this rule is defined in the makefile, you can manually run moc on a source file.
You have to inform qmake that the file contains a class. To do this, put #include "filename.moc" after the declaration of Counter. You can see more details here (QtCentre) or here (doc).
If you're working with another tool than qmake, say CMake, you have to specify a rule to force the moc to parse the .cpp files (the simplest is to process them all). For files that do not contain a Qt object class, moc will generate an empty file.
However, even if this class is made to be 'private', I advise you to declare it in a header (for example counter_private.h). For example, Qt source is using this trick.
It looks like you have only one code file. If you use the default way to create a Qt project build (qmake && make or QtCreator), the MOC only scans *.h files. If you have all your code in one main.cpp the MOC will not create any code, but that's needed for Signal/Slots to work.
The simplest way to make this specific example working would be adding a line "#include "main.moc"" at the end of your main.cpp. This dependency will be detected by qmake and the needed Makefile targets will be created.
The cutest way would be the clean one: One class - One header and one implementation file.
They moc/uic custom build commands are done on the header file, so it compiles when put in a seperate header/source file and not when put in the same source file

Why does this Simple Qt Application not link

I tried to write a simple Qt application like this:
main.cpp:
#include <QApplication>
class MyApp : public QApplication {
Q_OBJECT
public:
MyApp(int argc, char* argv[]);
};
MyApp::MyApp(int argc, char* argv[]) :
QApplication(argc,argv) {
}
int main(int argc, char* argv[]) {
MyApp app(argc,argv);
return app.exec();
}
But when I tried to compile and link it with Qt Creator 2.3.1 (Qt 4.7.4) I get 3 "unresolved external symbol" errors:
main.obj:-1: error:
LNK2001: unresolved external symbol
""public: virtual struct QMetaObject const * __thiscall MyApp::metaObject(void)const "
(?metaObject#MyApp##UBEPBUQMetaObject##XZ)".
main.obj:-1: error:
LNK2001: unresolved external symbol
""public: virtual void * __thiscall MyApp::qt_metacast(char const*)"
(?qt_metacast#MyApp##UAEPAXPBD#Z)".
main.obj:-1: error:
LNK2001: unresolved external symbol
""public: virtual int __thiscall MyApp::qt_metacall(enum QMetaObject::Call,int,void * *)"
(?qt_metacall#MyApp##UAEHW4Call#QMetaObject##HPAPAX#Z)".
I think they are somehow related to the MetaObjectCompiler of Qt, but I can't figure out a solution.
I know it's not considered good programming style in c++ to put declarations and definitions in one file, but that's not the point here. In my opinion it should be possible since there is nothing syntactically wrong here.
Use the code below, and make sure to run qmake (Build > Run qmake) before building.
#include <QApplication>
class MyApp : public QApplication {
Q_OBJECT
public:
MyApp(int argc, char* argv[]);
};
MyApp::MyApp(int argc, char* argv[]) :
QApplication(argc,argv) {
}
int main(int argc, char* argv[]) {
MyApp app(argc,argv);
return app.exec();
}
#include "main.moc"
Explanation: When you include the Q_OBJECT macro, this signals Qt to do a bunch of stuff that is not standard C++, such as signals and slots. It does this by running moc, which in large part is a code generator. Running qmake creates the metadata so that when your project is built, it knows which files to moc, etc.
I think you need to moc the file and include the resulting main.moc at the bottom.
I think this has something to do with QMake. It's not that the executable app can't see the exported DLL class. It's that the obj file for the class doesn't exist. Running QMake from the QT Creator Build menu and then building seems to work.
Why does this Simple Qt Application not link
I've just met the same problem, and it has been solved by changing the Character set of my header from Unicode to ANSI.