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.
Related
I'm trying to use bit7z in my C++ code to create a program that zips a directory. I'm receiving a LNK2019 error for something called _imp_CharUpperW#4 in my bit7z_d.lib. My IDE is Visual Studio Community 2019 and I use C++ 20.
These are my files:
main.cpp
#include <QCoreApplication>
#include <string>
#include <iostream>
#include <filesystem>
#include <bit7z.hpp>
#include "main.h"
#include <bitcompressor.hpp>
namespace fs = std::filesystem;
using namespace bit7z;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::string path = "C:/Users/aretz/Downloads/test";
for (const auto& entry : fs::directory_iterator(path))
std::cout << entry.path() << std::endl;
return a.exec();
}
void AIXLogger::CompressDir() {
Bit7zLibrary lib{ L"C:/Program Files/7-Zip/7z.dll" };
BitCompressor compressor{ lib, BitFormat::Zip };
vector< wstring > files = { L"C:/Users/aretz/Downloads/test" };
wstring zip = { L"output_archive.zip" };
compressor.compressFiles( files , zip );
}
void AIXLogger::Execute() {
CompressDir();
}
main.h
#pragma once
#include <qwidget.h>
#include <qobject.h>
#include <bit7z.hpp>
class AIXLogger : public QWidget
{
Q_OBJECT
public slots:
public:
void CompressDir();
void Execute();
};
aixLogger.pro
# ----------------------------------------------------
# This file is generated by the Qt Visual Studio Tools.
# ------------------------------------------------------
TEMPLATE = app
TARGET = aixLogger
DESTDIR = ./Debug
CONFIG += debug console
DEPENDPATH += .
MOC_DIR += .
OBJECTS_DIR += debug
UI_DIR += GeneratedFiles
RCC_DIR += GeneratedFiles
include(aixLogger.pri)
Screenshots of my Properties are here:
Additional Include Directories
Additional Library Directories
Additional Dependencies
This is the exact error:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol __imp__CharUpperW#4 referenced in function "wchar_t __cdecl MyCharUpper(wchar_t)" (?MyCharUpper##YA_W_W#Z) aixLogger D:\local\aretz\Programmierung\git-workplace\aixLogger\bit7z_d.lib(MyString.obj) 1
I have a simple program that should retrieve the HTML from a website URL.
main.cpp
#include "Downloader.h"
#include <QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
auto dl = new Downloader(&a);
QString url = "https://www.dognow.at/ergebnisse/?page=1";
dl->fetch(url);
return a.exec();
}
Downloader.h
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QNetworkReply>
#include <QObject>
class Downloader : public QObject
{
Q_OBJECT
public:
explicit Downloader(QObject* parent = nullptr);
void fetch(QString &url);
private:
QNetworkAccessManager* m_manager;
private slots:
void replyFinished(QNetworkReply* rep);
};
#endif // DOWNLOADER_H
Downloader.cpp
#include "Downloader.h"
#include <QDebug>
Downloader::Downloader(QObject* parent): QObject(parent),
m_manager(new QNetworkAccessManager(parent))
{}
void Downloader::fetch(QString& url)
{
qDebug() << "fetch " << url;
connect(m_manager, &QNetworkAccessManager::finished, this, &Downloader::replyFinished);
m_manager->get(QNetworkRequest(QUrl(url)));
}
void Downloader::replyFinished(QNetworkReply* rep)
{
QByteArray data=rep->readAll();
QString str(data);
qDebug() << "data len: " << str.length();
rep->close();
}
When I run the program on my local PC it works fine. When I run it on another machine the reply data is empty. On both systems I use Linux (x86_64) and Qt 5.15.0.
I hope someone can give me a hint where I should have a look at.
UPDATE: 2022-04-04 - 16:22:
when I run a simple curl command on the failing machine it works fine.
Ok, I found the problem.
On the failing machin I have an older ubuntu (16.04 LTS) running with an incompatible openssl version.
I found it out because I copied my own Qt libs build (debug) to the other machine and I got SSL error (incompatbile version).
I installed a newer openssl version and know it works.
When building several 3rd party libraries from source I get undefined symbols build errors or dll import errors.
Im trying to work out why certain functions don't link/can't be linked.
I am using qt creator 7.0.0 qt.6.2.4 online installer linked to LLVM 14.0.0 (13.0.0 has a bug with /MANIFESTDEPENDENCY)
I have followed the below link to build libxml(build with multithreadeddll or MD runtime)
https://forum.unified-automation.com/viewtopic.php?t=26
as well as followed a similar link to build libxslt.
I link to them in the .pro
INCLUDEPATH += ../../../Thirdparty/msvclibxml/include
INCLUDEPATH += ../../../Thirdparty/msvclibxml/include/libxml2
win32:DEPENDPATH += -L../../../Thirdparty/msvclibxml/lib -llibxml2
win32:LIBS += -L../../../Thirdparty/msvclibxml/lib -llibxml2
I have run into similar issues with building BMX when linking to self contained expat library in the code.
I don't experience any issue with my self built libraries, the AWS c++ sdk, FFmpeg so my general method isn't wrong.
Any help would be greatly appreciated.
Upon request here's the use case of lib xml that fails
in the header
//extern "C"
{
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>
#include <libxml/debugXML.h>
#include <libxml/HTMLtree.h>
#include <libxml/xmlIO.h>
#include <libxml/xinclude.h>
#include <libxml/catalog.h>
#include <libxml/parser.h>
#include <libxml/HTMLparser.h>
#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>
}
the called function.
int pdfWriter::generatePDFObject(QString stylesheetFileName, QString xmlFilePath, QString &pdfFileName, QTextDocument &document, QString &formattedString)
{
QFile fileCheck(xmlFilePath);
if (!fileCheck.open(QIODevice::ReadOnly))
{
qDebug() << "Xml file does not exist";
return 2;
}
fileCheck.close();
QFileInfo fileCheckInfo(xmlFilePath);
if (fileCheckInfo.suffix().compare("xml", Qt::CaseInsensitive) != 0)
{
qDebug() << "File does not have xml extension";
return 1;
}
//Put this in eFFData
QString styleSheetFileName;
QString styleSheetCustomerImage;
QString path = "C:/Users/User/hardcodedpathexample";
styleSheetFileName = path;
styleSheetFileName.append("/");
styleSheetFileName.append(stylesheetFileName);
styleSheetCustomerImage = path;
styleSheetCustomerImage.append("/");
styleSheetCustomerImage.append("hardecodedimagesfilenameexample");
// Code to create pdf file
pdfFileName = xmlFilePath;
pdfFileName.remove(".xml");
pdfFileName.append(".pdf");
xmlDocPtr doc, res;
xmlOutputBufferPtr buf = xmlAllocOutputBuffer(NULL);
doc = xmlParseFile(xmlFilePath.toUtf8());
const char *params[1];
params[0] = nullptr;
xsltStylesheetPtr cur = NULL;
xmlSubstituteEntitiesDefault(1);
xmlLoadExtDtdDefaultValue = 1;
cur = xsltParseStylesheetFile((const xmlChar *)styleSheetFileName.toLocal8Bit().data());
res = xsltApplyStylesheet(cur, doc, params);
xsltSaveResultTo(buf, res, cur);
xsltFreeStylesheet(cur);
xmlFreeDoc(res);
xmlFreeDoc(doc);
xsltCleanupGlobals();
xmlCleanupParser();
formattedString = QString::fromUtf8((char*)(xmlOutputBufferGetContent(buf)));
xmlOutputBufferClose(buf);
QPixmap pixmap;
pixmap = QPixmap(":/logo/e-logo.png");
document.addResource(QTextDocument::ImageResource, QUrl("image file name example"), pixmap.scaled(350, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation));
QPixmap customerPixmap(styleSheetCustomerImage);
document.addResource(QTextDocument::ImageResource, QUrl("image file name example"), customerPixmap.scaled(350, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation));
return 0;
}
multiple function fail - mainly xmlAllocOutputBuffer(Null) undefined symbol
I'm new to implementing Threads in QT and even after reading the Documentation several times and watching Videos, I get some Error which not even Google can help me with.
thread.cpp:14: error: C2440: "Initialisierung": "QFuture" kann nicht in "QFuture" konvertiert werden
Error Codes are in German, tried to change QT Language, but didn't change the Language of the Errors. I can translate them if needed.
It seems the Error happens in this QFuture<int> future = QtConcurrent::run(&Thread::GenerateTable); command, even thought I wrote it 1:1 like from the QT Documentation.
Here is the Code I want to put in a Thread, as you can see it's writing a bit bunch of Numbers into a File, which takes around a Minute.
Thread.h
#ifndef THREAD_H
#define THREAD_H
#include <QObject>
#include <QFuture>
#include <QtConcurrent/QtConcurrent>
class Thread : public QObject
{
Q_OBJECT
public:
explicit Thread(QObject *parent = nullptr);
static bool start();
private:
int GenerateTable();
};
#endif // THREAD_H
Thread.cpp
#include "thread.h"
Thread::Thread(QObject *parent) : QObject(parent)
{
}
bool Thread::start()
{
QFuture<int> future = QtConcurrent::run(&Thread::GenerateTable);
if (future.result() == 0){
return true;
}
else
return false;
}
int Thread::GenerateTable(){
QString Path = QDir::currentPath();
QFile file(Path + "/Table.csv");
if (!file.open(QFile::WriteOnly | QFile::Text)){
return -1;
}
else{
QTextStream stream(&file);
constexpr uint64_t upper = 10'000'000;
QVector<uint64_t> rando(upper);
std::iota(rando.begin(), rando.end(), 1);
std::shuffle(rando.begin(), rando.end(),
std::mt19937(std::random_device{}()));
for (uint32_t i = 0; i < 10'000'000; ++i) {
stream << rando[i] << ',' << '\n';
}
return 0;
}
}
Thread::GenerateTable() is a member function. It needs an object to work on. You are calling it (er .. passing it to QtConcurrent::run()) from the (static) Thread::start() and there's no Thread object to speak of.
Although you've tagged Qt6, I'll point at the Qt5 documentation for calling member functions: you can pass an object (pointer) which you'll need to allocate from somewhere.
I'm trying to use cpp-netlib with Visual Studio 2010.
I've built cpp-netlib and add .lib files to my project, but I can't compile them.
--Environment
Windows 7 x64
cpp-netlib 0.11.0
boost 1.55.0
Win32 OpenSSL v1.0.1f
My code is here.
#include <boost/network/protocol/http/client.hpp>
#include <iostream>
int main(int argc, char *argv[]) {
using namespace boost::network;
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " [url]" << std::endl;
return 1;
}
http::client client;
http::client::request request(argv[1]);
request << header("Connection", "close");
http::client::response response = client.get(request);
std::cout << body(response) << std::endl;
return 0;
}
I added the cpp-netlib library path and the cpp-netlib include path to the project.
Boost and openssl paths were also added.
I added the libs to the project.
libboost_system-vc100-mt-gd-1_55.lib
libboost_date_time-vc100-mt-gd-1_55.lib
libboost_regex-vc100-mt-gd-1_55.lib
cppnetlib-client-connections.lib
cppnetlib-uri.lib
I think the errors come from something related OpenSSL.
Error 55 error LNK2019: unresolved external symbol - function _BIO_ctrl ...
Actually, I have Japanese one so it's like below.
エラー 55 error LNK2019: 未解決の外部シンボル _BIO_ctrl が関数 "public: class boost::system::error_code const & __thiscall boost::asio::ssl::detail::engine::map_error_code(class boost::system::error_code &)const " (?map_error_code#engine#detail#ssl#asio#boost##QBEABVerror_code#system#5#AAV675##Z) で参照されました。 cppnetlib-client-connections.lib(client.obj)
エラー 57 error LNK2019: 未解決の外部シンボル _BIO_ctrl_pending が関数 "private: enum boost::asio::ssl::detail::engine::want __thiscall boost::asio::ssl::detail::engine::perform(int (__thiscall boost::asio::ssl::detail::engine::*)(void *,unsigned int),void *,unsigned int,class boost::system::error_code &,unsigned int *)" (?perform#engine#detail#ssl#asio#boost##AAE?AW4want#12345#P812345#AEHPAXI#Z0IAAVerror_code#system#5#PAI#Z) で参照されました。 cppnetlib-client-connections.lib(client.obj)
エラー 43 error LNK2019: 未解決の外部シンボル _BIO_free が関数 "public: __thiscall boost::asio::ssl::detail::engine::~engine(void)" (??1engine#detail#ssl#asio#boost##QAE#XZ) で参照されました。 cppnetlib-client-connections.lib(client.obj)
Could you tell me what I'm missing?
I tried to add more libs to the project, but it still didn't work.
I should've got to add these two library.
libeay32.lib
ssleay32.lib
I ran into the same problem, except i fixed it by using the WIN32 version of SSL instead of the X64 version.