Linux(CentOS7.0)下 C访问MySQL的各种坑
- 作者: 空巢老人-
- 来源: 51数据库
- 2020-08-12
今天尝试用C语言访问连接MySQL。 创建测试文件: mysqlVersion.c [root@iZ23xz8uttcZ test]# vi mysqlVersion.c 写入一下代码: #include <stdio.h> #include <mysql/mysql.h> void mysql_version( void ) {printf("MySQL client version : %d \n",mysql_get_client_version()); } int main(int argc,char *argv[]) { mysql_version(); return 0; } 编译: [root@iZ23xz8uttcZ test]# gcc -c -I /usr/include/mysql mysqlVersion.c -o mysqlVersion.o error提示: 找不到“mysql.h”文件。 解决方案: [root@iZ23xz8uttcZ ~] # rpm -ivh MySQL-devel-advanced-5.6.22-1.el7.x86_64.rpm 链接: [root@iZ23xz8uttcZ test]# gcc -l mysqlclient mysqlVersion.o -o mysqlVersion error提示: mysqlVersion.o: In function `mysql_version': mysqlVersion.c:(.text+0x5): undefined reference to `mysql_get_client_version' collect2: error: ld returned 1 exit status 解决方案: [root@iZ23xz8uttcZ ~] # rpm -ivh MySQL-shared-advanced-5.6.22-1.el7.x86_64.rpm [root@iZ23xz8uttcZ ~] # rpm -ivh MySQL-shared-compat-advanced-5.6.22-1.el7.x86_64.rpm 执行: [root@iZ23xz8uttcZ test]# ./mysqlVersion MySQL client version : 50622 |
推荐阅读