使用 mysql-c++-connector 8 将 c++ 连接到 mysql [英] Connecting c++ to mysql using mysql-c++-connector 8

查看:116
本文介绍了使用 mysql-c++-connector 8 将 c++ 连接到 mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 mysql-c++-connector8 (xdevapi) 连接到 mysql 8,我使用 .so 文件进行链接,但出现错误这是我的 ma​​in.cpp 文件

I want to connect to mysql 8 using mysql-c++-connector8 (xdevapi) and I use .so file for linking but I get errors here is my main.cpp file

#include <iostream>
#include <mysqlx/xdevapi.h>

using namespace ::mysqlx;
using std::cout;
using std::cin;
using std::endl;

int main(){

 Session sess("localhost",3306,"root","mypass");
 Schema db = sess.getSchema("university");

 Collection myColl = db.getCollection("student");
 DocResult myDocs = myColl.find("name like :param").execute();

 cout<<myDocs.fetchOne();
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(1)
set(CMAKE_CXX_STANDARD 17)

include_directories(~/mysql-connector-c++/include)
link_directories(~/mysql-connector-c++/lib64)

set(PROJECT_LINK_LIBS libmysqlcppconn8.so)
add_executable(myExe main.cpp)
target_link_libraries(myExe ${PROJECT_LINK_LIBS})

错误:

[ 50%] Linking CXX executable myExe
/usr/bin/ld: CMakeFiles/myExe.dir/main.cpp.o: in function `mysqlx::string::string(char const*)':
/home/amir/mysql-connector-c++/include/mysqlx/devapi/common.h:100: undefined reference to `mysqlx::string::Impl::from_utf8(mysqlx::string&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: CMakeFiles/myExe.dir/main.cpp.o: in function `mysqlx::string::string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/home/amir/mysql-connector-c++/include/mysqlx/devapi/common.h:105: undefined reference to `mysqlx::string::Impl::from_utf8(mysqlx::string&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: CMakeFiles/myExe.dir/main.cpp.o: in function `mysqlx::string::operator std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >() const':
/home/amir/mysql-connector-c++/include/mysqlx/devapi/common.h:115: undefined reference to `mysqlx::string::Impl::to_utf8[abi:cxx11](mysqlx::string const&)'
/usr/bin/ld: CMakeFiles/myExe.dir/main.cpp.o: in function `void mysqlx::internal::Settings_detail<mysqlx::internal::Settings_traits>::set<true, mysqlx::SessionOption, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, mysqlx::SessionOption, unsigned int&, mysqlx::SessionOption, mysqlx::string const&>(mysqlx::SessionOption, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, mysqlx::SessionOption&&, unsigned int&, mysqlx::SessionOption&&, mysqlx::string const&)':
/home/amir/mysql-connector-c++/include/mysqlx/devapi/detail/settings.h:67: undefined reference to `mysqlx::internal::Settings_detail<mysqlx::internal::Settings_traits>::do_set(std::__cxx11::list<std::pair<int, mysqlx::common::Value>, std::allocator<std::pair<int, mysqlx::common::Value> > >&&)'
/usr/bin/ld: CMakeFiles/myExe.dir/main.cpp.o: in function `void mysqlx::internal::Settings_detail<mysqlx::internal::Settings_traits>::set<true, mysqlx::SessionOption, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(mysqlx::SessionOption, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&)':
/home/amir/mysql-connector-c++/include/mysqlx/devapi/detail/settings.h:67: undefined reference to `mysqlx::internal::Settings_detail<mysqlx::internal::Settings_traits>::do_set(std::__cxx11::list<std::pair<int, mysqlx::common::Value>, std::allocator<std::pair<int, mysqlx::common::Value> > >&&)'
/usr/bin/ld: CMakeFiles/myExe.dir/main.cpp.o: in function `void mysqlx::internal::Settings_detail<mysqlx::internal::Settings_traits>::set<true, mysqlx::SessionOption, mysqlx::string const&>(mysqlx::SessionOption, mysqlx::string const&)':
/home/amir/mysql-connector-c++/include/mysqlx/devapi/detail/settings.h:67: undefined reference to `mysqlx::internal::Settings_detail<mysqlx::internal::Settings_traits>::do_set(std::__cxx11::list<std::pair<int, mysqlx::common::Value>, std::allocator<std::pair<int, mysqlx::common::Value> > >&&)'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/myExe.dir/build.make:95: myExe] Error 1
make[2]: *** [CMakeFiles/Makefile2:68: CMakeFiles/myExe.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:80: CMakeFiles/myExe.dir/rule] Error 2
make: *** [Makefile:118: myExe] Error 2

我不想使用旧式连接器,因为它不是 推荐 .

I don't want to use legacy connectors as it is not recommended .

推荐答案

TL;DR; 您需要通过将 _GLIBCXX_USE_CXX11_ABI 设置为 来使用旧的 GCC ABI>0.这可以使用 cmake 来完成,方法是将以下内容添加到您的 CMakeLists.txt 中:

TL;DR; You need to use the old GCC ABI by setting the _GLIBCXX_USE_CXX11_ABI to 0. This can be done with cmake by adding the following to your CMakeLists.txt:

add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)

<小时>

问题来自 MySQL 连接器的Linux - Generic",它可能是在没有新的 GCC 的 CXX11 ABI(在 gcc 5.1 中引入).


The issue comes from the "Linux - Generic" of the MySQL connector which have likely been built without the new CXX11 ABI of GCC (introduced in gcc 5.1).

由于你的编译器是最新的,默认是使用新的 ABI,所以当你包含 <mysqlx/xdevapi.h> 时,你创建如下声明:

Since your compiler is recent, the default is to use the new ABI, so when you include <mysqlx/xdevapi.h>, you create declarations such as:

mysqlx::string::Impl::from_utf8(
    mysqlx::string&, 
    std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)

...使用新的 ABI(请参阅 __cxx11 命名空间).

...that use the new ABI (see the __cxx11 namespace).

但是由于连接器是用旧的 ABI 构建的,libmysqlcppconn8.so 包含匹配这种声明的函数的符号:

But since the connectors have been built with the old ABI, the libmysqlcppconn8.so contains symbols for functions matching this kind of declaration:

mysqlx::string::Impl::from_utf8(
    mysqlx::string&, 
    std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)

您可以通过运行 readelf -Ws libmysqlcppconn8.so 来检查这一点,对于我获得的Linux - 通用"版本(使用 grep from_utf8)1:

You can check this by running readelf -Ws libmysqlcppconn8.so, for the "Linux - Generic" version I get (with a grep from_utf8)1:

1428: 00000000000a0a86 193 FUNC  GLOBAL DEFAULT   12 _ZN6mysqlx6string4Impl9from_utf8ERS0_RKSs
9437: 00000000000a0a86 193 FUNC  GLOBAL DEFAULT   12 _ZN6mysqlx6string4Impl9from_utf8ERS0_RKSs

但是对于 Ubuntu 18.10 版本,我得到:

But for the Ubuntu 18.10 version I get:

  725: 00000000000a23e0 183 FUNC GLOBAL DEFAULT   12 _ZN6mysqlx6string4Impl9from_utf8ERS0_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
10141: 00000000000a23e0 183 FUNC GLOBAL DEFAULT   12 _ZN6mysqlx6string4Impl9from_utf8ERS0_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

最简单的解决方法是告诉编译器使用旧的 ABI,以便符号匹配.这可以通过在编译时将 _GLIBCXX_USE_CXX11_ABI 宏设置为 0 来完成:

The easiest fix is to tell your compiler to use the old ABI, so that the symbol will match. This can be done by setting the _GLIBCXX_USE_CXX11_ABI macro to 0 when compiling:

g++ -D_GLIBCXX_USE_CXX11_ABI=0 main.cpp

...或通过将以下内容添加到 CMakeLists.txt:

...or by adding the following to CMakeLists.txt:

add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)

如果你使用多个外部库,你必须小心那些也使用旧ABI,否则你可能会面临相反的问题,即使用新ABI构建的库不能直接用于构建程序旧的 ABI.

If you use multiple external libraries, you have to be careful that those also use the old ABI, otherwise you will likely face the opposite issue, i.e., a library built with the new ABI cannot be used directly to build a program with the old ABI.

1Linux - Generic"版本包含对 CXX11 ABI 的引用,所以我真的不知道这里发生了什么.看起来库的某些部分已经使用新的 ABI 编译.

1 The "Linux - Generic" version contains reference to the CXX11 ABI, so I do not really know what is going on here. It looks like some parts of the library has been compile with the new ABI.

这篇关于使用 mysql-c++-connector 8 将 c++ 连接到 mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆