使用mysql-c ++-connector 8

时间:2019-01-03 13:01:56

标签: c++ mysql

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

#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

我不想使用旧版连接器,因为它不是recommended

2 个答案:

答案 0 :(得分:0)

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

add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)

问题来自MySQL连接器的“ Linux-通用”,它可能是在没有新的CXX11 ABI of GCC(gcc 5.1中引入)的情况下构建的。

由于您的编译器是最新的,因此默认设置是使用新的ABI,因此,在包含<mysqlx/xdevapi.h>时,您将创建如下声明:

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

...使用新的ABI(请参见__cxx11名称空间)。

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

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

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

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

  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来完成此操作:

g++ -D_GLIBCXX_USE_CXX11_ABI=0 main.cpp

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

add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)

如果您使用多个外部库,则必须注意那些库也使用旧的ABI,否则您可能会面临相反的问题,即,使用新ABI构建的库不能直接用于构建具有以下内容的程序:旧的ABI。


1 “ Linux-通用”版本包含对CXX11 ABI的引用,因此我真的不知道这里发生了什么。看来该库的某些部分已使用新的ABI进行编译。

答案 1 :(得分:0)

您可能需要下载当前操作系统的特定版本,而不是通用 linux 版本。

例如rhel8 x86_64平台,mysql-connector-c++-8.0.23-1.el8.x86_64.rpmmysql-connector-c++-devel-8.0.23-应下载 1.el8.x86_64.rpm。 (https://dev.mysql.com/downloads/connector/cpp/)

然后将其解压缩到同一文件夹中。 (注意如果你有windows系统,请在linux中解压,因为里面有软链接) 然后只使用它没有旧的CXX11ABI(需要NOT定义_GLIBCXX_USE_CXX11_ABI=0)