简体   繁体   中英

Qt linker errors windows

I'm trying to run this code, but all I get is linker errors and don't really know what to do or what I'm doing wrong here. Been struggling way too long with this now, any help is greatly appreciated so I can get this thing going. This is also my first Qt-application ever.

Running Qt Creator 3.5.1, Based on Qt 5.5.1 (MSVC 2013, 32 bit) Compiler: Microsoft Visual C++ Compiler 12.0 OS: Windows 8.1 Pro 64-bit

I have Qt project where I have files:

Hasher.h

#ifndef HASHER_H
#define HASHER_H

#include <QString>
#include <QCryptographicHash>

class Hasher : public QCryptographicHash
{
public:
    Hasher(const QByteArray &data, Algorithm method); /* Constructor */
    ~Hasher(); /* Destructor */

    QString name, output;
private:
    QCryptographicHash *QCryptObject;
};

#endif // HASHER_H

Hasher.cpp

#include "hasher.h"

/* Destructor */
Hasher::~Hasher() {

}

/*
 * Constructor Hasher(QByteArray &, Algorithm) generates hash
 * from given input with given algorithm-method
*/
Hasher::Hasher(const QByteArray &data, Algorithm method) {
   QByteArray result = this->QCryptObject->hash(data, method);
   this->output = result.toHex();
}

main.cpp

#include <QCoreApplication>
#include <QString>
#include <QFile>
#include <QDebug>

#include "hasher.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString fileName;
    QTextStream stream(stdin);

    qDebug() << "MD5 generator!" << endl;
    qDebug() << "Give filename to generate checksum from: ";
    fileName = stream.readLine();
    QFile* file = new QFile(fileName);
        if(file->open(QIODevice::ReadOnly))
            {
                Hasher hasher(file->readAll(), QCryptographicHash::Md5);
                qDebug() << "MD5 Hash of " << fileName << " is: " << hasher.output << endl;
                file->close();
            }
    return a.exec();
}

Errors I get:

main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Hasher::Hasher(class QByteArray const &,enum QCryptographicHash::Algorithm)" (??0Hasher@@QEAA@AEBVQByteArray@@W4Algorithm@QCryptographicHash@@@Z) referenced in function main

main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Hasher::~Hasher(void)" (??1Hasher@@QEAA@XZ) referenced in function main

debug\MD5-generator.exe:-1: error: LNK1120: 2 unresolved externals

.pro file

QT += core
QT -= gui

TARGET = MD5-generator
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp \
    hasher.cpp

HEADERS += \
    hasher.h

So, the linker error resulted from not updated makefiles and object files, since the new Hasher.cpp was not compled at all. In that case rebuilding project may help: Clean , Run qmake , Build .

In Hasher::Hasher you need to call base class constructor:

Hasher::Hasher(const QByteArray &data, Algorithm method)
    : QCryptographicHash(method)
{
   QByteArray result = this->hash(data, method);
   this->output = result.toHex();
}

I have no idea why MSVC compiles that code at all, it should not even get to linking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM