0

I want to try connecting my program (C++ code) to MariaDB. But instead it throws me error saying that it could not find my mariadbcpp library when executing this command to run the code:

Command: g++ -o code code.cpp -std=c++11 -lmariadbcpp

Code Sample:

// Includes
#include <iostream>
#include <mariadb/conncpp.hpp>

// Main Process
int main(int argc, char **argv)
{
   try {
      // Instantiate Driver
      sql::Driver* driver = sql::mariadb::get_driver_instance();

      // Configure Connection, including initial database name "test":
      sql::SQLString url("jdbc:mariadb://localhost:3306/test");

      // Use a properties map for the other connection options
      sql::Properties properties({{"user", "root"}, {"password", ""}});

      // Establish Connection
      // Use a smart pointer for extra safety
      std::unique_ptr<sql::Connection> conn(driver->connect(url, properties));
 
      // Use Connection
      // ...

      // Close Connection
      conn->close();
   }

   // Catch Exceptions
   catch (sql::SQLException& e) {
      std::cerr << "Error Connecting to the database: "
         << e.what() << std::endl;

      // Exit (Failed)
      return 1;
   }

   // Exit (Success)
   return 0;
}

Error:

/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/11/../../../../lib/libmariadbcpp.so when searching for -lmariadbcpp
/usr/bin/ld: skipping incompatible /lib/../lib/libmariadbcpp.so when searching for -lmariadbcpp
/usr/bin/ld: skipping incompatible /usr/lib/../lib/libmariadbcpp.so when searching for -lmariadbcpp
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/11/../../../libmariadbcpp.so when searching for -lmariadbcpp
/usr/bin/ld: skipping incompatible /lib/libmariadbcpp.so when searching for -lmariadbcpp
/usr/bin/ld: skipping incompatible /usr/lib/libmariadbcpp.so when searching for -lmariadbcpp
/usr/bin/ld: cannot find -lmariadbcpp: No such file or directory
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/11/../../../../lib/libmariadbcpp.so when searching for -lmariadbcpp
/usr/bin/ld: skipping incompatible /lib/../lib/libmariadbcpp.so when searching for -lmariadbcpp
/usr/bin/ld: skipping incompatible /usr/lib/../lib/libmariadbcpp.so when searching for -lmariadbcpp
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/11/../../../libmariadbcpp.so when searching for -lmariadbcpp
/usr/bin/ld: skipping incompatible /lib/libmariadbcpp.so when searching for -lmariadbcpp
/usr/bin/ld: skipping incompatible /usr/lib/libmariadbcpp.so when searching for -lmariadbcpp
collect2: error: ld returned 1 exit status

I already installed MariaDB Community Server, MariaDB C/Connector & MariaDB C++/Connector based on their respective docs.

MariaDB Community Server: https://www.digitalocean.com/community/tutorials/how-to-install-mariadb-on-ubuntu-20-04

MariaDB C/Connector: https://mariadb.com/docs/skysql/connect/programming-languages/c/install/#CS_Package_Repository

MariaDB C++/Connector: https://mariadb.com/docs/skysql/connect/programming-languages/cpp/install/#Linux_Installation_(Binary_Tarball)

I already rebuild the MariaDB C++/Connector by following the GitHub instruction here.

It does fix the compatibility issue, but /usr/bin/ld: cannot find -lmariadbcpp: No such file or directory error is still there.

I also already run the program with -L flag instead.The command is g++ -o code code.cpp -std=c++11 -L/usr/local/lib/mariadb/libmariadbcpp.so but instead it throws me bunch of undefined errors.

The Errors:

/usr/bin/ld: /tmp/ccblDiuQ.o: in function `main':
code.cpp:(.text+0x31): undefined reference to `sql::mariadb::get_driver_instance()'
/usr/bin/ld: code.cpp:(.text+0x51): undefined reference to `sql::SQLString::SQLString(char const*)'
/usr/bin/ld: code.cpp:(.text+0x194): undefined reference to `sql::SQLString::~SQLString()'
/usr/bin/ld: code.cpp:(.text+0x23c): undefined reference to `sql::SQLString::~SQLString()'
/usr/bin/ld: /tmp/ccblDiuQ.o: in function `std::pair<sql::SQLString const, sql::SQLString>::~pair()':
code.cpp:(.text._ZNSt4pairIKN3sql9SQLStringES1_ED2Ev[_ZNSt4pairIKN3sql9SQLStringES1_ED5Ev]+0x1c): undefined reference to `sql::SQLString::~SQLString()'
/usr/bin/ld: code.cpp:(.text._ZNSt4pairIKN3sql9SQLStringES1_ED2Ev[_ZNSt4pairIKN3sql9SQLStringES1_ED5Ev]+0x28): undefined reference to `sql::SQLString::~SQLString()'
/usr/bin/ld: /tmp/ccblDiuQ.o: in function `std::pair<sql::SQLString const, sql::SQLString>::pair<char const (&) [5], char const (&) [11], true>(char const (&) [5], char const (&) [11])':
code.cpp:(.text._ZNSt4pairIKN3sql9SQLStringES1_EC1IRA5_KcRA11_S5_Lb1EEEOT_OT0_[_ZNSt4pairIKN3sql9SQLStringES1_EC1IRA5_KcRA11_S5_Lb1EEEOT_OT0_]+0x30): undefined reference to `sql::SQLString::SQLString(char const*)'
/usr/bin/ld: code.cpp:(.text._ZNSt4pairIKN3sql9SQLStringES1_EC1IRA5_KcRA11_S5_Lb1EEEOT_OT0_[_ZNSt4pairIKN3sql9SQLStringES1_EC1IRA5_KcRA11_S5_Lb1EEEOT_OT0_]+0x4f): undefined reference to `sql::SQLString::SQLString(char const*)'
/usr/bin/ld: code.cpp:(.text._ZNSt4pairIKN3sql9SQLStringES1_EC1IRA5_KcRA11_S5_Lb1EEEOT_OT0_[_ZNSt4pairIKN3sql9SQLStringES1_EC1IRA5_KcRA11_S5_Lb1EEEOT_OT0_]+0x64): undefined reference to `sql::SQLString::~SQLString()'
/usr/bin/ld: /tmp/ccblDiuQ.o: in function `std::pair<sql::SQLString const, sql::SQLString>::pair<char const (&) [9], char const (&) [9], true>(char const (&) [9], char const (&) [9])':
code.cpp:(.text._ZNSt4pairIKN3sql9SQLStringES1_EC1IRA9_KcS7_Lb1EEEOT_OT0_[_ZNSt4pairIKN3sql9SQLStringES1_EC1IRA9_KcS7_Lb1EEEOT_OT0_]+0x30): undefined reference to `sql::SQLString::SQLString(char const*)'
/usr/bin/ld: code.cpp:(.text._ZNSt4pairIKN3sql9SQLStringES1_EC1IRA9_KcS7_Lb1EEEOT_OT0_[_ZNSt4pairIKN3sql9SQLStringES1_EC1IRA9_KcS7_Lb1EEEOT_OT0_]+0x4f): undefined reference to `sql::SQLString::SQLString(char const*)'
/usr/bin/ld: code.cpp:(.text._ZNSt4pairIKN3sql9SQLStringES1_EC1IRA9_KcS7_Lb1EEEOT_OT0_[_ZNSt4pairIKN3sql9SQLStringES1_EC1IRA9_KcS7_Lb1EEEOT_OT0_]+0x64): undefined reference to `sql::SQLString::~SQLString()'
/usr/bin/ld: /tmp/ccblDiuQ.o: in function `std::less<sql::SQLString>::operator()(sql::SQLString const&, sql::SQLString const&) const':
code.cpp:(.text._ZNKSt4lessIN3sql9SQLStringEEclERKS1_S4_[_ZNKSt4lessIN3sql9SQLStringEEclERKS1_S4_]+0x27): undefined reference to `sql::SQLString::operator<(sql::SQLString const&) const'
/usr/bin/ld: /tmp/ccblDiuQ.o: in function `std::pair<sql::SQLString const, sql::SQLString>::pair(std::pair<sql::SQLString const, sql::SQLString> const&)':
code.cpp:(.text._ZNSt4pairIKN3sql9SQLStringES1_EC2ERKS3_[_ZNSt4pairIKN3sql9SQLStringES1_EC5ERKS3_]+0x24): undefined reference to `sql::SQLString::SQLString(sql::SQLString const&)'
/usr/bin/ld: code.cpp:(.text._ZNSt4pairIKN3sql9SQLStringES1_EC2ERKS3_[_ZNSt4pairIKN3sql9SQLStringES1_EC5ERKS3_]+0x3f): undefined reference to `sql::SQLString::SQLString(sql::SQLString const&)'
/usr/bin/ld: code.cpp:(.text._ZNSt4pairIKN3sql9SQLStringES1_EC2ERKS3_[_ZNSt4pairIKN3sql9SQLStringES1_EC5ERKS3_]+0x54): undefined reference to `sql::SQLString::~SQLString()'
/usr/bin/ld: /tmp/ccblDiuQ.o:(.data.rel.local.DW.ref._ZTIN3sql12SQLExceptionE[DW.ref._ZTIN3sql12SQLExceptionE]+0x0): undefined reference to `typeinfo for sql::SQLException'
collect2: error: ld returned 1 exit status
fisherbone
  • 13
  • 1
  • 6
  • I expect you are mixing 32 and 64 bit and that is why its incompatible. Looks like the libraries are 64 bit. – drescherjm Jan 15 '23 at 05:42
  • @drescherjm my libraries are 64 bit and my server is 32 bit? – fisherbone Jan 15 '23 at 05:46
  • @drescherjm I already added additional flag for build command `g++ -m64 -o code code.cpp -std=c++11 -lmariadbcpp` however, the output is still the same – fisherbone Jan 15 '23 at 07:09
  • @drescherjm -m64 is for producing the 64 bit binaries AFAIK – fisherbone Jan 15 '23 at 07:10
  • 1
    It looks like you need `-m32` to build as 32-bit instead of building as 64-bit. Are you on windows? If on Linux, just build the connector yourself - you can build for both architectures that way (you can do the same on windows -- it's just a bit more involved from a dependency standpoint) Your connector is 32-bit (or the opposite 64-bit), thus `"... skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/11/../../../libmariadbcpp.so when searching for -lmariadbcpp"`. Are you linking against something other than just the code above and the connector? – David C. Rankin Jan 15 '23 at 07:12
  • `-m32` flag throws me error `fatal error: bits/c++config.h: No such file or directory`. I'm on WSL (Linux) and I install the C connector based on the docs above. It requires me to download it from the package manager while the C++ connector, I have to build it myself from the Binary Tarball (as the docs above). I'm not sure about the Connector version whether is it 32 or 64 bit, but I can confirm that my compiler is producing 64 bit binaries. I'm not linking the connector against something other than just the code above. Should I build the connector again? – fisherbone Jan 15 '23 at 07:26
  • Oh, WSL -- I was going to tell you you can build from [github - mariadb-connector-cpp](https://aur.archlinux.org/cgit/aur.git/snapshot/mariadb-connector-cpp-git.tar.gz) (I just built it on Arch to make sure). For whatever reason you have a mismatch between your library architecture and the WSL architecture (which if I recall is 64-bit only...) If you build the connector, you are guaranteed it will match the WSL architecture. (confirm and adjust your include directory location and library location, e.g. either `/usr/include/mysql` or `/usr/include/mariadb` less likely, and e.g. `/usr/lib`. – David C. Rankin Jan 15 '23 at 07:35
  • MariaDB doesn't provide 32-bit binary packages for MariaDB Connector/C++ – Georg Richter Jan 15 '23 at 16:24
  • @DavidC.Rankin Okay, I've tried to build the connector by following the BUILD instruction on GitHub [here](https://github.com/mariadb-corporation/mariadb-connector-cpp/blob/master/BUILD.md). I would say it did somehow fix the incompatibility issue, but when I ran the command `g++ -o code code.cpp -std=c++11 -lmariadbcpp`, it just throws me one error which is `/usr/bin/ld: cannot find -lmariadbcpp: No such file or directory`. I know where the path of the file is, it is in `/usr/local/lib/mariadb/libmariadbcpp.so` but I wonder why it doesn't detect it. – fisherbone Jan 16 '23 at 07:12
  • After you *BUILD* and *INSTALL* the connector, you will need to run `ldconfig` as `root` so that your new connector library is found and made available. I guess there is no real `root` account on WSL, so you will need to use `sudo`, e.g. `sudo ldconfig` to update the library configuration. When I boot win10 next, I'll gite it a go. Honestly, I rarely use it anymore. I've gotten to where I just use [Msys2](https://www.msys2.org/) on windows for a Linux environment. You need to tell the linker where to find the lib. Pass the library search path `-L/usr/local/lib/mariadb` to `g++` as well. – David C. Rankin Jan 16 '23 at 07:43
  • @DavidC.Rankin okay, I've ran `sudo ldconfig` and added the path of library by executing `g++ -o code code.cpp -std=c++11 --L/usr/local/lib/mariadb -lmariadbcpp`. **The build was successfully created** but when I tried to run the code, it says `/home/fisherbone/dev/test/code: error while loading shared libraries: libmariadbcpp.so: cannot open shared object file: No such file or directory` – fisherbone Jan 16 '23 at 09:35
  • Run `ldd` on the executable and see what it says. The issue here is that the C-runtime can't find the library when the executable starts. This is usually due to the library being in a non-standard location. However, most Linux distributions add `/usr/lib` and `/usr/local/lib` as default library locations. In the event Ubuntu doesn't automatically add `/usr/local/lib` as part of the normal library path. You can rebuild the connector with `-DCMAKE_INSTALL_PREFIX=/usr -DINSTALL_LIB_SUFFIX=/usr/lib -DUSE_SYSTEM_INSTALLED_LIB=TRUE` to put the lib in the normal system location. – David C. Rankin Jan 16 '23 at 10:01
  • You may want to pose the last runtime error on the [AskUbuntu](https://askubuntu.com/) here to see if they have a quick-fix for that issue. – David C. Rankin Jan 16 '23 at 10:02

1 Answers1

0

steps you followed seems to be correct: https://mariadb.com/resources/blog/how-to-connect-c-programs-to-mariadb

May be you can try with g++ option -L to specify path location of mariadbcpp.so. From error, it seems, it is not able to find one.

  • I already did try with `-L` option by issuing `g++ -o code code.cpp -std=c++11 -L/usr/local/lib/mariadb/libmariadbcpp.so`, but instead it throw me which bunch of undefined reference. Errors: `code.cpp:(.text+0x31): undefined reference to sql::mariadb::get_driver_instance()` and so on... – fisherbone Jan 16 '23 at 07:15
  • check this one, in case you haven't: https://stackoverflow.com/a/16005836/20950734 – Yogesh Dhawale Jan 17 '23 at 05:24