I want to develop a small Qt Widget application that gets data for plotting from a MySQL database. I have some of the functions that I want to use in a C++ header file which I wrote for an older C++ project. I want to make use of the functions in this header file, but I don't manage to add this header properly to my Qt project.
In Qt Creator I used the "Add existing Files" dialog to add the header file and I also told Qt Creator to use the MySQL_connector C++ Library that is used in my header file. The .pro file looks like this now:
#-------------------------------------------------
#
# Project created by QtCreator 2016-03-31T14:12:08
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = dummy
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
Level2Snapshot.cpp
HEADERS += mainwindow.h \
Level2Snapshot.h \
MySQL_Operations.h
FORMS += mainwindow.ui
unix:!macx: LIBS += -L$$PWD/../../../../usr/lib/ -lmysqlcppconn
INCLUDEPATH += $$PWD/../../../../usr/include
DEPENDPATH += $$PWD/../../../../usr/include
Now, when I want to initialize necessary sql objects in my MainWindow source file, I get "not declared" errors during compilation.
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
sql::Driver *driver;
sql::Connection *con;
}
MainWindow::~MainWindow()
{
delete ui;
}
I could make it work by including my "MySQL_Operations.h", which includes all the MySQL related stuff, into the source file:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "MySQL_Operations.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
sql::Driver *driver;
sql::Connection *con;
}
MainWindow::~MainWindow()
{
delete ui;
}
But that doesnt help me because I need to include the header file in my MainWindow.h because I want to declare methods there, that take the MySQL Connection as an argument. Such like this for example:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "MySQL_Operations.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void print_time_span(sql::Connection *con, std::string database, std::string table);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Doing this, I get "multiple definition" errors for all funtions that are implemented in MySQL_Operations.h. I am aware that similar questions have been asked on the multiple definition issue, but nothing helped me. obviously the compiler is aware of the content of my header file, because otherwise I don't understand how it complains about multiple definitons. But not explicitly adding MySQL_Operations.h to an #include statement but only to the .pro file, how it seems to be done in Qt projects that are created with Qt Creator , isn't working.
I put alot of effort into the MySQL stuff that is in my header file and I really would like to use that, instead of the MySQL tools that Qt provides. Therfore any help is greatly appreciated. I am new to Qt and might not be aware of some essentials about the build process. So don't hesitate to throw even the simplest advice at me.
Thanks alot! Niels
Thanks for the answers so far. I reduced the header to a minimum and it still reproduces the errors. Here is the file:
#ifndef MYSQL_OPERATIONS_H
#define MYSQL_OPERATIONS_H
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <vector>
namespace dom{
void print_time_span(sql::Connection *con, std::string database, std::string table){
sql::Statement *stmt;
sql::ResultSet *res;
std::string use_database = "USE " + database;
stmt = con->createStatement();
stmt->execute(use_database);
res = stmt->executeQuery("SELECT MAX(Time) AS Time FROM test");
res->next();
std::string endtime = res->getString("Time");
res = stmt->executeQuery("SELECT MIN(Time) AS Time FROM test");
res->next();
std::string starttime = res->getString("Time");
std::cout << "First entry in " <<
database <<
"." <<
table <<
" is from " << starttime <<
" and ranges to " <<
endtime << "." << std::endl;
delete res;
delete stmt;
}
}
#endif /* MYSQL_OPERATIONS_H */
It's just a simple query. The errors I get when adding when including the header file in the MainWindow.h are:
/home/niels/Programmierung/dummy/MySQL_Operations.h:28: Fehler: multiple definition of `dom::print_time_span(sql::Connection*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
And I get this error twice.