简体   繁体   中英

Use a Qt made library in Visual C++

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)?

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.

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 .

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