C++与MySQL的冲突

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:

当在C++代码中,直接引用MySQL文件时,可能会遇到如下错误

In file included from /usr/include/c++/4.1.0/bits/char_traits.h:46,
                 from /usr/include/c++/4.1.0/string:46,
/usr/include/c++/4.1.0/bits/stl_algobase.h:92:28: error: macro "swap" requires 3 arguments, but only 2 given
/usr/include/c++/4.1.0/bits/stl_algobase.h:127:26: error: macro "swap" requires 3 arguments, but only 2 given

/usr/include/c++/4.1.0/bits/vector.tcc:176:20: error: macro "swap" requires 3 arguments, but only 1 given
/usr/include/c++/4.1.0/cctype:70: error: '::isalnum' has not been declared
/usr/include/c++/4.1.0/cctype:71: error: '::isalpha' has not been declared
/usr/include/c++/4.1.0/cctype:72: error: '::iscntrl' has not been declared
/usr/include/c++/4.1.0/cctype:73: error: '::isdigit' has not been declared
/usr/include/c++/4.1.0/cctype:74: error: '::isgraph' has not been declared
/usr/include/c++/4.1.0/cctype:75: error: '::islower' has not been declared
/usr/include/c++/4.1.0/cctype:76: error: '::isprint' has not been declared
/usr/include/c++/4.1.0/cctype:77: error: '::ispunct' has not been declared
/usr/include/c++/4.1.0/cctype:78: error: '::isspace' has not been declared
/usr/include/c++/4.1.0/cctype:79: error: '::isupper' has not been declared
/usr/include/c++/4.1.0/cctype:80: error: '::isxdigit' has not been declared
/usr/include/c++/4.1.0/cctype:81: error: '::tolower' has not been declared
/usr/include/c++/4.1.0/cctype:82: error: '::toupper' has not been declared

解决办法:
尽量对MySQL进行二次包装,让调用者看不到MySQL头文件,如在CPP中包含:
#include <my_global.h>
#include <my_sys.h>
#include <mysql.h>


在头文件中只进行引用声明:
struct st_mysql;
struct st_mysql_res;

typedef long num_t;
typedef char ** MYSQL_ROW;  /** return data as array of strings */

不要在头文件直接include到MySQL的头文件,而且保证只在一个CPP文件中有对MySQL文件的include,否则你可能遇到很多莫名其妙的编译错误,如果不想到这一点,即使花一天时间也未必能找到错误原因。


补充:
MySQL4.x和MySQL5.x头文件不兼容的,最好使用5.x版本

补充:
有些版本只能引用#include <mysql.h>,包含其它会报C++标准库中某文件错误。

原帖发在我的论坛:
http://bbs.hadoopor.com/thread-1564-1-1.html

    本文转自eyjian 51CTO博客,原文链接:http://blog.51cto.com/mooon/909758,如需转载请自行联系原作者



相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
打赏
0
0
0
0
347
分享
相关文章
Linux C/C++ 开发(学习笔记八):Mysql数据库图片存储
Linux C/C++ 开发(学习笔记八):Mysql数据库图片存储
169 0
Linux C/C++ 开发(学习笔记七):Mysql数据库C/C++编程实现 插入/读取/删除
Linux C/C++ 开发(学习笔记七):Mysql数据库C/C++编程实现 插入/读取/删除
246 0
嵌入式C++、STM32、MySQL、GPS、InfluxDB和MQTT协议数据可视化
通过本文的介绍,我们详细讲解了如何结合嵌入式C++、STM32、MySQL、GPS、InfluxDB和MQTT协议,实现数据的采集、传输、存储和可视化。这种架构在物联网项目中非常常见,可以有效地处理和展示实时数据。希望本文能帮助您更好地理解和应用这些技术,构建高效、可靠的数据处理和可视化系统。
153 82
C++orm使用插曲——MySQL保留字
C++orm使用插曲——MySQL保留字
76 7
使用 C++ 结合 MySQL 数据库实现留言板
使用 C++ 结合 MySQL 数据库实现留言板
179 1
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
795 0
C++实现MySQL数据库连接池
为了提升MySQL数据库(基于C/S设计(客户端-服务器))的访问瓶颈,除了在服务器端增加缓冲服务器缓存常用的数据之外
604 0
[mysql 基于C++实现数据库连接池 连接池的使用] 持续更新中
[mysql 基于C++实现数据库连接池 连接池的使用] 持续更新中