gpt4 book ai didi

c++ - 解决链接器错误MySQL Connector/C++

转载 作者:行者123 更新时间:2023-12-02 10:53:02 25 4
gpt4 key购买 nike

我希望能够从我的c++程序连接到本地MySQL实例,但是以下最小文件testfile.cpp无法编译并返回 undefined reference :

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

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

我怀疑没有使用正确的编译标志。当我使用命令
c++ -o test1 -std=c++11 -lmysqlcppconn8 -I /usr/include/mysql-cppconn-8/ testfile.cpp 

我收到以下错误消息(翻译成英文):
/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

该文件的头来自MySQL Connector/C++的 Github上的示例代码。

SO上的 This question似乎相关,但语法/目录可能已过时。无论如何,我都不知道如何根据我的情况和图书馆位置来调整答案。因此,我在这里寻求帮助。

更多信息:
我正在运行Linux Ubuntu 18.04,MySQL 8.0.19版本,并在 /usr/lib/x86_64-linux-gnu/中包含以下文件
libmysqlcppconn.so  
libmysqlcppconn.so.7.8.0.19
libmysqlcppconn.so.7

但我不知道该如何称呼他们。
/usr/include/mysql-cppconn-8/中,我有目录 jdbc/mysql/mysqlx/
我使用apt软件包管理器安装了以下二进制软件包: libmysqlcppconn-devlibmysqlcppconn7libmysqlcppconn8-1libmysqlcppconn8-2(这可能有些过分,但根据 installation guide的说法,其中一个必须安装许多此类库)。
which mysql返回 /usr/bin/mysql

最佳答案

当您编译源文件并将二进制文件与目标文件和库链接时,顺序确实很重要。提供导出符号的共享库必须跟随目标文件,而其他共享库也要导入这些符号。在您的情况下,必须将共享库放在c++命令邀请的末尾:

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

编译testfile.cpp之后发现的 undefined symbol 将从下面的libmysqlcppconn8.so中导入。链接器不记得先前库中导出的符号。有关更多信息,请阅读这篇不错的文章: Why does the order in which libraries are linked sometimes cause errors in GCC

关于c++ - 解决链接器错误MySQL Connector/C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60903370/

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