Using External Lib/DLLs in Qt Creator? - c++

I decided after a bunch of headaches all morning, that using Qt Creator for my first Qt project would probably be better than MSVC (had too many issues compiling).
I am wondering though how I can add the .dlls and .libs I need for my external tools through Qt Creator. I found this post Adding external library into Qt Creator project which makes sense.
I need a little more info though such as...Do I link dlls or libs first, what is the syntax to add dlls to the build step in qmake (i assume its close to win32:LIBS += path/to/Psapi.lib)
Thanks!

Compiling external libraries with QtCreator/gcc
If you own the source code of your libraries this is the .pro file to make an external library (.dll and .a) or Framework (on Mac OS X) from them:
TEMPLATE = lib
INCLUDEPATH = <your-include-paths>
HEADERS += <your-headers>
SOURCES += <your-sources>
TARGET = MyLib /* The name of your libary */
/* Win32: To generate a MyLib.dll and libMyLib.a (gcc) or MyLib.lib (MSVC) file */
win32 {
CONFIG += dll
}
/* Just in case you need to generate Mac Frameworks: */
macx {
CONFIG += shared lib_bundle
FRAMEWORK_HEADERS.version = Versions
FRAMEWORK_HEADERS.files += <your library headers>
/* Example:
FRAMEWORK_HEADERS.files += /path/to/your/lib/MyLib.h
*/
FRAMEWORK_HEADERS.path = Headers
QMAKE_BUNDLE_DATA = FRAMEWORK_HEADERS
VERSION = 0.5.0 // a framework version you can define
}
Adding external libraries to your QtCreator/gcc project
/* your project settings */
/* If you compile on windows */
win32 {
/* If you compile with QtCreator/gcc: */
win32-g++:LIBS += /path/to/your/libMyLib.a
/* IF you compile with MSVC: */
win32-msvc:LIBS += /path/to/your/libMyLib.lib
}
/* If compile on Mac and want to link against a framework */
macx {
LIBS+= -framework MyLib
QMAKE_FLAGS += -F/path/to/MyLib
}
Note that to use external libraries with gcc you need the libMyLib.a file that contains the linking information. The libMyLib.lib are generated by MS Visual Studio and can't be processed by gcc afaik!

Related

Qt vulkan classes not defined during make on windows

I have been trying to compile a Qt/Vulkan project that works fine on Linux, on Windows. I am using QtCreator, Qt (5.11.0), and MingW.
I can qmake without problems, but make crashes with errors for each reference to a QVulkan* class, such as "not defined in this scope", but seems to have found and included the headers for that class.
I have tried compiling the qt vulkan examples, and got the same problem.
I ran configure -v, and found :
Qt Gui:
Vulkan ................................. yes
Here is an example of a header that crashes :
#ifndef WINDU_H
#define WINDU_H
#include <QWindow>
#include <QVulkanInstance>
#include <QVulkanFunctions>
#include <QVulkanDeviceFunctions>
class Windu : public QWindow {
public :
Windu();
~Windu();
void render();
void start();
void reset();
QVulkanInstance inst;
QVulkanFunctions* vki;
QVulkanDeviceFunctions* vkd;
};
#endif
I have stripped it down to the important : this gives not defined errors for QWindow, QVulkanInstance, QVulkanFunctions, QVulkanDeviceFunctions.
I have tried building qtgui separately, but it also crashes because it can't find Qt OpenGLES sources files.
I don't know much about C++ on windows.
This is my .pro file :
SHADERS = $$files(*.comp, true)
SHADERS += $$files(*.frag, true)
SHADERS += $$files(*.vert, true)
spirv.output = ${QMAKE_FILE_NAME}.spv
spirv.commands = glslangValidator -V ${QMAKE_FILE_NAME} -o ${QMAKE_FILE_OUT}
spirv.depends = $$SHADERS
spirv.input = SHADERS
spirv.variable_out = COMPILED_SHADERS
spirv.CONFIG = target_predeps
SOURCES = $$files(*.cpp, true)
HEADERS = $$files(*.h, true)
# install
target.path = build
target.depends = spirv
DESTDIR=bin #Target file directory
OBJECTS_DIR=build #Intermediate object files directory
MOC_DIR=build #Intermediate moc files directory
CONFIG+=debug
QMAKE_EXTRA_COMPILERS += spirv
and here is the repo if needed :
https://github.com/Paul-Hubert/fantastic-octree/blob/master/fantastic-octree.pro
So I found out that on Windows, the only way to get the Vulkan classes is to build Qt yourself. I had done this, but something i had done wrong made the build unuseable. You have to build the sources as described here : http://doc.qt.io/qt-5/build-sources.html, and use that to build the project. I added a kit in QtCreator to do this.

Undefined reference errors linking a dll in QtCreator

I have built a simple dll using g++ under mingw:
We have an include file:
#ifndef __TESTDLL_H
#define __TESTDLL_H
class sineCalculator
{
double n;
public:
sineCalculator();
sineCalculator(double);
double sine();
void setAngle(double);
};
#endif
Then an implementation cpp:
#include <testdll.h>
#include <math.h>
sineCalculator::sineCalculator()
{
n = 0;
}
sineCalculator::sineCalculator(double x)
{
n = x;
}
double sineCalculator::sine()
{
return sin(n);
}
void sineCalculator::setAngle(double x)
{
n = x;
}
This I have compiled as a dll.producing both a .dll file and .a import library with the names visaTest.dll and libvisaTest.a
I can write a small program and link to this dll successfully just using the command line - no Qt just g++. I can create objects and run the methods all fine.
However, I now want to add this library to a Qt application within QtCreator. I have followed the advice of several questions and added these lines to my .pro file:
INCLUDEPATH += C:/msys64/home/hoyla/libs/
INCLUDEPATH +=C:/msys64/home/hoyla/includes/
DEPENDPATH +=C:/msys64/home/hoyla/libs/
win32:CONFIG(release, debug|release): LIBS += -LC:/msys64/home/hoyla/libs/ -lvisaTest
else:win32:CONFIG(debug, debug|release): LIBS += -LC:/msys64/home/hoyla/libs/ -lvisaTest
else:unix: LIBS += -L$$PWD/../../../../libs/ -lvisaTest
Although I use some absolute paths here I have also tried with relative paths. However, I keep getting undefined reference errors to my dll functions when building the Qt file. I would point out that the paths point to the location of my .a import library, the .dll itself is within the system path. What am I doing wrong here?
Mea Culpa! It seems I had compiled my library under 64-bit MinGW but Qt Creator was defaulting to the 32-bit toolchain. One would have thought that the linker might have been able to determine that was the error and generate a more useful error message but there we go. Lesson learned - make sure you're using the tools you think you're using.

How to embed SQLite into a C++ project

I'm trying to embed SQLite into my project. I have included the following files into a directory called lite : sqlite3.dll, sqlite3.h, and sqlite3.lib.
This is my project:
#include <stdio.h>
#include <lite/sqlite3.h>
int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("test.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
}else{
fprintf(stderr, "Opened database successfully\n");
}
sqlite3_close(db);
}
I get the following errors when I run the project:
:-1: error: cannot find -lsqlite3d
collect2.exe:-1: error: error: ld returned 1 exit status
What could I be doing wrong?
I'm working in Qt. This is my .pro file:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lite/ -lsqlite3
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lite/ -lsqlite3d
else:unix: LIBS += -L$$PWD/lite/ -lsqlite3
INCLUDEPATH += $$PWD/lite
DEPENDPATH += $$PWD/lite
Since you are already using Qt why don't you use Qt SQL module? You are going to save a lot of pain, you will bypass this kind of linkage problems just by adding
QT += sql
to you Qt project file, and adding
#include <QtSql>
to you surce files. You'll have a lot of model-view classes that facilitate the integration of the database into your application UI.
This is the recommended way to use SQL in Qt applications, unless you have very very specific needs. You can have Qt use different SQL engines under the hood (SQLite, MySQL,...), but Qt will abstract all this for you.
Despite too late, there are a couple of issues here which I think were completely left out in this question, even though the workaround is practical ..
The error is quite clear .. linker could not find the "debug" version of the lib lsqlite3d which is so configured in qmake .pro file. either get the "debug" file of the lib, or remove this line from .pro
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lite/ -lsqlite3d
This seems a common mistake when adding a library in Qt .. there is an option like :
Add "d" suffix for debug version
And this must be deselected when there is no debug version of the lib.
Second and the essence of this answer, since SQLite is written in C , the include
section is not correct and wont work; It must be corrected to be:
extern "C" {
#include <lite/sqlite3.h>
}
With these two issues corrected .. there should be no problem adding SQLite library, or, in general, any C library to Qt.
Practically with small code like SQLite, another option is to statically compile it with the project .. by just adding sqqlite3.h and sqlite3.c to the Qt project and removing the linkage to sqlite3.lib in .pro .. with the include section as updated.

Porting a Windows compiled QT application to Linux?

I created this simply application in Qt4 and would like to test it on RH Linux distro. The distro has both QtCore "Qt4" and Qt3 installed. I cannot add or delete any of these Qt versions, but would like to work with what's available.
I have an error compiling my windows based Qt program "Qt4" in Linux .
First question :
- How do I compile my compile in Linux without QT creator "only Qt libraries" are installed, what I did is get the .pro file from windows and typed qmake . , the errors are :
WARNING : Found potential symbol conflict of mainwindow.cpp (mainwwindow.cpp) in SOURCES
WARNING : Found potential symbol conflict of mainwindow.h (mainwwindow.cpp) in HEADERS
WARNING : Found potential symbol conflict of dialog.cpp (dialog.cpp) in SOURCES
WARNING : Found potential symbol conflict of dialog.h (dialog.h) in HEADERS
How can I modify qmake to specify the version of Qt
Thank you.
Below is my .pro file
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = TestTool
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
dialog.cpp
HEADERS += mainwindow.h \
dialog.h
FORMS += mainwindow.ui \
dialog.ui
The compilation error is :
Try using the Qt4 version of qmake explicitly:
/usr/lib64/qt4/bin/qmake -o Makefile TestTool.pro

QtCreator LNK2019 error with external library

I have a problem when I want to link a library to my Qt project.
When I try to include an external library (libnodave.lib) in Qt Creator and try to build it, the following error occurs.
main.obj:-1: Fehler: LNK2019: unresolved external symbol __imp_daveSetDebug referenced in function main
I'm pretty sure that I included all needed files in my project and the .pro file. I used the "Add Library" wizard to add the library.
After no success with Qt Creator, I created a minimal example with Visual Studio. When I include all the needed files to the VS project, I can build and run it without errors. So I think that there must be a problem with Qt Creator linking the library. I also tried the Qt-Visual-Studio-Add-in, but there, the same error occurs.
Here are my minimal examples with the library I want to include.
In the Visual Studio example, I added the library path, the include path, and the name of the library to the project properties. It works.
I hope you can help me with my problem.
EDIT:
I want to use the library to get some data from a S7-300 SPS device.
The following code is the minimal example from Qt Creator.
#include <QCoreApplication>
#include <QDebug>
#include <nodave.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
daveInterface *di;
daveSetDebug(daveDebugConnect); // Function of libnodave Library
qDebug() << "Hello World";
return a.exec();
}
This is the whole code from the Visual Studio minimal example.
#include "stdafx.h"
#include <nodave.h>
int _tmain(int argc, _TCHAR* argv[])
{
daveInterface *di;
daveSetDebug(daveDebugConnect);
printf("Hello World\n");
return 0;
}
The code is very small, so I don't think that there is an error inside.
That's why I think it must be a problem with the Qt linker or something like that.
EDIT:
My .pro file.
QT += core
QT -= gui
TARGET = qtminimal
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
win32: LIBS += -L$$PWD/../libnodave-0.8.5/win/ -llibnodave
INCLUDEPATH += $$PWD/../libnodave-0.8.5
DEPENDPATH += $$PWD/../libnodave-0.8.5
The problem was that the Qt project is 64bit and the library I want to include is only 32bit.
So I downloaded the 32bit version of Qt and now it works.
I found the mistake, when I tried to build only the minimal example with libnodave, without any 64bit Qt libraries.
By creating a new Qt project in VS2013, with this workaround and adding the libnodave library afterwards I could change whether it should be a 64bit or 32bit build. By choosing the 32bit build, the Qt library creates errors but not the libnodave lib. When I choose 64bit build, only libnodave creates the errors.
I hope it is useful for someone else.