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

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

本次编译目标产物:

  • 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 连接

代码

#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也可以编译成功。

编译命令如下所示:

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 版本的才可以。

Release 模式编译

Release 模式的编译流程与 Debug 模式基本一致,主要区别在于 OpenSSL 的版本配置。

准备 Release 版 OpenSSL

由于 Release 模式需要 /MT 编译,需要准备相应版本的 OpenSSL:

  • Debug 模式:使用 OpenSSL-3.1-Win32-mtd(带调试符号)
  • Release 模式:使用 OpenSSL-3.1-Win32-mt(无调试符号)

OpenSSL 的编译命令参考(以 Release 为例):

# 在 OpenSSL 源码目录执行
perl Configure VC-WIN32 no-shared -mt
nmake

编译完成后,将其安装到如 D:\OpenSSL-3.1-Win32-mt 目录。

CMake 生成工程(Release)

Release 模式的 CMake 配置命令:

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

主要变化:

  • 将 OpenSSL 路径从 mtd 改为 mt
  • 其他参数与 Debug 模式保持一致

编译 Release 静态库

# 生成工程文件后,使用 MSBuild 编译
msbuild mysqlcppconn8-static.vcxproj /p:Configuration=Release /p:Platform=Win32

或者直接用 Visual Studio 打开解决方案,选择 Release 配置进行编译。

编译产物

编译成功后,在 build/release 目录下会生成以下静态库文件:

  • mysqlcppconn8-static.lib - X DevAPI 版本(推荐)
  • mysqlcppconn-static.lib - JDBC 版本

Release 模式使用示例

在项目中使用 Release 版静态库的配置:

#include <mysqlx/xdevapi.h>
 
using namespace mysqlx;
 
// 链接 Release 版静态库
#pragma comment(lib, "mysqlcppconn8-static.lib")  // Release 版
#pragma comment(lib, "dnsapi.lib")
 
// 如果使用 X DevAPI,也需要定义 STATIC_CONCPP
#define STATIC_CONCPP
 
int main() {
    try {
        Session sess(SessionOption::USER, "root",
                     SessionOption::PWD, "yourpassword",
                     SessionOption::HOST, "localhost",
                     SessionOption::PORT, 33060,
                     SessionOption::DB, "test");
 
        auto result = sess.sql("SELECT VERSION()").execute();
        std::cout << "MySQL Version: " << result.fetchOne()[0] << std::endl;
    }
    catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }
    return 0;
}

注意事项

  1. 运行时库配置:确保项目属性中的「C/C++ → 代码生成 → 运行时库」设置为 多线程 (/MT)
  2. 预处理器宏:使用静态库时需要定义 STATIC_CONCPP
  3. 依赖库:可能还需要链接系统库如 ws2_32.libcrypt32.lib
  4. OpenSSL 依赖:虽然编译为静态库,但运行时仍需 OpenSSL 的 DLL 文件在 PATH 中