Mac下Xcode通過MySQL Connector/C++ 8.0連接MySQL詳細環境搭建歷程

問題描述

爲了使mac下Xcode建立的c++項目能操作mysql數據庫,找遍了國內國外各種教程,踩坑無數,終於摸索出了環境搭建的方法。

心得是,官方文檔永遠是最好的教程:

5.1 Building Connector/C++ Applications: General Considerations

5.2.2 macOS Notes

當然其它各類博客、Stack Overflow上的帖子也或多或少提供了些線索,但無一例外都是不全面的!

摸索了整整半天,吐血探索出來的完整環境搭建步驟整理如下。

如果不在意原理,只想按步驟搭環境,可以直接看最後的總結

環境需求

參考官方文檔,配置環境有以下需求:
Build Tools and Configuration Settings

  1. C++11 Support:編譯器需開啓-std=c++11選項
  2. Connector/C++ Header Files:頭文件搜索路徑需包含ConnectorC++安裝路徑/include
  3. Boost Header Files:在Unix系統中,需要Boost 1.59.0版以上的庫, 頭文集搜索路徑需包含boost所在位置(實際上沒有也行)
  4. Link Libraries:使用OpenSSL編譯時需要引用OpenSSL動態庫
  5. Runtime Libraries:如果使用了動態庫編譯,需要在工程中和可執行文件處均放置Connector/C++、OpenSSL等運行時庫
  6. Using the Connector/C++ Dynamic Library:使用動態庫時,用到libmysqlcppconn8.A.dylib (macOS),A代表版本名
  7. Using the Connector/C++ Static Library:使用靜態庫時,用到libmysqlcppconn8-static.a (Unix, macOS)

第6步,使用動態庫的Makefile文件如下:

MYSQL_CONCPP_DIR = Connector/C++ installation location
CPPFLAGS = -I $(MYSQL_CONCPP_DIR)/include -L $(MYSQL_CONCPP_DIR)/lib64
LDLIBS = -lmysqlcppconn8
CXXFLAGS = -std=c++11
app : app.cc

對應的編譯指令是:

g++ -std=c++11 -I .../include -L .../lib64 app.cc -lmysqlcppconn8 -o app

第7步,使用靜態的Makefile文件如下:

MYSQL_CONCPP_DIR = Connector/C++ installation location
CPPFLAGS = -DSTATIC_CONCPP -I $(MYSQL_CONCPP_DIR)/include
LDLIBS = $(MYSQL_CONCPP_DIR)/lib64/libmysqlcppconn8-static.a -lssl -lcrypto -lpthread
CXXFLAGS = -std=c++11
app : app.cc

對應的編譯指令是:

g++ -std=c++11 -DSTATIC_CONCPP -I .../include app.cc
  .../lib64/libmysqlcppconn8-static.a -lssl -lcrypto -lpthread -o app

安裝MySQL Connector/C++

到官網下載:下載地址
在這裏插入圖片描述
直接下載dmg文件安裝,默認會安裝到/usr/local/mysql-connector-c++-8.0.20
在這裏插入圖片描述
同時也有兩個鏈接文件夾,都可視作同一個。

在Xcode中創建MySQL項目

創建一個命令行項目,名爲,main.cpp裏寫入下面測試代碼:

#include <mysql.h>
#include <iostream>
using namespace std;

//把下面配置信息修改爲自己數據庫的
const char host[] = "localhost";
const char user[] = "root";
const char pwd[] = "123456";
const char database[] = "staff";
unsigned int port = 3306;

int main()
{
    MYSQL myCont;
    mysql_init(&myCont);
    if (mysql_real_connect(&myCont, host, user, pwd, database, port, NULL, 0)){
        cout << "Hello MySQL!" << endl;
    }
    else
    {
        cout << "connect failed!" << endl;
    }
    mysql_close(&myCont);
    return 0;
}

現在什麼庫都沒有配置,當然會報很多錯誤,下面要做的是把錯誤一個個解決掉。

逐步解決報錯

‘mysql.h’ file not found

這個頭文件路徑是在mysql安裝文件夾裏,默認位置:/usr/local/mysql/include

在項目配置中Header Search Paths中加入該路徑:
在這裏插入圖片描述
重新編譯,'mysql.h' file not found已經消失。

Undefined symbol: _mysql_init

下面遇到了三個未定義符合的錯誤:

Undefined symbol: _mysql_close
Undefined symbol: _mysql_init
Undefined symbol: _mysql_real_connect

Library Search Paths中添加/usr/local/mysql-connector-c++/lib64

Other Linker Flags中添加-lmysqlcppconn-static

重新編譯,很開心地看到,錯誤已經發生變化並增加到了100個…
在這裏插入圖片描述

Undefined symbol: _BIO_free

這些錯誤其實都是缺少SSL庫引起的,這些庫其實已經在/usr/local/mysql-connector-c++/lib64中有了,只需在Other Linker Flags中添加-lssl-lcrypto

重新編譯後,開心地發現,編譯已經能通過了。

只不過還會報錯:dyld: Library not loaded: libssl.1.1.dylib

dyld: Library not loaded: libssl.1.1.dylib

程序編譯通過,仍無法運行,錯誤詳情如下:

dyld: Library not loaded: libssl.1.1.dylib
  Referenced from: /Users/xxx/Library/Developer/Xcode/DerivedData/mysqlcpp-eifmjimmumeleagsnsupxghuyjfd/Build/Products/Debug/mysqlcpp
  Reason: image not found

這是由於未能找到動態鏈接庫引起的,哪個路徑報錯(Referenced from後面的),就把庫文件放到哪就完事了。

打開finder-go-go to folder,輸入/Users/xxx/Library/Developer/Xcode/DerivedData/mysqlcpp-eifmjimmumeleagsnsupxghuyjfd/Build/Products/Debug/mysqlcpp,回車,到達相應位置;

或者直接在工程中products文件夾下右鍵-show in finder即可:
在這裏插入圖片描述
/usr/local/mysql-connector-c++/lib64中的libcrypto.1.1.dyliblibssl.1.1.dylib複製過去就行了:
在這裏插入圖片描述

重新運行,即可運行成功:
在這裏插入圖片描述

總結

首先下載安裝mysql-connector-c++-8.0.20-macos10.15-x86-64bit.dmg

然後在Xcode工程具體配置步驟是:

  1. Header Search Paths中加入/usr/local/mysql/include
  2. Library Search Paths中添加/usr/local/mysql-connector-c++/lib64
  3. Other Linker Flags中添加-lmysqlcppconn-static-lssl-lcrypto
  4. /usr/local/mysql-connector-c++/lib64中的libcrypto.1.1.dyliblibssl.1.1.dylib複製到可執行程序所在文件夾
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章