1

Greeting

I created plugin with Qt (5.6.2) and trying to load it but it returns null all the time. I checked several question and also tried the solutions but it didn't work for me.

Can you take a look of the following code and see whats wrong?

DeviceManager.hpp

#ifndef DEVICE_MANAGER_HPP
#define DEVICE_MANAGER_HPP

#include <QtCore>
#include <string>

using namespace std;

class DeviceManager
{

public:
    virtual ~DeviceManager() {}

    virtual bool initialize() = 0;
    virtual string getBrandName() = 0;

};

QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(DeviceManager, "com.some.address/1.0")
QT_END_NAMESPACE

#endif //DEVICE_MANAGER_HPP

DeviceManagerImpl.hpp

#ifndef DEVICE_MANAGER_IMPL_HPP
#define DEVICE_MANAGER_IMPL_HPP

#include "DeviceManager.hpp"

#include <string>
using namespace std;

class DeviceManagerImpl : public QObject, public DeviceManager
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "com.some.address/1.0")
    Q_INTERFACES(DeviceManager)

public:
    DeviceManagerImpl();

    //Override Method
    bool initialize();          //Have implementation in cpp file
    string getBrandName();      //Have implementation in cpp file

private:
    ...

};

#endif //DEVICE_MANAGER_IMPL_HPP

Pro File

QT       += core gui sql

TARGET = Device-Manager
#TARGET = $$qtLibraryTarget(Device-Manager)
TEMPLATE = lib
CONFIG += plugin


SOURCES += \
    ...

HEADERS += \
    ...

DISTFILES += Device-Manager.json

unix {
    target.path = /usr/lib
    INSTALLS += target
}

And this is how i try to load the plugin in my main process.

QPluginLoader * pluginLoader = new QPluginLoader(pluginPath.c_str());
QObject * plugin = pluginLoader->instance();

if (plugin)
{
    deviceManager = qobject_cast<DeviceMAnager *>(plugin);
    return true;
}
else
{
    delete pluginLoader;
    return false;
}

Im using QT 5.6.2 and QT Creator and MinGW 32bit.

EDIT 1: I found the reason but i have no idea why it cause the problem, Im using library that is linked to project but when i use its function, The instance returns null.

LIBS += $$(STANDARD_XFS_DIRECTORY)/LIB/MSXFS.lib
INCLUDEPATH += $$(STANDARD_XFS_DIRECTORY)/INCLUDE

I also have no compile or linker error. Can anyone tell me what wrong here?

EDIT 2:

The following configs of library and plugin in CMakeList.txt work perfectly fine when i make the project with Visual Studio. (Made with CMake)

ADD_DEFINITIONS(${QT_DEFINITIONS})                                                                                      
ADD_DEFINITIONS(-DUNICODE -D_UNICODE)                                                                                       
ADD_DEFINITIONS(-DQT_PLUGIN)                                                                                
ADD_DEFINITIONS(-DQT_SHARED)                                                                                            
ADD_DEFINITIONS(-DQT_DLL)                                                                                           
ADD_DEFINITIONS(-DQT_LARGEFILE_SUPPORT)
ADD_DEFINITIONS(-DQT_THREAD_SUPPORT)

INCLUDE_DIRECTORIES(
    ${STANDARD_XFS_DIRECTORY}/INCLUDE
)

LINK_DIRECTORIES(
    ${STANDARD_XFS_DIRECTORY}
    ${STANDARD_XFS_DIRECTORY}/LIB
)

SET(XFS_LIBS
    MSXFS
    xfs_conf
    SSIDLL
)

TARGET_LINK_LIBRARIES(Device-Manager 
    ${XFS_LIBS}
)

And this is the configs of library and plugin in .pro file in Qt Creator with MinGW which is not working when i use the library.

QT  -= gui
QT  += core sql

TARGET = Device-Manager
TEMPLATE = lib
CONFIG += plugin
CONFIG += c++11

DEFINES += DEVICEMANAGER_LIBRARY

INCLUDEPATH += $$(STANDARD_XFS_DIRECTORY)
INCLUDEPATH += $$(STANDARD_XFS_DIRECTORY)/INCLUDE
message(Include : $${INCLUDEPATH})

LIBS += $$(STANDARD_XFS_DIRECTORY)/SSIDLL.lib
LIBS += $$(STANDARD_XFS_DIRECTORY)/LIB/MSXFS.lib
LIBS += $$(STANDARD_XFS_DIRECTORY)/LIB/xfs_conf.lib
message(Lib : $${LIBS})

So i can say the code it self is fine and i'm definitely missing some configs in .pro file.

PS1: Plugin and the application that load the plugin, Both have INCLUDEPATH and LIBS of the library in their pro file.

PS2: The application and the plugin both are in debug mode

PS3: I dont get any compile error or linker error during compile.

EDIT 3:

I made simple console application and included my library header and lib file and called the function and this is the error i get when i run the application. So it must be it. But seriously why!!!

The Code:

LPWFSRESULT result = new WFSRESULT();
WFSFreeResult(result);

The errors:

PS: The library is from trusted source and it work perfectly with Visual Studio.

Thanks in advance.

M.H.
  • 223
  • 3
  • 10
  • 23
  • Any output log? What is in `errorString` of plugin loader? – Dmitry Sazonov Dec 11 '17 at 13:57
  • @DmitrySazonov "Cannot load library ...\Device-Manager.dll: The specified module could not be found." – M.H. Dec 11 '17 at 14:25
  • So, you got an answer? – Dmitry Sazonov Dec 11 '17 at 14:27
  • @DmitrySazonov Not really! I searched this error before and i didn't find anything to match my case or i'm missing something!!?? Can you tell me what do you think? – M.H. Dec 11 '17 at 14:40
  • I think that there are no .dll file that you are looking for. Are you sure that you set a correct `pluginPath`? – Dmitry Sazonov Dec 11 '17 at 15:02
  • @Dmitry Sazonov Yes, The path is correct, I'm sure of that. – M.H. Dec 11 '17 at 15:19
  • Ok, now try to use debugger. Do a step-by-step debug with going into `QPluginLoader` constructor and into `instance` method. – Dmitry Sazonov Dec 11 '17 at 15:54
  • @DmitrySazonov Can you check edit 1? Thanks – M.H. Dec 12 '17 at 08:21
  • What should I check? Once again: do a step-by-step debug with going into `QPluginLoader` constructor and into `instance` method. – Dmitry Sazonov Dec 12 '17 at 09:11
  • Is this library linked to the plugin or or the project that loads the plugin? – Felix Dec 12 '17 at 09:58
  • @Felix To both actually. – M.H. Dec 12 '17 at 13:13
  • @Felix I added Edit 2 with more information about the problem. – M.H. Dec 12 '17 at 13:29
  • thats very strange... maybe try this syntax for all your libraries: `LIBS += -L$$(STANDARD_XFS_DIRECTORY)/LIB/ -lMSXFS`. This is the standard way of adding libraries, maybe it has to do with the problem... – Felix Dec 12 '17 at 14:09
  • @Felix I also made the following change on LIBS and still cant fix the problem. win32:LIBS += $$(STANDARD_XFS_DIRECTORY)/SSIDLL.lib win32:LIBS += $$(STANDARD_XFS_DIRECTORY)/LIB/MSXFS.lib win32:LIBS += $$(STANDARD_XFS_DIRECTORY)/LIB/xfs_conf.lib unix:LIBS += -L/$$(STANDARD_XFS_DIRECTORY) -lSSIDLL unix:LIBS += -L/$$(STANDARD_XFS_DIRECTORY)/LIB -lMSXFS unix:LIBS += -L/$$(STANDARD_XFS_DIRECTORY)/LIB -lxfs_conf – M.H. Dec 12 '17 at 14:17
  • @Felix LIBS += -L$$(STANDARD_XFS_DIRECTORY)/LIB/ -lMSXFS didn't fix the problem btw. – M.H. Dec 12 '17 at 14:54

2 Answers2

2

Just reading posts properly can sometimes open ones eyes.

The problem is: You are using minGW with Qt, but are using a lib-file, i.e. a library compiled with the msvc-compiler (visual studio). This is not possible, as they are generally incompatible!

You have 2 options now: Either you install the Qt Kit for whatever version of Visual Studio the library was compiled for, or, if the library is open source, compile it yourself using mingw.

Felix
  • 6,885
  • 1
  • 29
  • 54
0

Well, I found the problem however its so silly. I run project over windows 10 32 bit and it work fine. Maybe something is not incompatible with 64 bit and that caused this mess.

Thanks everyone for all your solution and i hope your solutions help others problem.

Thanks alot!

M.H.
  • 223
  • 3
  • 10
  • 23