Using Dlls in Qt C++ - c++

I'm trying to build a project with a library (dll) I made. I've never attempted to either load or make a library before and I'm getting the following error.
error: undefined reference to `imp__ZN6NeuronC1Ev'
In Qt, the error is shown in the following line.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow) <--------------------------- Error indicated here.
{
ui->setupUi(this);
}
Project File
QT += core gui
TARGET = Jane
TEMPLATE = app
LIBS += -L quote(C:\Programming\Jane\Jane\Source\Neuron.dll)
SOURCES += main.cpp\
MainWindow.cpp
HEADERS += MainWindow.h
FORMS += MainWindow.ui
Here is one of the classes that I've exported
#ifndef NEURON_H
#define NEURON_H
#include <QList>
#include "Neuron_global.h"
#include <Sensor.h>
class NEURONSHARED_EXPORT Neuron
{
public:
explicit Neuron();
const double getOutput() const;
const double & getWeight() const;
void setWeight(const double& weight);
private:
double weight; // The weight of this neuron.
QList<Neuron*> neurons; // This Neuron's children.
QList<Sensor*> sensors; // This Neuron's Sensors.
};
#endif // NEURON_H
The NEURONSHARED_EXPORT Marco is defined in "Neuron_global.h"
#ifndef NEURON_GLOBAL_H
#define NEURON_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(NEURON_LIBRARY)
# define NEURONSHARED_EXPORT Q_DECL_EXPORT
#else
# define NEURONSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // NEURON_GLOBAL_H
If anyone has any advice on how to fix this, I would greatly appreciate it.
Edit:
I've added the libNeuron.a file to the LIBS argument in pro file. However, I'm now getting the following error.
LIBS += libNeuron.a
cannot find -lNeuron.a
any ideas?

What are you trying to do?
LIBS += -L quote(C:\Programming\Jane\Jane\Source\Neuron.dll)
It variable contains a lib-files, which project will be linked with, not a library itself!
You should find a lib for the dll or use WINAPI LoadLibrary/GetProcAddres functions to load dll dynamically.

This is only a quick guess of mine: your trouble is caused c++ name mangling. Google
"qt dll c++ name mangling"
and find some examples of working dll / client projects.

In this case everything looks right (assuming NEURON_LIBRARY is not defined since you're building under the app template, although Windows v. Linux act differently in this regard).
qmake is known not to pick up all the changes that it ought to so I'd recommend re-running qmake and then your make variant (e.g. make, gmake, nmake):
$ qmake
$ nmake
In some cases, you'll actually need to do a clean (or delete the relevant object files) before everything will be able to link correctly.

Related

The program has unexpectedly finished in QT opencv

I know that this has been posted many times,but I could not find the solution from previous posts. I followed tutorial on How to setup Qt and openCV on Windows from wiki Qt.
My .pro file and mainwindows.cpp are shown below. I wanted to open image following the example. What is wrong here? Checked the opencv version and it is the same as libs included. The PATH is also correct.
The cpp file
#include "ui_mainwindow.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
cv::Mat image = cv::imread("C://1.jpg", 1);
cv::namedWindow("My Image");
cv::imshow("My Image", image);
}
MainWindow::~MainWindow()
{
delete ui;
}
and
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = opencvtest
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += C:\opencv\build\include
LIBS += C:\opencv-build\bin\libopencv_core451.dll
LIBS += C:\opencv-build\bin\libopencv_highgui451.dll
LIBS += C:\opencv-build\bin\libopencv_imgcodecs451.dll
LIBS += C:\opencv-build\bin\libopencv_imgproc451.dll
LIBS += C:\opencv-build\bin\libopencv_features2d451.dll
LIBS += C:\opencv-build\bin\libopencv_calib3d451.dll
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
When program crashes like that under Qt Creator, and you have extra libraries, the very likely reason is that the extra libraries are missing from the runtime PATH.
In this case, you need to add C:\opencv-build\bin to the PATH. There are at least 3 ways to go about it.
Edit the system environment, so that the relevant directory is always in the system PATH. You need to restart Qt Creator for this change to take effect. This is not the recommended way, unless you actually want these things in there also for other purposes.
You can edit the Build environment of the project under Qt Creator Project view. There's separate configuration for each build type, so you may need to do this to them all separately, which both good and bad. It is good, because then you can have different directory for different builds (for example debug vs relase, MSVC vs MinGW builds). It's bad because it's extra hassle and makes it easier to have something wrong.
You can add it to the run environment in the Qt Creator Project view. Then it will be the same for all build types.
In this case, 3 is probably the way to go.
Qt Creator annoyingly does not display any information about which DLL is missing, it just says the program crashed. This can be solved by instead string the "Qt command prompt" for the correct toolchain from Windows Start menu (search Qt and you should find it). Then go to the built .exe directory and run the .exe. You should now get an error dialog where Windows tells you which DLL it failed to find. Then you can look where that DLL is and add it to the path and try again, until the program starts. After you know the directories using this method, you can then add them to Qt Creator as explained above.

Library path is definitely correct and can create an instance of said library, but get "Undefined reference" when calling any function

I'm using Qt Creator to create a new project, and I have a test "MathLibrary" that I created in Visual Studio. I want to use this library in my Qt project.
I have searched for many hours for an answer to my solution and in almost all cases the answer was simply that the library had not been added to the PATH in the .pro file. I'm 99% sure that I have done everything correct, but something is causing me to get an Undefined Reference error when I try calling any function in this library. Here's what I have so far.
The library -
MathLibraryH.h:
#pragma once
namespace MathLibrary
{
class Functions
{
public:
// Returns a + b
double Add(double a, double b);
// Returns a * b
double Multiply(double a, double b);
// Returns a + (a * b)
double AddMultiply(double a, double b);
};
}
MathLibrary.cpp:
#include "stdafx.h"
#include "MathLibraryH.h"
namespace MathLibrary
{
double Functions::Add(double a, double b)
{
return a + b;
}
double Functions::Multiply(double a, double b)
{
return a * b;
}
double Functions::AddMultiply(double a, double b)
{
return a + (a * b);
}
}
The QT project -
TestQTProject.pro:
QT += core gui \
network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = TestQTProject
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp \
HEADERS += \
mainwindow.h \
FORMS += \
mainwindow.ui
DISTFILES += \
com_github_msorvig_s3.pri
LIBS += -L$$PWD/../Libs -lMathLibrary
INCLUDEPATH += $$PWD/../Incs
mainwindow.cpp:
#include "MathLibraryH.h"
// .... other stuff ....
void MainWindow::on_btnStage1_clicked()
{
MathLibrary::Functions lib; // This is just fine
lib.Add(5, 9); // The "Add" function (or any other function in the library)
causes an undefined reference error
}
I'm still new to Qt but I can't see what's wrong with any of this code.
Other things I've tried based on answers from searching:
Adding the following code to MathLibrary.h:
#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif
Changing the format of the LIBS declaration in the .pro file to all of the following:
Multi-line:
LIBS += -L$$PWD/../Libs
LIBS += -lMathLibrary
Hard coded single line:
LIBS += -LC:\svn\software\WIP\TestQTProject\Libs -lMathsLibrary
Nothing I've done works and I have no other ideas left.
For what it's worth, the library works fine in any project created using visual studio, and I've tried creating both a static and dynamic library.
Add
MATHLIBRARY_API
infront of your class name to export all public members in your dll. During export the dllexport part should be set and during import the dllimport part of course.
Here's a link that explains how to export classes from a dll:
https://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL
If you were trying to link dynamically, the problem is that your library MathsLibrary was not exported as a dynamic library, and neither you have imported it as a dynamic library (that's what __declspec(dllexport) does, and the macros should be used at the front of functions or classes declarations to make any difference). And even if you did, there is a chance that you have compiled it differently from your Qt application, so there is a linking issue.
But since you are trying to link statically (I didn't see any library file loading at your code), the problem seems to be the diff between the compilation of your library and your Qt application.
You should try to build your Qt Application with the same flags and same compiler that you built your library, or vice versa. If your code is working as you say, it maybe works.
For further details:
DL Library
How to Create a Plugin Framework
Building plugins using Qt Framework

How to get the CImg library working for Qt

I'm currently learning how to use Qt. I want to try out some simple image processing applications using Qt, and since I'm already familiar with CImg I want to use that. I guess it should be possible to do so, if not mark my question for deletion or something.
My question is: how to get CImg working for Qt? CImg is a header file. Lets say its located on my desktop. I import it using Qt creator 4.1.0, by using the "add existing file..." in the rightclick menu on the header folder. Then my menu looks like this:
.
It compiles when I add #include "CImg.h", but I can't use it, even when I'm trying to type using namespace cimg_library it will tell me that cimg_library doesn't exist. I also tried just creating a header file and copying the content of the CImg.h into it but then it simply fails to compile and the Qt Creator freezes.
Edit: I managed to make the situation a bit better by adding the header location to the include code (like this: #include "C:/Users/Marci/Desktop/CImg.h" )I can now "see" CImg related stuff in the dev environment, and it won't bother me with not finding the constructor for CImg or anything like that. However when I try to compile while using anything CImg related it will give me around 20 linker errors. (Error code: LNK2019) My .pro file looks like this:
#-------------------------------------------------
#
# Project created by QtCreator 2016-11-08T17:08:58
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = grayscale
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h \
C:/Users/Marci/Desktop/CImg.h
LIBS += -C:/Users/Marci/Desktop/ -CImg.h
FORMS += mainwindow.ui
Edit2: after implementing the changes that PeterT suggested in his comment my .pro file looks like this:
#-------------------------------------------------
#
# Project created by QtCreator 2016-11-08T17:08:58
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = grayscale
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h \
INCLUDEPATH += C:/Users/Marci/Desktop
FORMS += mainwindow.ui
And my mainwindow.cpp (in which i'm trying to create a CImg object) looks like this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <CImg.h>
using namespace cimg_library;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
CImg<unsigned char> myimage(100,200);
}
MainWindow::~MainWindow()
{
delete ui;
}
The compiler errors i get are: error: C2871: 'cimg_library': a namespace with this name does not exist
error: C2065: 'CImg': undeclared identifier
error: C2062: type 'unsigned char' unexpected
I hope this is specific enough.
After I few months I figured it out. The problem lies in the fact, that CImg uses a windows .dll file for the visualizing functions of the class cimg_display. Since Qt is platform independent it doesnt like this. However you can make it work with an interesting trick. First you need to include the header file normally, in the project file. After that, whenever you actually #include it, you need to write the following macro:
#define cimg_display 0
In my understanding this makes it work, because the C and C++ compilers simply copy the content of the included file into the source. And thanks to the macro the compiler will ignore the class thats causing trouble for us.

Qt subdirs incude classes

I'm follow Qt Echo Plugin example and trying to write complex application. My project have following structure:
MainDir \
Main.pro
kernel \
kernel.pro
abstractinterface.h
main.cpp
testplugin \
testplugin.pro
abstractplugin.h
abstractplugin.cpp
Problem is in plugin header file:
#include <QObject>
#include <QtPlugin>
#include "abstractinterface.h"
class AbstractPlugin : public QObject, AbstractInterface
// An error appears here
// expected class-name before '{' token
{
Q_OBJECT
//... plugin initialization code ...
public:
explicit AbstractPlugin(QObject *parent = 0);
};
Also, autocompletion can't find class AbstractInterface.
So, question is: what I'm doing wrong? In testplugin.pro file I have line INCLUDEPATH += ../kernel/.
Any help appreciated.
---- EDIT -----
abstractinterface.h
#include <QtPlugin>
#define INTERFACE_ID "AbstractInterface/1.0"
class AbstractInterface
{
public:
virtual ~AbstractInterface();
virtual void init();
virtual void enable();
virtual void disable();
};
Q_DECLARE_INTERFACE(AbstractInterface, INTERFACE_ID)
Given that your pasted files look correct and work here, I am leaning towards that your problem is this line:
INCLUDEPATH += ../kernel/
You likely execute qmake from the project root where your main project file resides calling qmake recursively to generate the Makefiles. However, at the point of generation, the aforementioned path will extend from the project root rather than from the sub-directory. Please fix your testplugin.pro project file by using this instead:
INCLUDEPATH += $$PWD/../kernel/
However, what is even better design is to not handle it inside that project file, but the other kernel.pro where the header files reside. It is more flexible design to add this in there:
INCLUDEPATH += $$PWD
Edit: Based on your comment which was not in the original question, it seems that you have another problem. You seem to have messed with the include guards called the same in two different files and that is why the second inclusion did not result in providing the accessibility for you.

SYSTEMTIME in qt

I am a bit stuck at the moment with a little sample project that I would like to run to test some cryptology that I want to use in a main project.
Basically I am using the latest Qt Creator and I have created a simple window dialog. Furthermore, I would like to test the PBKDF2 implementation through CkCrypt2
So what I have done is downloading the X64 version of the library and added it to my project folder. I then told my Qt project to use an external library, the final .pro file looks like this:
#-------------------------------------------------
#
# Project created by QtCreator 2013-06-09T18:09:44
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = crypt2test
TEMPLATE = app
SOURCES += main.cpp\
m
ainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/libs/ -lChilkatDbgDll_x64
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/libs/ -lChilkatDbgDll_x64d
else:unix: LIBS += -L$$PWD/libs/ -lChilkatDbgDll_x64
INCLUDEPATH += $$PWD/include
DEPENDPATH += $$PWD/include
I can successfully load the library but I cannot start the application.
My mainwindow.cpp looks like this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "CkCrypt2.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
CkCrypt2 crypt;
bool success;
success = crypt.UnlockComponent("Just some random text ");
if ( !success )
{
qDebug() << "Not successfully unlocking the library";
}
}
MainWindow::~MainWindow()
{
delete ui;
}
The error message I get in the compiler is:
c:\qt\qt5.0.2\tools\qtcreator\bin\crypt2test\include\CkString.h:127: error: C2061: syntax error : identifier 'SYSTEMTIME'
c:\qt\qt5.0.2\tools\qtcreator\bin\crypt2test\include\CkString.h:129: error: C2061: syntax error : identifier 'SYSTEMTIME'
C:\Qt\Qt5.0.2\Tools\QtCreator\bin\crypt2test\include\CkCrypt2.h:429: error: C2061: syntax error : identifier 'SYSTEMTIME'
Looking into the files I see:
void appendDateRfc822(SYSTEMTIME &sysTime);
void appendDateRfc822Gmt(SYSTEMTIME &sysTime);
// GETSIGNATURESIGNINGTIME_BEGIN
bool GetSignatureSigningTime(int index, SYSTEMTIME &outSysTime);
// GETSIGNATURESIGNINGTIME_END
Okay, so it is complaining about the SYSTEMTIME construct. So I look up the error C2061
Basically it says:
The compiler found an identifier where it wasn't expected. Make sure
that identifier is declared before you use it.
Which makes sense, so I look up the SYSTEMTIME and try to do :
#include <windows.h>
But that leads to many more errors in the windows.h file itself.
I use the MS Visual C++ compiler in Qt. Even though I use qmake. I am very new to this and I do not understand it all yet. Furthermore, I have no idea how to fix this, because including the windows.h does not help.
What seems to be the problem here ? Is this an issue regarding my compiler or the constulation that I use a third party app which library is compiled with MS Visual C++ and I am now trying to use this on my Windows machine in Qt with a Windows Visual C++ compiler ?
For any help I am gratefully thankful!
EDIT1:
Actually, after a clean all and qmake and build project I have now different errors and none were found in the Windows.h as previously stated. Since there were so many I have made a screenshot: http://i.imgur.com/B8EoENB.png
EDIT2:
I have adjusted the library that I include. Before this I was using the multi-threaded library of CkCrypt in the Debug mode. I have now included the single realease library. Which is located in the same directory.
When including windows.h I got the errors that things were already defined. So I removed the line again. With this result: http://i.imgur.com/z415txR.png
This shows at the bottom that MSVCRT conflichts with other library. It mentions to use NODEFAULTLIB:library but I am not to sure how to do that. Will google and keep this up to date as I process.
Many years ago, Chilkat was originally developed for the Windows platform only, and used SYSTEMTIME for this reason. In the last 5 years (approx) Chilkat is cross-platform, and SYSTEMTIME no longer makes sense. To cope with the issue, there is a "SystemTime.h" header in the same directory as the CkCrypt2.h header file. You could include this to solve the problem. (However, if WIN32 is defined, you'll probably need to edit SystemTime.h to remove the #ifdef.)
In any case, the methods using SYSTEMTIME are going to be deprecated. For any method or property that uses SYSTEMTIME, there should be a newer alternative method/property that instead uses CkDateTime.
Finally, Chilkat will test with Qt so that for the next version, (hopefully) Qt out-of-the-box will compile without any pitfalls.