正在解决链接器错误 MySQL Connector/C++
Resolving Linker error MySQL Connector/C++
我希望能够从我的 C++ 程序连接到本地 MySQL 实例,但以下最小文件 testfile.cpp 无法编译并且 returns 未定义引用:
#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
此文件中的 header 来自 MySQL Connector/C++ 的 Github 上的示例代码。
SO 上的 This question 似乎相关,但 syntax/directories 可能已过时。无论如何,我不知道如何根据我的情况和图书馆的位置调整那里给出的答案。因此,我在这里寻求帮助。
更多信息:
我是 运行 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-dev
、libmysqlcppconn7
、libmysqlcppconn8-1
和 libmysqlcppconn8-2
(这可能有点矫枉过正,但根据 installation guide 一个必须安装相当多的这些库)。
which mysql
returns /usr/bin/mysql
当您使用目标文件和库编译源文件和 link 二进制文件时,顺序很重要。提供导出符号的共享库必须跟在导入这些符号的目标文件和其他共享库之后。在您的情况下,共享库必须放在 c++ 命令邀请的末尾:
c++ -o test1 -std=c++11 -I /usr/include/mysql-cppconn-8/ testfile.cpp -lmysqlcppconn8
编译后发现的未定义符号testfile.cpp将从以下libmysqlcppconn8.so导入。链接器不记得从以前的库中导出的符号。有关详细信息,请阅读这篇不错的文章:Why does the order in which libraries are linked sometimes cause errors in GCC.
我希望能够从我的 C++ 程序连接到本地 MySQL 实例,但以下最小文件 testfile.cpp 无法编译并且 returns 未定义引用:
#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
此文件中的 header 来自 MySQL Connector/C++ 的 Github 上的示例代码。
SO 上的This question 似乎相关,但 syntax/directories 可能已过时。无论如何,我不知道如何根据我的情况和图书馆的位置调整那里给出的答案。因此,我在这里寻求帮助。
更多信息:
我是 运行 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-dev
、libmysqlcppconn7
、libmysqlcppconn8-1
和 libmysqlcppconn8-2
(这可能有点矫枉过正,但根据 installation guide 一个必须安装相当多的这些库)。
which mysql
returns /usr/bin/mysql
当您使用目标文件和库编译源文件和 link 二进制文件时,顺序很重要。提供导出符号的共享库必须跟在导入这些符号的目标文件和其他共享库之后。在您的情况下,共享库必须放在 c++ 命令邀请的末尾:
c++ -o test1 -std=c++11 -I /usr/include/mysql-cppconn-8/ testfile.cpp -lmysqlcppconn8
编译后发现的未定义符号testfile.cpp将从以下libmysqlcppconn8.so导入。链接器不记得从以前的库中导出的符号。有关详细信息,请阅读这篇不错的文章:Why does the order in which libraries are linked sometimes cause errors in GCC.