是否可以从图书馆连接到 mysql?

Is it possible to connect to mysql from the library so?

在为外部应用程序编写我的第一个插件时遇到了很大的障碍。 插件作为 DLL 文件加载(SO for linux) 尝试连接到数据库,但它需要一些其他编译。 到处都写着有必要使用给定的命令编译应用程序:

gcc exa_7.c -o exa_7 -std=c99  `mysql_config --cflags --libs'

命令是最正确的,但我无法组合到属性:-c -fPIC

当我尝试在不传递 MYSQL 所需的属性的情况下触发代码时,它会导致我出现很多错误:

dllmain.c:46:5: error: unknown type name ‘MYSQL’; did you mean ‘MYSQL_XID’?
     MYSQL      *MySQLConRet;
     ^~~~~
     MYSQL_XID
dllmain.c:47:5: error: unknown type name ‘MYSQL’; did you mean ‘MYSQL_XID’?
     MYSQL      *MySQLConnection = NULL;
     ^~~~~
     MYSQL_XID
dllmain.c:74:5: error: unknown type name ‘MYSQL_RES’; did you mean ‘MYSQL_XID’?
     MYSQL_RES      *mysqlResult = NULL;
     ^~~~~~~~~
     MYSQL_XID
dllmain.c:90:6: error: unknown type name ‘MYSQL_ROW’; did you mean ‘MYSQL_XID’?
      MYSQL_ROW       mysqlRow;
      ^~~~~~~~~
      MYSQL_XID
dllmain.c:91:6: error: unknown type name ‘MYSQL_FIELD’; did you mean ‘MYSQL_XID’?
      MYSQL_FIELD    *mysqlFields;
      ^~~~~~~~~~~
      MYSQL_XID

我的清单包括:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <mysql/mysql.h>
#include <mysql/my_global.h>
#include <mysql/my_config.h>
#include <sys/socket.h> 
#include <arpa/inet.h>
#include "ts3_functions.h"
#include "plugin.h"
#include <pthread.h>
//(more includes, but not important)

你知道如何解决这个问题吗?

//编辑: 删除 #include <mysql/mysql.h>

的评论

现在我用命令编译它:gcc -pthread -c -fPIC -o test.o dllmain.c

编译运行正确(没有任何错误),但是当运行程序得到消息:undefined symbol: mysql_init

可能是代码有问题?:

MYSQL      *MySQLConRet;
MYSQL      *MySQLConnection = NULL;

MySQLConnection = mysql_init( NULL );
MySQLConRet = mysql_real_connect( MySQLConnection,
                                  hostName, 
                                  userId, 
                                  password, 
                                  DB, 
                                  0, 
                                  NULL,
                                  0 );

if ( MySQLConRet == NULL ) {
    printf(mysql_error(MySQLConnection) );
    exit(1);
}

您还没有包含 <mysql/mysql.h>。取消注释该行。此文件需要使用 MySQL API。 <mysql/my_global.h> 是一个内部头文件,用于包含通用系统头文件和定义实用宏 -- 它 不是 <mysql/mysql.h> 的替代品,不应直接包含完全没有。