1

I have a problem. I want to connect to Mariadb with C++. My Code is as above. I'm new to Linux and C++, so I didn't now what to do. Please help me. Very important for me. Thanks libmariadbclient-dev and mariadb-server is installed. Compiler is Geany (G++). I use code. https://github.com/Roslaniec/MariaCpp/tree/master/mariacpp

#include </home/pi/MariaCpp-master/MariaCpp-master/mariacpp/lib.hpp>
#include </home/pi/MariaCpp-master/MariaCpp-master/mariacpp/connection.hpp>
#include </home/pi/MariaCpp-master/MariaCpp-master/mariacpp/exception.hpp>
#include </home/pi/MariaCpp-master/MariaCpp-master/mariacpp/uri.hpp>
#include <cstdlib>
#include <iostream>
#include <stdlib.h>

int test(const char *uri, const char *user, const char *passwd)
{
    std::clog << "DB uri: " << uri << std::endl;
    std::clog << "DB user: " << user << std::endl;
    std::clog << "DB passwd: " << passwd << std::endl;

    try {
        my_bool reconnect = 1;
        MariaCpp::Connection conn;

        // Some examples how to modify connection options
        conn.options(MYSQL_OPT_RECONNECT, &reconnect);
        conn.options(MYSQL_INIT_COMMAND, "SET SESSION TRANSACTION ISOLATION LEVEL " "READ UNCOMMITTED");
        conn.options(MYSQL_INIT_COMMAND, "set session wait_timeout = 31536000");
        conn.options(MYSQL_INIT_COMMAND, "set autocommit=1");

        // Connect to DB using MySQL Connector/C++ style host URI.
        // You can also use alternative connect() method with
        // the same arguments as C-API.
        conn.connect(MariaCpp::Uri(uri), user, passwd);

        std::clog << "Connection status: SUCESS" << std::endl;

        // Print some statistics
        std::clog << "MySQL Stat: " << conn.stat() << std::endl;

        conn.close(); // optional
    } catch (MariaCpp::Exception &e) {
        std::cerr << e << std::endl;
        return 1;
    }
    return 0;
}

int main()
{
    MariaCpp::scoped_library_init maria_lib_init;
    const char *uri = std::getenv("TEST_DB_URI");
    const char *user = std::getenv("TEST_DB_USER");
    const char *passwd = std::getenv("TEST_DB_PASSWD");
    if (!uri) uri = "tcp://localhost:3306/dotnetdb";
    if (!user) user = "root";
    if (!passwd) passwd = "12345"; 
    return test(uri, user, passwd);
}

Error Output:

g++ -Wall -o "connect" "connect.cpp" (/home/pi/MariaCpp-master/MariaCpp-master/test dizininde)
/usr/bin/ld: /tmp/ccjoOfvg.o: in function `test(char const*, char const*, char const*)':
connect.cpp:(.text+0xb4): undefined reference to `MariaCpp::Connection::Connection()'
/usr/bin/ld: connect.cpp:(.text+0xd0): undefined reference to `MariaCpp::Connection::options(mysql_option, void const*)'
/usr/bin/ld: connect.cpp:(.text+0xec): undefined reference to `MariaCpp::Connection::options(mysql_option, void const*)'
/usr/bin/ld: connect.cpp:(.text+0x108): undefined reference to `MariaCpp::Connection::options(mysql_option, void const*)'
/usr/bin/ld: connect.cpp:(.text+0x124): undefined reference to `MariaCpp::Connection::options(mysql_option, void const*)'
/usr/bin/ld: connect.cpp:(.text+0x158): undefined reference to `MariaCpp::Uri::Uri(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: connect.cpp:(.text+0x17c): undefined reference to `MariaCpp::Connection::connect(MariaCpp::Uri const&, char const*, char const*, unsigned long)'
/usr/bin/ld: connect.cpp:(.text+0x1e0): undefined reference to `MariaCpp::Connection::stat()'
/usr/bin/ld: connect.cpp:(.text+0x214): undefined reference to `MariaCpp::Connection::close()'
/usr/bin/ld: connect.cpp:(.text+0x228): undefined reference to `MariaCpp::Connection::~Connection()'
/usr/bin/ld: connect.cpp:(.text+0x298): undefined reference to `MariaCpp::Connection::~Connection()'
/usr/bin/ld: /tmp/ccjoOfvg.o: in function `MariaCpp::scoped_library_init::scoped_library_init()':
connect.cpp:(.text._ZN8MariaCpp19scoped_library_initC2Ev[_ZN8MariaCpp19scoped_library_initC5Ev]+0x10): undefined reference to `MariaCpp::library_init()'
/usr/bin/ld: /tmp/ccjoOfvg.o: in function `MariaCpp::scoped_library_init::~scoped_library_init()':
connect.cpp:(.text._ZN8MariaCpp19scoped_library_initD2Ev[_ZN8MariaCpp19scoped_library_initD5Ev]+0x10): undefined reference to `MariaCpp::library_end()'
/usr/bin/ld: /tmp/ccjoOfvg.o: in function `MariaCpp::operator<<(std::ostream&, MariaCpp::Exception const&)':
connect.cpp:(.text._ZN8MariaCpplsERSoRKNS_9ExceptionE[_ZN8MariaCpplsERSoRKNS_9ExceptionE]+0x1c): undefined reference to `MariaCpp::Exception::print(std::ostream&) const'
collect2: error: ld returned 1 exit status**
jww
  • 97,681
  • 90
  • 411
  • 885
  • This looks very unusual: `g++ -Wall -o "connect" "connect.cpp"`. But to get back to your problem, you need to add/link the MariaDB client library. I believe it is `libmysqlclient` or `libmariadbclient`, so you need `-lmysqlclient` or `-lmariadbclient` option with your compile command. I could be wrong about the library name, though. – jww Dec 05 '19 at 11:17
  • I think this is probably a better duplicate: [Which lib to use? libmariadbclient or libmysqlclient?](https://stackoverflow.com/q/44030382/608639) It is specific to MariaDB, and it offers a command line demonstrating the link option. – jww Dec 05 '19 at 11:39
  • Thank you for feedback but I see the same error. I write this on terminal screen. g++ -Wall -o connect connect.cpp -lmariadbclient – Özgür Saklanmaz Dec 05 '19 at 11:39
  • Shooting from the hip... You compile and link command should look something like `g++ -Wall -I /home/pi/MariaCpp-master/MariaCpp-master/mariacpp -o connect connect.cpp -L /home/pi/MariaCpp-master/MariaCpp-master/mariacpp/lib -lmariadbclient`. The `-L` path should be where `libmariadbclient.{a|so}` is located. – jww Dec 05 '19 at 11:43
  • I entered the file path with cd and ls commands. So I didn't write the file path. But mariadb client could not find the file path. I am beginner sorry. – Özgür Saklanmaz Dec 05 '19 at 11:54
  • Is there an easy way to compile with geany ? – Özgür Saklanmaz Dec 05 '19 at 11:56
  • Sorry, I don't know Geany. I don't use IDEs. I still write my Makefiles by hand. – jww Dec 05 '19 at 12:04
  • I think one thing you should do... You appear to be using [MariaCPP](https://github.com/Roslaniec/MariaCpp). Clone it, `cd MariaCPP`, and then perform `mkdir cmake-build; cd cmake-build; cmake ../; make; sudo make install`. Now your headers and libraries are on-path at `/usr/local/include` and `/usr/local/lib`. – jww Dec 05 '19 at 12:05

0 Answers0