c++-connector 8将c++连接到mysql

woobm2wo  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(418)

我想使用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

我不想使用旧的连接器,因为这是不推荐的。

ttcibm8c

ttcibm8c1#

热释光;博士;您需要通过设置 _GLIBCXX_USE_CXX11_ABI0 . 这可以通过 cmake 通过将以下内容添加到 CMakeLists.txt :

add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)

这个问题来自mysql连接器的“linux-generic”,它可能是在没有gcc的新cxx11abi(在gcc5.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-generic”版本(带有 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-generic”版本包含对cxx11 abi的引用,所以我不知道这里到底发生了什么。看起来库的某些部分已经用新的abi编译。

相关问题