Windows下MySQL Connector/C++ 8.1编译说明

概述:本文主要记录 MySQL Connector/C++ 8.1 静态库的编译方式,因项目工程问题,需要编译 /MT 模式的静态库文件。

官方文档:4.3 从源安装连接器/C++_MySQL 连接器/C++ 8.0 开发人员指南

本次编译目标产物:

  • [x] Debug 模式下 /MTd 编译 静态编译
  • [ ] Release 模式下 /MT 编译 静态编译

前言

需求及官方文件说明

由于 MySQL官方并没有提供 /MT 模式的lib文件,因此编译所需要的静态库都需要从源码编译,首先说明一下官方已编译好的文件:

MySQL :: MySQL Connector/C++ Developer Guide :: 5.2.1 Windows Notes 这里可以看到官方提供了 /MD 模式下的 Debug 和 Release 静态库文件,下载为:MySQL :: Download MySQL Connector/C++ (Archived Versions)。关于下载后安装包的文件这我补充一下:

  • mysql-connector-c+±8.0.33-win32-debug:Debug 模式下 /MD 的编译产物
  • mysql-connector-c+±8.0.33-win32:Release 模式下 /MD 的编译产物

两个下载包的文件基本一致,主要是以下四个文件:

  • mysqlcppconn.lib: 使用 JDBC+openssl1.1.1 版本编译
  • mysqlcppconn8.lib:使用 X DEVAPI+openssl1.1.1 编译产物
  • mysqlcppconn8-static.lib:使用 X DEVAPI+OpenSSL3.3.1 编译产物
  • mysqlcppconn-static.lib:使用 JDBC+OpenSSL3.3.31 编译产物

使用 JDBCX DEVAPI 的包需要使用不同的方式去连接 MySQL,具体代码如下所示:

使用 JDBC 连接

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <list>
#include <cstdlib>

#include <mysqlx/xdevapi.h>

using namespace mysqlx;
#ifdef _DEBUG
#pragma comment(lib, "mysqlcppconn8-static-debug-openssl3-mdd.lib") // 特殊命名标记了一下,官方库文件下 mysqlcppconn8_static.lib
#pragma comment(lib, "dnsapi.lib")
#else
#pragma comment(lib, "mysqlcppconn8_md.lib")// 特殊命名标记了一下,官方库文件下 mysqlcppconn8_static.libs
#pragma comment(lib, "dnsapi.lib")
#endif // DEBUG

int main() {
// 修改控制台编码
// std::system("chcp 65001");

try {
// 改成你的信息b
Session sess(SessionOption::USER, "root",
SessionOption::PWD, "123456",
SessionOption::HOST, "localhost",
SessionOption::PORT, 33060,
SessionOption::DB, "test");

auto result = sess.sql("select * from user").execute();

for (auto row : result.fetchAll()) {
std::cout << row[0] << " " << row[1] << " " << row[2] << "\n";
}

}
catch (const std::exception& e) {
std::cerr << e.what() << '\n';
}
}

预处理器

参考官方说明:For applications that use X DevAPI, X DevAPI for C, or (as of Connector/C++ 8.0.16) the legacy JDBC API, define the STATIC_CONCPP macro. All that matters is that you define it; the value does not matter. For example: -DSTATIC_CONCPP

使用 JDBC 连接编译时,需要在预处理器添加 STATIC_CONCPP 宏。

预处理器添加宏

特别的,如果执行以上步骤之后还是编译报错,需要根据相关报错信息链接其他的库。如 MSCVRT.lib

使用cmkae生成工程文件

有其他特殊需求参考官方说明:MySQL :: MySQL Connector/C++ Developer Guide :: 4.3 Installing Connector/C++ from Source

官方要求安装boost库,实际编译不安装boost也可以编译成功。

编译命令如下所示:

1
cmake . -G "Visual Studio 15" -A Win32 -DBUILD_STATIC=ON -DWITH_SSL="D:\OpenSSL-3.1-Win32-mtd" -DWITH_JDBC=ON -DWITH_MYSQL="C:\Program Files\MySQL\MySQL Server 8.0"

生成 vs2017工程(v141工具集)下的工程文件,产物为静态库,OpenSSL安装位置为 "D:\OpenSSL-3.1-Win32-mtd。说明:

  • -G “Visual Studio 16”:指定工具集
  • -A Win32 指定生成的系统
  • -DBUILD_STATIC=ON:指定生成静态库
  • -DWITH_SSL=“D:\OpenSSL-3.1-Win32-mtd”:指定 OpenSSL 的安装位置
  • -DWITH_MYSQL=“C:\Program Files\MySQL\MySQL Server 8.0”:指定 mysql的安装位置
  • -DWITH_JDBC=ON:指定编译JDBC

由于需要 /MT 版本的,因此 OpenSSL 也需要自己编译 /MT 版本的才可以。


Windows下MySQL Connector/C++ 8.1编译说明
https://hodlyounger.github.io/C_OpenSource/MySQL/mysql-connnector-8.0编译说明/
作者
mingming
发布于
2023年10月27日
许可协议