Qt c++ embedded mysql - c++

I've been writing a program on QT 5.0.2 with mingw 4.7 on windows with mysql server 5.7.
I first started with installing the MYQSL driver for QT with this tutorial. It works and I can access the data, but i have to start the server from mysql workbench or it won't make a connection with the database (because it's not running).
For my program I need the server to start up when I run the program. This is the part where i'm stuck. I know I need to use the libmysqld library but I don't seem to get it to work. I didn't find much useful examples so i tried it like this:
this is the .pro file:
QT += core sql
QT += sql
QT -= gui
TARGET = Temp
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += "C:\Program Files (x86)\MySQL\MySQL Server 5.7\include"
INCLUDEPATH += "C:\Program Files (x86)\MySQL\MySQL Server 5.7\lib"
LIBS += -L"C:\Program Files (x86)\MySQL\MySQL Server 5.7\lib" -llibmysqld
this is my main file:
#include <QtSQL>
#include <qsql_mysql.h>
#include <mysql.h>
int main(int argc, char *argv[])
{
QSqlDatabase mydb;
MYSQL *mysql;
static char *server_options[] = \
{ "mysql_test", "--defaults-file=/home/cquiros/temp/mysql/my.cnf", NULL };
int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;
static char *server_groups[] = { "embedded", NULL };
qDebug() << "Loading embedded";
mysql_library_init(num_elements, server_options, server_groups);
mysql = mysql_init(NULL);
mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "embedded");
mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
mysql_real_connect(mysql, NULL,NULL,NULL, "database1", 0,NULL,0);
QMYSQLDriver *drv = new QMYSQLDriver(mysql);
mydb = QSqlDatabase::addDatabase(drv,"connection1"); //Add the database connector to MySQL
mydb.setDatabaseName("test");
if (!mydb.open()) //Try to opens the database
{
qDebug() << "Error while opening the database";
}
}
When i run this, i get this error: "
error: undefined reference to `imp__ZN12QMYSQLDriverC1EP8st_mysqlP7QObject'
collect2.exe:-1: error: error: ld returned 1 exit status
Can anyone help me get on track?

Related

Trying to build a test with Opencv and QT

Recently build OPENCV 3.4.3 with CMAKE 3.12.4 and MINGW64(32bit) 8.1.0 in Windows 7. Have QT 5.6 and i trying to test this build as QT Console App, but when i compiling with QT only get the message "Press "RETURN" to close this window...".
.PRO file:
CONFIG += c++11
TARGET = test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
OTHER_FILES += test.png
INCLUDEPATH += C:\Users\Test\Desktop\opencv\build\include
LIBS += C:\Users\Test\Desktop\mingw\bin\libopencv_*.dll
main.cpp:
#include <QCoreApplication>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"
using namespace std;
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
cout << "Hello World!" << endl;
cv::Mat mat;
mat = cv::imread("test.png");
cvNamedWindow("hello");
cv::imshow("hello",mat);
return a.exec();
}
What im doing wrong?
Imo the problem is not in your code, I just run it (using Qt5.5, Win10, openCV 4 and mingw64), even if I had to change cvNamedWindow to cv::namedWindow.
I have two hints:
did you build openCV with mingw64? If not, do so.
are the openCV dlls in the path when you run the application? You can also copy all opencv dlls in the program folder to check that quickly...

Qt embedding MYSQL

This Question has been asked by different people about 3times by QLands: here and carnifrex: here and there. My problem is exactly like carnifrex's: I am trying to compile my application to connect to MYSQL(Mariadb) database without Mariadb installation i.e embedded server but I get this Error Below:
error: undefined reference to 'imp__ZN12QMYSQLDriverC1EP8st_mysqlP7QObject' collect2.exe:-1: error: error: ld returned 1 exit status
Unfortunately, carnifrex was not replied. Here's my Code
main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include <QSql>
#include <QMessageBox>
#include "qsql_mysql.h"
#include <mysql.h>
bool createConnection(QMYSQLDriver *drver)
{
QSqlDatabase db = QSqlDatabase::addDatabase(drver);
db.setHostName("localhost");
db.setDatabaseName("exama");
db.setPort(3306);
db.setUserName("root");
db.setPassword("Adm1n16");
if (!db.open()) {
QMessageBox::warning(0, QObject::tr("Database Error"),
db.lastError().text());
return false;
}
return true;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MYSQL *mysql;
static char *server_options[] = \
{"mysql_test", "--defaults-file = C:/Program Files (x86)/MariaDB 10.1/data/my.cnf", NULL };
int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;
static char *server_groups[] = { "embedded", NULL };
qDebug() << "Loading embedded";
mysql_library_init(num_elements, server_options, server_groups);
mysql = mysql_init(NULL);
mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "embedded");
mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
mysql_real_connect(mysql, NULL,NULL,NULL, "database1", 0,NULL,0);
QMYSQLDriver *drv = new QMYSQLDriver(mysql);
if (!createConnection(drv))
return 1;
MainWindow w;
w.show();
return a.exec();
}
mysqlConnect.pro:
QT+=core gui sql\
widgets \
greaterThan(QT_MAJOR_VERSION, 5): QT += widgets
INCLUDEPATH += "C:\Qt\Qt5.4\5.4\mingw491_32\include\QtSql\5.4.0\QtSql\private"
INCLUDEPATH += "C:\Program Files (x86)\MariaDB 10.1\include\mysql"
QMAKE_LIBDIR += "C:\Program Files (x86)\MariaDB 10.1\lib"
LIBS += -lmysql
TARGET = mySQLConnect
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
QMAKE_CXXFLAGS+=-std=c++11
The compilation fails at this line in main.cpp:
QMYSQLDriver *drv = new QMYSQLDriver(mysql);
I am using Qt Creator 5.4.0 with MingW 4.9.1 32bit on x64 bit Windows 7 Machine. I will be grateful for any assistance granted.

Qt 5.5.1 MSVC 2013, 32 bit - LNK1181: cannot open input file 'NIDAQmx.lib'

I'm using Qt creator 3.5.1 to try and compile c code from a National Instruments DAQmx example.
To do this I need to include a header file "NIDAQmx.h" and link a library file "NIDAQmx.lib". Both this files are present in the project folder.
When I try to compile with msvc 2013 I get the error:
LNK1181: cannot open input file 'NIDAQmx.lib'
Searching the internet I find this is a common issue and the error code 'LNK1181' means the compiler can't find the library. But all cases presented are very specific and I couldn't find a clue to why this is not working.
Here is my .pro file:
QT += core
QT -= gui
TARGET = Test4NI
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += \
main.cpp
win32: LIBS += -L$$PWD/./ -lNIDAQmx
INCLUDEPATH += $$PWD/.
DEPENDPATH += $$PWD/.
So, the last 3 commands were added by QtCreator itself when I added the library with the wizard by right clicking the project name and selecting 'Add Library...'.
I have also tried to add the path with both 'INCLUDEPATH += "$absolute path" ' and with 'LIB += "$absolute path\NIDAQmx.lib" ', but to no avail.
And below is the example code in the main.c++ file. I took the code from the orignal c example and pasted it on 'main.c++'. I don't believe this to be the source of the issue, as it is an error about library include.
#include <stdio.h>
#include "C:\BK\This\WorkEn\Qt\Test4\Test4NI\NIDAQmx.h"
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
int main(void)
{
int32 error=0;
TaskHandle taskHandle=0;
int32 read;
float64 data[1000];
char errBuff[2048]={'\0'};
/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",10000.0,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,1000));
/*********************************************/
// DAQmx Start Code
/*********************************************/
DAQmxErrChk (DAQmxStartTask(taskHandle));
/*********************************************/
// DAQmx Read Code
/*********************************************/
DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,1000,10.0,DAQmx_Val_GroupByChannel,data,1000,&read,NULL));
printf("Acquired %d points\n",(int)read);
Error:
if( DAQmxFailed(error) )
DAQmxGetExtendedErrorInfo(errBuff,2048);
if( taskHandle!=0 ) {
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
if( DAQmxFailed(error) )
printf("DAQmx Error: %s\n",errBuff);
printf("End of program, press Enter key to quit\n");
getchar();
return 0;
}
So you can see I include the header file with an absolute reference as it wasn't working any other way.
It seems like something silly. I've been at this for the last days.
I hope it's solved soon.
Thank you
So I finally broke through. What follows compiled without errors.
What I did was I created a new console application for 'Desktop Qt 5.5.1 MSVC2013 32bit' kit. Then I placed the header and lib files in the project folder.
I modified the main.cpp that was created on "New Project" to include the header file, and also included the lib file and header in the .pro. In the main.cpp I copied only the code lines that were relevant from the NI example I'm trying to compile.
You can check the diferences between both projects comparing the question above with the working code below. The main routine is the same, but the main function is c++ and a QCoreApplication is executed.
So, here's the .pro file:
QT += core
QT -= gui
TARGET = Test5NI
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
HEADERS += "C:\BK\This\WorkEn\Qt\Test5\Test5NI\NIDAQmx.h"
LIBS += "C:\BK\This\WorkEn\Qt\Test5\Test5NI\NIDAQmx.lib"
SOURCES += main.cpp
And here's the main.cpp:
#include <QCoreApplication>
#include <C:\BK\This\WorkEn\Qt\Test5\Test5NI\NIDAQmx.h>
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
int main(int argc, char *argv[]){
int32 error=0;
TaskHandle taskHandle=0;
int32 read;
float64 data[1000];
char errBuff[2048]={'\0'};
/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",10000.0,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,1000));
/*********************************************/
// DAQmx Start Code
/*********************************************/
DAQmxErrChk (DAQmxStartTask(taskHandle));
/*********************************************/
// DAQmx Read Code
/*********************************************/
DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,1000,10.0,DAQmx_Val_GroupByChannel,data,1000,&read,NULL));
printf("Acquired %d points\n",(int)read);
Error:
if( DAQmxFailed(error) )
DAQmxGetExtendedErrorInfo(errBuff,2048);
if( taskHandle!=0 ) {
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
if( DAQmxFailed(error) )
printf("DAQmx Error: %s\n",errBuff);
printf("End of program, press Enter key to quit\n");
getchar();
QCoreApplication a(argc, argv);
return a.exec();
}
So I guess I didn't have a proper Qt application running by not executing the last two lines of code. This makes sense.

Can't debug cpp code under Qt Creator when linking with CUDA libs

I have a problem with debugging a simple cpp code (it call some CUDA functions like cuInit(), cuDeviceGetCount()..). When I put a break point into the CPP code and start debugging I get this message:
This does not seem to be a "Debug" build.
When I remove all CUDA-calls and do not link the program against the cuda.lib and cudart.lib then the code is debuggable (it is possible to stop the program at the breakpoint and no error message is displayed).
Here is my CPP code:
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <cuda.h>
#include <builtin_types.h>
int main(int argc, char* argv [])
{
QCoreApplication(argc, argv);
int deviceCount = 0;
int cudaDevice = 0;
char cudaDeviceName [100];
cuInit(0);
cuDeviceGetCount(&deviceCount);
cuDeviceGet(&cudaDevice, 0);
cuDeviceGetName(cudaDeviceName, 100, cudaDevice);
qDebug() << "Number of devices: " << deviceCount;
qDebug() << "Device name:" << cudaDeviceName;
}
Here is my .pro file:
QT += core
QT -= gui
TARGET = cudatest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
#################################
# Begin CUDA configuration
win32 {
CUDA_PATH = "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v6.5"
CUDA_INC_DIR = $$CUDA_PATH/include
contains(QMAKE_TARGET.arch, x86_64) {
SYSTEMNAME = x64
SYSTEMTYPE = 64
} else {
SYSTEMNAME = Win32
SYSTEMTYPE = 32
}
CUDA_LIB_DIR = $$CUDA_PATH/lib/$$SYSTEMNAME
QMAKE_CXXFLAGS_RELEASE -= -MD
QMAKE_CXXFLAGS_RELEASE += -MT
QMAKE_CXXFLAGS_DEBUG -= -MDd
QMAKE_CXXFLAGS_DEBUG += -MTd
}
INCLUDEPATH += $$CUDA_INC_DIR
LIBS += -L$$CUDA_LIB_DIR -lcuda -lcudart
#End CUDA configuration
########################
Environment:
Qt Creator 3.2.2
CUDA v6.5
CPP Compiler: VC++ 2013 Express
Debugger: C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x86\cdb.exe
Qt 5.3.2 (compiled by VC++ 2013, 32bit)
I tried to do the same with VC++ 2010 Proffesional with the same result.
Can anyone give me a suggestion where could be the problem?
Thank you.
its probably because you are using Visual Express 2013. It says here that there is no compiler support for VS 2013 Express for CUDA v6.5(Under the Table 2. Windows Compiler Support in CUDA 6.5) You need to install the complete version of Visual Studio.

libjpegTurbo: libjpeg-62 File not recognized

I tried to use libjpeg-turbo with qt.
I downloaded libjpeg-turbo and installed. I wanted to use it within a project but I got the following fault:
C:\libjpeg-turbo-gcc64\bin\libjpeg-62.dll:-1: Error: file not
recognized: File format not recognized
As soon as I removed the libjpeg-62.dll I received the following fault:
C:\test\main.cpp:8: Error: undefined reference to
`tjInitCompress'
Why is the libjpeg-62 not recognizing the file format?
Thanks for help,
Willy
PS. Here is the code:
test.pro
QT += core
QT -= gui
TARGET = test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += C:\libjpeg-turbo-gcc64\include
LIBS += -LC:\libjpeg-turbo-gcc64\bin -llibjpeg-62
main.cpp
#include <QCoreApplication>
#include <turbojpeg.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
tjhandle _jpegCompressor = tjInitCompress();
return a.exec();
}
Ok now it works,
it was the wrong version of libjpeg-turbo. Now I use the libjpeg-turbo-gcc and not the libjpeg-turbo-gcc64. Also i change the Libs-Path to
LIBS += "C://libjpeg-turbo-gcc64//bin//libjpeg-62.dll"
MfG Willy