1

I created a shared library with Qt Creator (compiler MSVC2010 32bits) to make some operations with xml files (when I had to select the modules, I checked QtCore, QtXml and QtXmlPatterns).

Here is the code of the lib :

Header :

#ifndef CONVERTERSHARED_H
#define CONVERTERSHARED_H

#include "convertershared_global.h" //created automatically by Qt
#include <QString>

class CONVERTERSHAREDSHARED_EXPORT ConverterShared
{
public:
    ConverterShared();
    bool convert(QString inFileName);
};

#endif // CONVERTERSHARED_H

.cpp

#include "convertershared.h"

#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QXmlStreamWriter>
#include <QXmlStreamReader>
#include <QDomDocument>
#include <QMap>

#include <iostream>
#include <fstream>
using namespace std;

ConverterShared::ConverterShared()
{
}

bool ConverterShared::convert(QString inFileName)
{
//process...
}

So it creates a .lib, and a .dll.

When I use this lib in a Qt project (including paths to includes, libraries in .pro, putting .dll in the exe directory), all is fine, I can create an instance of ConverterShared and use the convert function.

My problem is to use it in a Visual Studio (C++ 2010) project which doesn't use Qt. So in the properties of the project, I added :

  • in C/C++ -> General -> Additional Include Directories -> path/to/convertershared.h (contains convertershared_global.h too)

  • in Libraries -> General -> Additional Dependencies -> ConverterShared.lib (tested with path/ConverterShared.lib and without path)

  • in Libraries ->General -> Additional Libraries Directories -> path/to/lib

Then, in a .cpp file, it finds convertershared.h so I include it but when I compile, I get the error :

Cannot open include file QtCore/qglobal.h: No such file or directory
(and then IntelliSense cannot open source file "QtCore/qglobal.h", "QString")

So how to correct that and use my Qt lib in a Visual project which doesn't use Qt (the PC on which the final Visual project is compiled doesn't necessarly have Qt installed)?

Nejat
  • 31,784
  • 12
  • 106
  • 138
SteveTJS
  • 635
  • 17
  • 32

2 Answers2

3

I think you need define the path to Qt include files, such us qglobal.h etc., which referenced from your convertershared_global.h file. For instance, in VS project's properties page C/C++ -> General -> Additional Include Directories add $(QTDIR)\include\QtCore and other paths, if necessary, to make the compiler find all includes.

vahancho
  • 20,808
  • 3
  • 47
  • 55
  • Indeed, defining the path to Qt solves the error but it still requires Qt installed on the PC. – SteveTJS Jan 22 '15 at 13:32
  • @SteveTJS, You will not be able to run that program without Qt libraries anyways. And I think you need them to link your program too. – vahancho Jan 22 '15 at 13:35
  • So I will rephrase my question : how to deploy a Qt library just like you can deploy Qt application providing only Qt dlls? – SteveTJS Jan 22 '15 at 13:47
  • 1
    @SteveTJS, I don't think there is a defined method. You simply need to identify your project's dependencies and copy only those Qt files (headers, libs, dlls) you need. – vahancho Jan 22 '15 at 13:57
  • Thanks for the hint. In my case I had to add $(QTDIR)\include (instead of $(QTDIR)\include\QtCore) to make it work. – Sumyrda - remember Monica May 31 '17 at 09:34
1

If you link your application with a shared library that uses Qt, you should also link your application with Qt and define the include paths. Even if you link with Qt, your application needs an instance of QCoreApplication and also an event loop to work.

In your case since you don't have a Qt application and want to link to a Qt shared library you can create an instance of the QCoreApplication in a new thread in your shared library and then run the event loop as described here.

If don't want to link your application with Qt, you can load your shared library at runtime and dynamically call it's functions using QLibrary like here.

Community
  • 1
  • 1
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • QLibrary sounds good but it seems that it still requires Qt to be installed on the PC, doesn't it? – SteveTJS Jan 22 '15 at 13:47
  • 1
    Definitely you need Qt libraries. But as i mentioned you need an event loop even if you link to Qt and provide the libraries. – Nejat Jan 22 '15 at 13:50