Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. error: undefined reference to `mysql_init'
Forum Updated to NodeBB v4.3 + New Features

error: undefined reference to `mysql_init'

Scheduled Pinned Locked Moved Solved General and Desktop
30 Posts 5 Posters 3.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    micha_eleric
    wrote on last edited by
    #1

    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?

    Christian EhrlicherC JonBJ 2 Replies Last reply
    0
    • M micha_eleric

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #25

      @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
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      1
      • M micha_eleric

        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?

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        You forgot to link against the mysql libs.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        M 1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          You forgot to link against the mysql libs.

          M Offline
          M Offline
          micha_eleric
          wrote on last edited by
          #3

          @Christian-Ehrlicher said in error: undefined reference to `mysql_init':

          You forgot to link against the mysql libs.

          that was my guess, but no clue how to link with qt creator.

          Christian EhrlicherC 1 Reply Last reply
          0
          • M micha_eleric

            @Christian-Ehrlicher said in error: undefined reference to `mysql_init':

            You forgot to link against the mysql libs.

            that was my guess, but no clue how to link with qt creator.

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @micha_eleric you already have a pro file so click on my link I provided and add the necessary line.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            M 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @micha_eleric you already have a pro file so click on my link I provided and add the necessary line.

              M Offline
              M Offline
              micha_eleric
              wrote on last edited by
              #5

              @Christian-Ehrlicher did not notice link was a link.
              would i add

              unix:LIBS += -L/usr/include/mysql -lmysql
              

              ?
              and does it matter where in .pro i add it?

              jsulmJ 1 Reply Last reply
              0
              • M micha_eleric

                @Christian-Ehrlicher did not notice link was a link.
                would i add

                unix:LIBS += -L/usr/include/mysql -lmysql
                

                ?
                and does it matter where in .pro i add it?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @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

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                M 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @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

                  M Offline
                  M Offline
                  micha_eleric
                  wrote on last edited by
                  #7

                  @jsulm would i add

                  unix:LIBS += -L/usr/lib/mysql -lmysql
                  

                  ?
                  and does it matter where in .pro i add it?

                  jsulmJ 1 Reply Last reply
                  0
                  • M micha_eleric

                    @jsulm would i add

                    unix:LIBS += -L/usr/lib/mysql -lmysql
                    

                    ?
                    and does it matter where in .pro i add it?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @micha_eleric Why don't you simply try?
                    It does not matter where in the pro file you add it.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M 2 Replies Last reply
                    0
                    • jsulmJ jsulm

                      @micha_eleric Why don't you simply try?
                      It does not matter where in the pro file you add it.

                      M Offline
                      M Offline
                      micha_eleric
                      wrote on last edited by
                      #9

                      @jsulm :-1: error: cannot find -lmysql: No such file or directory

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • M micha_eleric

                        @jsulm :-1: error: cannot find -lmysql: No such file or directory

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @micha_eleric Then you should pass the correct path where the mysql library resides in.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        M 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @micha_eleric Why don't you simply try?
                          It does not matter where in the pro file you add it.

                          M Offline
                          M Offline
                          micha_eleric
                          wrote on last edited by
                          #11

                          @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.

                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Christian Ehrlicher

                            @micha_eleric Then you should pass the correct path where the mysql library resides in.

                            M Offline
                            M Offline
                            micha_eleric
                            wrote on last edited by
                            #12

                            @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 EhrlicherC 1 Reply Last reply
                            0
                            • M micha_eleric

                              @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 EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #13

                              It must be something like libmysql.so

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              M 3 Replies Last reply
                              1
                              • Christian EhrlicherC Christian Ehrlicher

                                It must be something like libmysql.so

                                M Offline
                                M Offline
                                micha_eleric
                                wrote on last edited by
                                #14

                                @Christian-Ehrlicher closest i find is libmysqlclient.so

                                1 Reply Last reply
                                0
                                • Christian EhrlicherC Christian Ehrlicher

                                  It must be something like libmysql.so

                                  M Offline
                                  M Offline
                                  micha_eleric
                                  wrote on last edited by
                                  #15

                                  @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 EhrlicherC 1 Reply Last reply
                                  0
                                  • M micha_eleric

                                    @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 EhrlicherC Offline
                                    Christian EhrlicherC Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #16

                                    @micha_eleric So why don't you try it out?

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    SGaistS M 3 Replies Last reply
                                    0
                                    • Christian EhrlicherC Christian Ehrlicher

                                      @micha_eleric So why don't you try it out?

                                      SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #17

                                      Hi,

                                      When you want to use a library as you do, you need to install the corresponding dev package.

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      M 1 Reply Last reply
                                      0
                                      • M micha_eleric

                                        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?

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on last edited by JonB
                                        #18

                                        @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 is libmysqlclient.... That is the route I would follow.

                                        M 1 Reply Last reply
                                        2
                                        • Christian EhrlicherC Christian Ehrlicher

                                          @micha_eleric So why don't you try it out?

                                          M Offline
                                          M Offline
                                          micha_eleric
                                          wrote on last edited by
                                          #19

                                          @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.

                                          1 Reply Last reply
                                          0
                                          • SGaistS SGaist

                                            Hi,

                                            When you want to use a library as you do, you need to install the corresponding dev package.

                                            M Offline
                                            M Offline
                                            micha_eleric
                                            wrote on last edited by
                                            #20

                                            @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

                                            1 Reply Last reply
                                            0

                                            • Login

                                            • Login or register to search.
                                            • First post
                                              Last post
                                            0
                                            • Categories
                                            • Recent
                                            • Tags
                                            • Popular
                                            • Users
                                            • Groups
                                            • Search
                                            • Get Qt Extensions
                                            • Unsolved