簡體   English   中英

解決鏈接器錯誤 MySQL Connector/C++

[英]Resolving Linker error MySQL Connector/C++

我希望能夠從我的 C++ 程序連接到本地 MySQL 實例,但以下最小文件 testfile.cpp 不會編譯並返回未定義的引用:

#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++'s Github上的示例代碼。

這個關於 SO 的問題似乎相關,但語法/目錄可能已經過時。 無論如何,我不知道如何根據我的情況和圖書館的位置調整那里給出的答案。 因此,我在這里尋求幫助。

更多信息:
我正在運行 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 (這可能有點矯枉過正,但根據安裝指南,必須安裝相當多的這些庫)。

which mysql返回/usr/bin/mysql

當您編譯源文件並將二進制文件與目標文件和庫鏈接時,順序很重要。 提供導出符號的共享庫必須遵循導入這些符號的目標文件和其他共享庫。 在您的情況下,共享庫必須放在 c++ 命令邀請的末尾:

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

編譯 testfile.cpp 后發現的未定義符號將從以下 libmysqlcppconn8.so 中導入。 鏈接器不記得從先前庫中導出的符號。 有關更多信息,請閱讀這篇不錯的文章: 為什么鏈接庫的順序有時會導致 GCC 中的錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM