gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:57:19 32 4
gpt4 key购买 nike

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

最佳答案

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-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 –通用”版本包含对CXX11 ABI的引用,因此我真的不知道这里发生了什么。似乎库的某些部分已使用新的ABI进行编译。

关于c++ - 使用mysql-c++-connector 8将c++连接到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54022842/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com