Resolving Linker error MySQL Connector/C++

ksyrium :

I would like to be able to connect from my c++ program to a local MySQL instance, but the following minimal file testfile.cpp does not compile and returns undefined references:

#include <mysqlx/xdevapi.h>    
using namespace ::mysqlx;

int main()
{
    printf("Hello world!\n");
    return 0;
}

I suspect to not use the right compile flags. When I use the command

c++ -o test1 -std=c++11 -lmysqlcppconn8 -I /usr/include/mysql-cppconn-8/ testfile.cpp 

I am getting the following error messages (translated to English):

/tmp/cc02ZbBr.o: In the function "mysqlx::abi2::r0::string::traits<char>::to_str[abi:cxx11](mysqlx::abi2::r0::string const&)":
testfile.cpp:(.text._ZN6mysqlx4abi22r06string6traitsIcE6to_strB5cxx11ERKS2_[_ZN6mysqlx4abi22r06string6traitsIcE6to_strB5cxx11ERKS2_]+0x2e): undefined reference to "mysqlx::abi2::r0::string::Impl::to_utf8[abi:cxx11](mysqlx::abi2::r0::string const&)"
/tmp/cc02ZbBr.o: In the function "mysqlx::abi2::r0::DbDoc::DbDoc()":
testfile.cpp:(.text._ZN6mysqlx4abi22r05DbDocC2Ev[_ZN6mysqlx4abi22r05DbDocC5Ev]+0x1b): undefined reference to "vtable for mysqlx::abi2::r0::DbDoc"
/tmp/cc02ZbBr.o: In the function "mysqlx::abi2::r0::DbDoc::~DbDoc()":
testfile.cpp:(.text._ZN6mysqlx4abi22r05DbDocD2Ev[_ZN6mysqlx4abi22r05DbDocD5Ev]+0xf): undefined reference to "vtable for mysqlx::abi2::r0::DbDoc"
/tmp/cc02ZbBr.o: In the function "mysqlx::abi2::r0::Value::print(std::ostream&) const":
testfile.cpp:(.text._ZNK6mysqlx4abi22r05Value5printERSo[_ZNK6mysqlx4abi22r05Value5printERSo]+0x88): undefined reference to "mysqlx::abi2::r0::common::Value::print(std::ostream&) const"
/tmp/cc02ZbBr.o:(.data.rel.ro._ZTCN6mysqlx4abi22r05ValueE0_NS1_6common5ValueE[_ZTVN6mysqlx4abi22r05ValueE]+0x18): undefined reference to "typeinfo for mysqlx::abi2::r0::common::Value"
/tmp/cc02ZbBr.o:(.data.rel.ro._ZTCN6mysqlx4abi22r05ValueE0_NS1_6common5ValueE[_ZTVN6mysqlx4abi22r05ValueE]+0x20): undefined reference to "mysqlx::abi2::r0::common::Value::print(std::ostream&) const"
/tmp/cc02ZbBr.o:(.data.rel.ro._ZTIN6mysqlx4abi22r05ValueE[_ZTIN6mysqlx4abi22r05ValueE]+0x28): undefined reference to "typeinfo for mysqlx::abi2::r0::common::Value"
collect2: error: ld returned 1 exit status

The header from this file comes from a sample code on MySQL Connector/C++'s Github.

This question on SO seems relevant but the syntax/directories might be outdated. In any case, I do not know how to adjust the answers given there to my situation and location of libraries. Therefore I am asking for help here.

More information:
I'm running Linux Ubuntu 18.04, MySQL version 8.0.19 and have the following files in /usr/lib/x86_64-linux-gnu/

libmysqlcppconn.so  
libmysqlcppconn.so.7.8.0.19  
libmysqlcppconn.so.7  

but I do not know how to refer to them.
In /usr/include/mysql-cppconn-8/ I have the directories jdbc/, mysql/ and mysqlx/.
I installed the following binary packages using the apt package manager: libmysqlcppconn-dev, libmysqlcppconn7, libmysqlcppconn8-1 and libmysqlcppconn8-2 (which is probably overkill but according to the installation guide one has to install quite a few of these libraries).

which mysql returns /usr/bin/mysql

S.M. :

When you compile source files and link binaries with object files and libraries, the order does matter. Shared libraries providing exported symbols must follow object files and other shared libraries importing these symbols. In your case, the shared library must be placed in the end of the c++ command invitation:

c++ -o test1 -std=c++11 -I /usr/include/mysql-cppconn-8/ testfile.cpp  -lmysqlcppconn8

The undefined symbols discovered after compiling testfile.cpp will be imported from the following libmysqlcppconn8.so. Linkers doesn't remember exported symbols from prior libraries. For more information read this nice article: Why does the order in which libraries are linked sometimes cause errors in GCC.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=386660&siteId=1