error: undefined reference to `mysql_init'
-
@Christian-Ehrlicher did not notice link was a link.
would i addunix:LIBS += -L/usr/include/mysql -lmysql
?
and does it matter where in .pro i add it?@micha_eleric said in error: undefined reference to `mysql_init':
unix:LIBS += -L/usr/include/mysql -lmysql
Include folders contain header files not libraries. Libs are in /usr/lib
-
@micha_eleric said in error: undefined reference to `mysql_init':
unix:LIBS += -L/usr/include/mysql -lmysql
Include folders contain header files not libraries. Libs are in /usr/lib
@jsulm would i add
unix:LIBS += -L/usr/lib/mysql -lmysql
?
and does it matter where in .pro i add it? -
@jsulm would i add
unix:LIBS += -L/usr/lib/mysql -lmysql
?
and does it matter where in .pro i add it?@micha_eleric Why don't you simply try?
It does not matter where in the pro file you add it. -
@micha_eleric Why don't you simply try?
It does not matter where in the pro file you add it.@jsulm :-1: error: cannot find -lmysql: No such file or directory
-
@jsulm :-1: error: cannot find -lmysql: No such file or directory
@micha_eleric Then you should pass the correct path where the mysql library resides in.
-
@micha_eleric Why don't you simply try?
It does not matter where in the pro file you add it.@jsulm lol, even tried
unix:LIBS += -L/usr/lib -lmysql
and
unix:LIBS += -L/usr/lib/mysql
of course it did not work, nor did i think it would, but it was a guess out of complete ignorance.
-
@micha_eleric Then you should pass the correct path where the mysql library resides in.
@Christian-Ehrlicher path has plugin, plugins, and private folders.
i know what the header files are called, no clue what the library files are called, so dont know what to look for. -
@Christian-Ehrlicher path has plugin, plugins, and private folders.
i know what the header files are called, no clue what the library files are called, so dont know what to look for.It must be something like libmysql.so
-
It must be something like libmysql.so
@Christian-Ehrlicher closest i find is libmysqlclient.so
-
It must be something like libmysql.so
@Christian-Ehrlicher said in error: undefined reference to `mysql_init':
It must be something like libmysql.so
when i search install libmysql.so, it give links to install libmysqlclient, which i already installed.
-
@Christian-Ehrlicher said in error: undefined reference to `mysql_init':
It must be something like libmysql.so
when i search install libmysql.so, it give links to install libmysqlclient, which i already installed.
@micha_eleric So why don't you try it out?
-
@micha_eleric So why don't you try it out?
Hi,
When you want to use a library as you do, you need to install the corresponding dev package.
-
trying to understand how to use database, search for database gave mysql, search for mysql example gave https://www.codeguru.com/database/database-programming-with-c-c/
finally got mysql libraries installed.
.pro
QT += core gui QT += sql greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 INCLUDEPATH += /usr/include/mysql/ SOURCES += \ CBankAccount.cpp \ CBankTransaction.cpp \ main.cpp \ CMainWindow.cpp HEADERS += \ CBankAccount.h \ CBankTransaction.h \ CMainQindow.h FORMS += \ database.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
CBankTransaction.h
#ifndef CBANKTRANSACTION_H #define CBANKTRANSACTION_H #include "CBankAccount.h" #include <string> class CBankTransaction { private: MYSQL* db_conn; public: CBankTransaction(const std::string HOST, const std::string USER, const std::string PASSWORD, const std::string DATABASE); }; #endif // CBANKTRANSACTION_H
CBankTransaction.cpp
#include "CBankTransaction.h" CBankTransaction::CBankTransaction(const std::string HOST, const std::string USER, const std::string PASSWORD, const std::string DATABASE) { db_conn = mysql_init(NULL); db_conn = mysql_real_connect(db_conn, HOST.c_str(), USER.c_str(), PASSWORD.c_str(), DATABASE.c_str(), 0, NULL, 0); }
error
error: undefined reference to `mysql_init' undefined reference to `mysql_real_connect'
what i have tried:
added "QT += sql", no change.
removed "#include <mysql.h>" from "CBankTransaction.h", because
"CBankAccount.h" already had it, no change.someone said to a similar question, "The error is telling you that you haven't added the MySQL libs to your link-line.", but i have no clue about linker in qt creator. modify pro file, make file is auto generated, linker file? linker options?
from general undefined reference search, function not defined was an option, but mysql is a library, so function should be defined. i can find the .h file, but dont know what file the compiler is linking.
error is in the constructor. am i missing something in my code?
is it possible that library is not properly installed?@micha_eleric
I wonder if you should take a step back and question why you are even trying to do this? You have chosen to pick example code from 2016, and one that has nothing to do with Qt. It uses direct, "low level" libraries to interact with MYSQL. But Qt provides a whole set of "higher level" classes,QSql...
, which are integrated with Qt (e.g. can be used for models for Qt's views) and preferable in a host of ways.Why don't you abandon that example's approach and just use the Qt way? My understanding is that if you are Windows you may have to(?) compile the MySQL libraries, but if you are Linux like I am your distro (e.g. Ubuntu) will offer a pre-compiled set of libraries for MySQL. My system does not have any
libmysql.so
files, the only stuff islibmysqlclient...
. That is the route I would follow. -
@micha_eleric So why don't you try it out?
@Christian-Ehrlicher said in error: undefined reference to `mysql_init':
@micha_eleric So why don't you try it out?
i did. even different ways.
-
Hi,
When you want to use a library as you do, you need to install the corresponding dev package.
@SGaist said in error: undefined reference to `mysql_init':
Hi,
When you want to use a library as you do, you need to install the corresponding dev package.
back tracking installs
[9th] sudo apt install mariadb-client-core-10.6
[8th] sudo apt install mysql-client-core-8.0
[7th] sudo apt-get install pike7.8-mysql
[6th] sudo apt-get install libmariadb-dev
[5th] sudo apt-get install libmariadb-dev-compat
[4th] sudo apt-get install libmariadbclient-dev [i think this is what moved mysql.h to new location, which caused previous .pro to need to be changed]
[3rd] sudo apt-get install libmysqlclient-dev
[2nd] sudo apt-get install mysql-shell
[1st] sudo dpkg-reconfigure libdvd-pkg -
@micha_eleric So why don't you try it out?
@Christian-Ehrlicher
[expected to need lmysql]
unix:LIBS += -L/usr/lib/x86_64-linux-gnu -lmysql
:-1: error: cannot find -lmysql: No such file or directory[maybe llibmysqlclient instead of lmysql]
unix:LIBS += -L/usr/lib/x86_64-linux-gnu -llibmysqlclient
:-1: error: cannot find -llibmysqlclient: No such file or directory[maybe l and lib are the same, as in lmath instead of libmath]
unix:LIBS += -L/usr/lib/x86_64-linux-gnu -libmysqlclient
:-1: error: cannot find -libmysqlclient: No such file or directory[did not expect it to work, but why not try and see?]
unix:LIBS += -L/usr/lib/x86_64-linux-gnu
error: undefined reference tomysql_init' undefined reference to
mysql_real_connect'i will keep guessing
:-1: error: note to link with /usr/lib/x86_64-linux-gnu/libmysqlclient.a use -l:libmysqlclient.a or rename it to liblibmysqlclient.a
does not seem to be what https://doc.qt.io/qt-6/qmake-variable-reference.html#libs, says, but why not try?unix:LIBS += -L/usr/lib/x86_64-linux-gnu -l:libmysqlclient
:-1: error: cannot find -l:libmysqlclient: No such file or directoryunix:LIBS += -L/usr/lib/x86_64-linux-gnu -liblibmysqlclient
:-1: error: cannot find -liblibmysqlclient: No such file or directoryunix:LIBS += -L/usr/lib/x86_64-linux-gnu -l:libmysqlclient.a
298 errors in libunix:LIBS += -L/usr/lib/x86_64-linux-gnu -liblibmysqlclient.a
:-1: error: cannot find -liblibmysqlclient.a: No such file or directoryunix:LIBS += -L/usr/lib/x86_64-linux-gnu -libmysqlclient.a
:-1: error: cannot find -libmysqlclient.a: No such file or directoryunix:LIBS += -L/usr/lib/x86_64-linux-gnu -libmysqlclient.so
:-1: error: cannot find -libmysqlclient.so: No such file or directoryunix:LIBS += -L/usr/lib -libmysqlclient
:-1: error: cannot find -libmysqlclient: No such file or directoryunix:LIBS += -L/usr/lib/ -libmysqlclient
:-1: error: cannot find -libmysqlclient: No such file or directoryunix:LIBS += -L/usr/lib/x86_64-linux-gnu -l;libmysqlclient
[error, but disappears and can not copy]unix:LIBS += -L/usr/lib/x86_64-linux-gnu -l libmysqlclient
:-1: error: cannot find -llibmysqlclient: No such file or directoryjust to see what would happen
unix:LIBS += -L/usr/lib/ -llibmysqlclient
:-1: error: cannot find -llibmysqlclient: No such file or directory
:-1: error: note to link with
/usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libmysqlclient.a use
-l:libmysqlclient.a or rename it to liblibmysqlclient.aunix:LIBS += -L/usr/lib -llibmysqlclient
:-1: error: cannot find -llibmysqlclient: No such file or directory
:-1: error: note to link with
/usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libmysqlclient.a use
-l:libmysqlclient.a or rename it to liblibmysqlclient.aunix:LIBS += -L/usr/ -llibmysqlclient
:-1: error: cannot find -llibmysqlclient: No such file or directory
:-1: error: note to link with
/usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libmysqlclient.a use
-l:libmysqlclient.a or rename it to liblibmysqlclient.aunix:LIBS += -L/ -llibmysqlclient
:-1: error: cannot find -llibmysqlclient: No such file or directory
:-1: error: note to link with
/usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libmysqlclient.a use
-l:libmysqlclient.a or rename it to liblibmysqlclient.aunix:LIBS += -L -llibmysqlclient
error: undefined reference tomysql_init' undefined reference to
mysql_real_connect' -
@micha_eleric
I wonder if you should take a step back and question why you are even trying to do this? You have chosen to pick example code from 2016, and one that has nothing to do with Qt. It uses direct, "low level" libraries to interact with MYSQL. But Qt provides a whole set of "higher level" classes,QSql...
, which are integrated with Qt (e.g. can be used for models for Qt's views) and preferable in a host of ways.Why don't you abandon that example's approach and just use the Qt way? My understanding is that if you are Windows you may have to(?) compile the MySQL libraries, but if you are Linux like I am your distro (e.g. Ubuntu) will offer a pre-compiled set of libraries for MySQL. My system does not have any
libmysql.so
files, the only stuff islibmysqlclient...
. That is the route I would follow.@JonB said in error: undefined reference to `mysql_init':
@micha_eleric
I wonder if you should take a step back and question why you are even trying to do this?first example found, first example try
You have chosen to pick example code from 2016
does it matter if it is from 2016, or 1908, it showed what library is needed, and how to link libraries, which will be needed for future projects.
-
It must be something like libmysql.so
@Christian-Ehrlicher said in error: undefined reference to `mysql_init':
It must be something like libmysql.so
.so files that show up from mysql search
libmysqlclient.so
libmysqlclient.so.21
libmysqlclient.so.21.2.35
mysql_clear_password.so -
folder /usr/lib/x86_64-linux-gnu/ has libmysqlclient.so
error seems to be with how i have
unix:LIBS += -L/usr/lib/x86_64-linux-gnu -llibmysqlclient -
folder /usr/lib/x86_64-linux-gnu/ has libmysqlclient.so
error seems to be with how i have
unix:LIBS += -L/usr/lib/x86_64-linux-gnu -llibmysqlclient@micha_eleric said in error: undefined reference to `mysql_init':
unix:LIBS += -L/usr/lib/x86_64-linux-gnu -llibmysqlclient
This is wrong. If you use -l you skip the lib prefix:
unix:LIBS += -L/usr/lib/x86_64-linux-gnu -lmysqlclient